Sha256: 96fc9b3a6df3cfee5f709db0d3c9e4174c809743e67c79185f2dc1fbb941a419

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

module MightyGrid
  class GridRenderer
    attr_reader :columns, :th_columns, :blank_slate_handler, :row_attributes_handler

    def initialize(grid, view)
      @grid = grid
      @columns = []
      @th_columns = []
      @blank_slate_handler = nil
      @row_attributes_handler = proc {}
    end

    def column(attr_or_options = {}, options = nil, &block)
      if attr_or_options.is_a?(Hash)
        options = attr_or_options.symbolize_keys
      else
        attribute = attr_or_options.to_sym
        options = {} unless options.is_a?(Hash)
      end

      if attribute.present?
        options = {
          ordering: true,
          attribute: attribute,
          title: @grid.klass.human_attribute_name(attribute)
        }.merge!(options)
      end

      if block_given?
        @columns << MightyGrid::Column.new(options, &block)
      else
        @columns << MightyGrid::Column.new(options)
      end
    end

    def actions(opts = {})
      options = {
        partial: 'mighty_grid/actions',
        only: [:show, :edit, :destroy],
        html: {},
        th_html: {},
        title: I18n.t('mighty_grid.actions', default: 'Actions')
      }

      opts.assert_valid_keys(options.keys)
      options.merge!(opts)

      @columns << MightyGrid::Column.new(options) { |object| @grid.controller.render_to_string(partial: options[:partial], locals: { actions: options[:only], object: object }) }
    end

    def row_attributes(&block)
      @row_attributes_handler = block if block_given?
    end

    def blank_slate(html_or_opts = nil, &block)
      if (html_or_opts.is_a?(Hash) && html_or_opts.key?(:partial) || html_or_opts.is_a?(String)) && !block_given?
        @blank_slate_handler = html_or_opts
      elsif html_or_opts.nil? && block_given?
        @blank_slate_handler = block
      else
        fail MightyGrid::Exceptions::ArgumentError.new("blank_slate accepts only a string, a block, or :partial => 'path_to_partial' ")
      end
    end

    def total_columns
      @columns.count
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mighty_grid-2.1.1 lib/mighty_grid/grid_renderer.rb
mighty_grid-2.1.0 lib/mighty_grid/grid_renderer.rb
mighty_grid-2.0.0 lib/mighty_grid/grid_renderer.rb