Class: MightyGrid::GridRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/mighty_grid/grid_renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grid, view) ⇒ GridRenderer

Returns a new instance of GridRenderer


5
6
7
8
9
10
11
# File 'lib/mighty_grid/grid_renderer.rb', line 5

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

Instance Attribute Details

#blank_slate_handlerObject (readonly)

Returns the value of attribute blank_slate_handler


3
4
5
# File 'lib/mighty_grid/grid_renderer.rb', line 3

def blank_slate_handler
  @blank_slate_handler
end

#columnsObject (readonly)

Returns the value of attribute columns


3
4
5
# File 'lib/mighty_grid/grid_renderer.rb', line 3

def columns
  @columns
end

#row_attributes_handlerObject (readonly)

Returns the value of attribute row_attributes_handler


3
4
5
# File 'lib/mighty_grid/grid_renderer.rb', line 3

def row_attributes_handler
  @row_attributes_handler
end

#th_columnsObject (readonly)

Returns the value of attribute th_columns


3
4
5
# File 'lib/mighty_grid/grid_renderer.rb', line 3

def th_columns
  @th_columns
end

Instance Method Details

#actions(opts = {}) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mighty_grid/grid_renderer.rb', line 36

def actions(opts = {})
  options = {
    partial: 'mighty_grid/actions',
    only: [:show, :edit, :destroy]
  }

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

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

#blank_slate(html_or_opts = nil, &block) ⇒ Object


52
53
54
55
56
57
58
59
60
# File 'lib/mighty_grid/grid_renderer.rb', line 52

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

#column(attr_or_options = {}, options = nil, &block) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mighty_grid/grid_renderer.rb', line 13

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

#row_attributes(&block) ⇒ Object


48
49
50
# File 'lib/mighty_grid/grid_renderer.rb', line 48

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

#total_columnsObject


62
63
64
# File 'lib/mighty_grid/grid_renderer.rb', line 62

def total_columns
  @columns.count
end