Sha256: 73a5dd9cd33298c413717fbd1968f9ca5acfc67fab432062d13a9f62810267e4

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 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: {
            object: nil,
            iteration: iteration
          }
        }
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lite-component-1.0.7 lib/lite/component/base.rb