Sha256: 92dfc5ed16b0d501201473853e5e375ff6a7a895800ff2d83715147a6599172a

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

module Trestle
  class Table
    class ActionsColumn
      attr_reader :table, :block

      def initialize(table, &block)
        @table = table
        @block = block_given? ? block : default_actions
      end

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

      def default_actions
        ->(action) do
          action.delete
        end
      end

      class ActionsBuilder
        attr_reader :template, :instance

        def initialize(template, instance)
          @template, @instance = template, instance
        end

        def button(content, url, options={})
          options[:class] = Array(options[:class])
          options[:class] << "btn" unless options[:class].include?("btn")

          template.concat template.link_to(content, url, options)
        end

        def delete
          button(template.icon("fa fa-trash"), template.admin.path(:destroy, id: template.admin.to_param(instance)), method: :delete, class: "btn-danger", data: { toggle: "confirm-delete", placement: "left" })
        end
      end

      class Renderer < Column::Renderer
        def header
        end

        def classes
          "actions"
        end

        def options
          {}
        end

        def data
          {}
        end

        def content(instance)
          builder = ActionsBuilder.new(@template, instance)

          @template.with_output_buffer do
            @template.instance_exec(builder, &@column.block)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trestle-0.8.2 lib/trestle/table/actions_column.rb
trestle-0.8.0 lib/trestle/table/actions_column.rb