Sha256: 1bc61cf00945fdacdc2a62222da172a1702bbe0cc57031c113a127dce1fb2e6c

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module TaoOnRails
  module Components
    class Base

      attr_reader :options, :template_path, :view

      def initialize view, options = {}
        @view = view
        @options = options
        @template_path = @options.delete(:template_path) || self.class.template_path
      end

      def render &block
        if view.lookup_context.exists?(template_path, [], true)
          if block_given?
            view.render layout: template_path, locals: {component: self}, &block
          else
            view.render partial: template_path, locals: {component: self}
          end
        else
          view.content_tag self.class.tag_name, options, &block
        end
      end

      def self.tag_name
        @tag_name ||= "#{self.tag_prefix}-#{self.component_name.to_s.dasherize}"
      end

      def self.component_name
        @component_name ||= self.name.underscore.split('/').map(&:singularize).join('_')
          .gsub(/(.+)_component$/, '\1')
          .gsub(/^#{Regexp.quote(self.tag_prefix.to_s.underscore)}_(.+)/, '\1')
      end

      def self.tag_prefix
        :tao
      end

      def self.template_path
        @template_path ||= "components/#{self.name.underscore.gsub(/(.+)_component$/, '\1')}"
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tao_on_rails-0.7.2 lib/tao_on_rails/components/base.rb
tao_on_rails-0.7.1 lib/tao_on_rails/components/base.rb