Module: MightyGrid::ViewHelpers

Defined in:
lib/mighty_grid/helpers/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#blank_slate_template(rendering) ⇒ Object


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mighty_grid/helpers/view_helpers.rb', line 67

def blank_slate_template(rendering)
  if rendering.blank_slate_handler.present?
    case rendering.blank_slate_handler
    when Proc then rendering.blank_slate_handler.call
    when String then rendering.blank_slate_handler
    when Hash then render(rendering.blank_slate_handler)
    end
  else
     :div, 'No records found'
  end
end

#define_grid(grid, opts = {}) {|rendering| ... } ⇒ Object

Yields:

  • (rendering)

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mighty_grid/helpers/view_helpers.rb', line 8

def define_grid(grid, opts = {}, &block)
  rendering = GridRenderer.new(grid, self)

  yield rendering

  options = {
    html: {},
    header_tr_html: {},
    order_type: MightyGrid.order_type,
    order_asc: MightyGrid.order_asc,
    order_desc: MightyGrid.order_desc,
    order_asc_link_class: MightyGrid.order_asc_link_class,
    order_desc_link_class: MightyGrid.order_desc_link_class,
    order_active_link_class: MightyGrid.order_active_link_class,
    order_wrapper_class: MightyGrid.order_wrapper_class
  }

  opts.assert_valid_keys(options.keys)

  options.merge!(opts)

  options[:order_wrapper_class] = MightyGrid::MgHTML.join_html_classes({}, 'mg-order-wrapper', options[:order_wrapper_class])[:class]

  table_html_attrs = MightyGrid::MgHTML.join_html_classes(options[:html], 'mighty-grid', MightyGrid.table_class)

  grid.read

  if grid.relation.total_count > 0
    grid.output_buffer =  :table, table_html_attrs do
      html = header_grid_html(rendering, grid, options)
      html += footer_grid_html(rendering, grid)
      html += body_grid_html(rendering, grid)
      html
    end
  else
    grid.output_buffer = blank_slate_template(rendering)
  end
end

#grid(grid, opts = {}, &block) ⇒ Object


3
4
5
6
# File 'lib/mighty_grid/helpers/view_helpers.rb', line 3

def grid(grid, opts = {}, &block)
  define_grid(grid, opts, &block)
  render_grid(grid)
end

#mighty_filter_for(grid, options = {}, &block) ⇒ Object

Creates a form to filter the data in the target grid.


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mighty_grid/helpers/view_helpers.rb', line 55

def mighty_filter_for(grid, options = {}, &block)
  html_options = options[:html] ||= {}
  html_options = MightyGrid::MgHTML.join_html_classes(html_options, 'mighty-grid-filter')
  html_options[:method] = options.delete(:method) if options.key?(:method)
  html_options[:method] ||= :get

  filter = FilterRenderer.new(grid, self)

  output = capture(filter, &block)
  form_tag(options[:url] || {}, html_options) { output }
end

#render_grid(grid) ⇒ Object

Used after define_grid to actually output the grid HTML code. Usually used with detached filters: first define_grid, then grid_filters, and then render_grid


50
51
52
# File 'lib/mighty_grid/helpers/view_helpers.rb', line 50

def render_grid(grid)
  grid.output_buffer.html_safe
end