Sha256: e26850ef2173cd0badda7f1725719ddbce76f72206de4b8fb02cf55f6ca2a452

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 KB

Contents

module Trestle
  class Table
    class Row
      attr_reader :options, :block

      def initialize(options={}, &block)
        @options = options
        @block = block if block_given?
      end

      def renderer(table:, template:)
        Renderer.new(self, table: table, template: template)
      end

      class Renderer
        def initialize(row, table:, template:)
          @row, @table, @template = row, table, template
        end

        def columns
          @table.columns.map { |column|
            column.renderer(table: @table, template: @template)
          }.select(&:render?)
        end

        def render(instance)
          @template.content_tag(:tr, options(instance)) do
            @template.safe_join(columns.map { |column| column.render(instance) }, "\n")
          end
        end

        def options(instance)
          options = Trestle::Options.new

          if @table.admin && @table.autolink? && @table.admin.actions.include?(:show)
            options.merge!(data: { url: admin_url_for(instance) })
            options.merge!(data: { behavior: "dialog" }) if @table.admin.form.dialog?
          end

          options.merge!(@row.options)
          options.merge!(@template.instance_exec(instance, &@row.block)) if @row.block

          options
        end

      protected
        def admin_url_for(instance)
          @template.admin_url_for(instance, admin: @table.admin)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
trestle-0.9.8 lib/trestle/table/row.rb
trestle-0.9.7 lib/trestle/table/row.rb
trestle-0.9.6 lib/trestle/table/row.rb
trestle-0.9.5 lib/trestle/table/row.rb
trestle-0.9.4 lib/trestle/table/row.rb
trestle-0.9.3 lib/trestle/table/row.rb