Class: MightyGrid::FilterRenderer

Inherits:
Object
  • Object
show all
Includes:
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


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

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

Instance Method Details

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

Get checkbox


43
44
45
46
47
48
49
# File 'lib/mighty_grid/filter_renderer.rb', line 43

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

  f_options = filter_options(name, options)

  check_box_tag(@grid.get_filter_name(name, f_options[:model]), value, checked, options)
end

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

Get label HTML tag


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

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

  f_options = filter_options(name, options, false)

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

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

Get button for Reset filter changes


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

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

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

Get select HTML tag


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

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

  selected = nil
  if options.key?(:selected)
    selected = options.delete(:selected)
    @grid.add_filter_param(get_filter_param_name(name, f_options[:model]), selected)
  end

  selected = get_filter_param(name, f_options[:model]) if get_filter_param(name, f_options[:model])
  opts = options_for_select(option_tags, selected)

  select_tag(@grid.get_filter_name(name, f_options[:model]), opts, options)
end

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

Get button for Apply filter changes


52
53
54
55
56
# File 'lib/mighty_grid/filter_renderer.rb', line 52

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

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

Get input HTML tag


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

def text_field(name, options = {})
  f_options = filter_options(name, options)
  text_field_tag(@grid.get_filter_name(name, f_options[:model]), get_filter_param(name, f_options[:model]), options)
end