Sha256: aa6c4afaff33029651f55e791d62a76a8163663859aefcddeefb2761bd9a00fb
Contents?: true
Size: 1.7 KB
Versions: 1
Compression:
Stored size: 1.7 KB
Contents
# frozen_string_literal: true module Lite module Component class Base include ActionView::Context include ActionView::Helpers attr_reader :context, :options attr_writer :iteration def initialize(context, options = {}) @context = context @options = default_options.deep_merge(options) end alias helpers context alias c context alias h helpers class << self def component_name component_path.split('/').last end def component_path name.underscore.sub('_component', '') end def render(context, options = {}) klass = new(context, options) klass.render end end def iteration @iteration ||= Lite::Component::Iteration.new(1, 0) end alias i iteration def locals @locals ||= Lite::Component::Locals.new(options[:locals]) end alias l locals def render collection = options.delete(:collection) return render_content if collection.nil? || !collection.respond_to?(:each) Lite::Component::Collection.render( collection, component: self, spacer_template: options.delete(:spacer_template) ) end def render_content context.render(options) end def to_partial_path "components/#{self.class.component_path}" end private def default_options { partial: to_partial_path, object: self, as: :component, locals: { c: self, object: nil, iteration: iteration } } end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
lite-component-1.0.8 | lib/lite/component/base.rb |