Class: MightyGrid::FilterRenderer

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers
Defined in:
lib/mighty_grid/filter_renderer.rb

Instance Method Summary collapse

Constructor Details

#initialize(grid, view) ⇒ FilterRenderer

Returns a new instance of FilterRenderer.



6
7
8
# File 'lib/mighty_grid/filter_renderer.rb', line 6

def initialize(grid, view)
  @grid = grid
end

Instance Method Details

#check_box(name, value = '1', checked = false, options = {}) ⇒ Object

Get checkbox



40
41
42
43
44
# File 'lib/mighty_grid/filter_renderer.rb', line 40

def check_box(name, value = '1', checked = false, options = {})
  checked = true if get_filter_param(name)

  check_box_tag(@grid.get_filter_name(name), value, checked, options)
end

#label(name, content_or_options = nil, options = nil, &block) ⇒ Object

Get label HTML tag



11
12
13
14
15
16
# File 'lib/mighty_grid/filter_renderer.rb', line 11

def label(name, content_or_options = nil, options = nil, &block)
  options = content_or_options if content_or_options.is_a?(Hash)
  human_name = (content_or_options.is_a?(Hash) || content_or_options.nil?) ? @grid.klass.human_attribute_name(name) : content_or_options

  label_tag(get_filter_id(name), human_name, options, &block)
end

#reset(content = nil, options = {}, &block) ⇒ Object

Get button for Reset filter changes



59
60
61
62
63
64
65
66
67
# File 'lib/mighty_grid/filter_renderer.rb', line 59

def reset(content = nil, options = {}, &block)
  content = I18n.t('mighty_grid.filters.reset', default: 'Reset changes') if content.blank?
  options.merge!(type: :reset)
  if block_given?
    (:button, nil, options, &block)
  else
    (:button, content, options)
  end
end

#select(name, options = {}) ⇒ Object

Get select HTML tag



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mighty_grid/filter_renderer.rb', line 24

def select(name, options = {})
  option_tags = @grid.filters[name].collection

  selected = nil
  if options.key?(:selected)
    selected = options.delete(:selected)
    @grid.add_filter_param(name, selected)
  end

  selected = get_filter_param(name) if get_filter_param(name)
  opts = options_for_select(option_tags, selected)

  select_tag(@grid.get_filter_name(name), opts, options)
end

#submit(content = nil, options = {}, &block) ⇒ Object

Get button for Apply filter changes



47
48
49
50
51
52
53
54
55
56
# File 'lib/mighty_grid/filter_renderer.rb', line 47

def submit(content = nil, options = {}, &block)
  content = I18n.t('mighty_grid.filters.submit', default: 'Apply changes') if content.blank?
  options.merge!(type: :submit)
  if block_given?
    (:button, nil, options, &block)
  else
    (:button, content, options)
  end

end

#text_field(name, options = {}) ⇒ Object

Get input HTML tag



19
20
21
# File 'lib/mighty_grid/filter_renderer.rb', line 19

def text_field(name, options = {})
  text_field_tag(@grid.get_filter_name(name), get_filter_param(name), options)
end