Sha256: 0cc7e11d7ab7ba81daa66293295c70a0ff522fc5ab961ca6649064f839ce71c2

Contents?: true

Size: 1.1 KB

Versions: 9

Compression:

Stored size: 1.1 KB

Contents

module TableHelp
  class AttributesTableFor
    attr_reader :resource, :context, :options, :rows
    delegate :concat, :capture, :tag, to: :context

    def initialize(resource, context, options = {})
      @resource = resource
      @context  = context
      @options  = default_options.merge(options)
      @rows     = []
    end

    def row(name = nil, method_name = nil, &block)
      rows << [
        Formatter.format_attribute_name(name, resource),
        Strategy.new(name, block_given? ? block : method_name),
      ]
    end

    def to_html
      return if resource.nil?
      tag.table(tbody, options)
    end

    private

      def tbody
        tag.tbody do
          rows.each do |attr_name, strategy|
            concat(
              tag.tr do
                concat tag.th(attr_name)
                concat tag.td(Formatter.format_value(strategy.name, strategy.to_value(resource, context)), class: "col-#{strategy.name}")
              end,
            )
          end
        end
      end

      def default_options
        TableHelp.config.default_options[:attributes_table_for] || {}
      end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
table_help-0.2.0 lib/table_help/attributes_table_for.rb
table_help-0.1.7 lib/table_help/attributes_table_for.rb
table_help-0.1.6 lib/table_help/attributes_table_for.rb
table_help-0.1.5 lib/table_help/attributes_table_for.rb
table_help-0.1.4 lib/table_help/attributes_table_for.rb
table_help-0.1.3 lib/table_help/attributes_table_for.rb
table_help-0.1.2 lib/table_help/attributes_table_for.rb
table_help-0.1.1 lib/table_help/attributes_table_for.rb
table_help-0.1.0 lib/table_help/attributes_table_for.rb