Sha256: 41eac0ff99fd382e92d2a14ec3df774e1cad452fd3e5da1ae472d3cb57fcc372

Contents?: true

Size: 1.52 KB

Versions: 7

Compression:

Stored size: 1.52 KB

Contents

module Para
  module FormBuilder
    module Tabs
      def tabs(&block)
        manager = TabsManager.new(template, object, self)
        block.call(manager)

        template.render partial: 'para/form/tabs', locals: { tabs: manager.tabs }
      end

      class TabsManager
        attr_reader :template, :object, :builder

        def initialize(template, object, builder)
          @template = template
          @object = object
          @builder = builder
        end

        def tab(identifier, options = {}, &block)
          tabs << Tab.new(template, object, builder, identifier, options, &block)
          nil
        end

        def tabs
          @tabs ||= []
        end
      end

      class Tab
        attr_reader :template, :object, :builder, :identifier, :icon, :content

        delegate :capture, to: :template

        def initialize(template, object, builder, identifier, options, &content_block)
          @template = template
          @object = object
          @builder = builder
          @identifier = identifier
          @content = capture { content_block.call }
          @icon = options[:icon]
        end

        def title
          if Symbol === identifier
            ::I18n.t("forms.tabs.#{ object.class.model_name.i18n_key }.#{ identifier }")
          else
            identifier
          end
        end

        def dom_id
          @dom_id = [
            builder.nested_resource_dom_id.presence,
            identifier.to_s.parameterize
          ].compact.join('-')
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
para-0.7.0 lib/para/form_builder/tabs.rb
para-0.6.9 lib/para/form_builder/tabs.rb
para-0.6.8 lib/para/form_builder/tabs.rb
para-0.6.7 lib/para/form_builder/tabs.rb
para-0.6.3 lib/para/form_builder/tabs.rb
para-0.6.2 lib/para/form_builder/tabs.rb
para-0.5.4 lib/para/form_builder/tabs.rb