Module: Datagrid::FormBuilder

Defined in:
lib/datagrid/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#datagrid_filter(filter_or_attribute, **options, &block) ⇒ String

Returns a form input html for the corresponding filter name

  • select for enum, xboolean filter types
  • check_box for boolean filter type
  • text_field for other filter types.

Parameters:

  • filter_or_attribute (Datagrid::Filters::BaseFilter, String, Symbol)

    filter object or filter name

  • options (Hash)

    options of rails form input helper

Returns:

  • (String)

    a form input html for the corresponding filter name

    • select for enum, xboolean filter types
    • check_box for boolean filter type
    • text_field for other filter types


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/datagrid/form_builder.rb', line 14

def datagrid_filter(filter_or_attribute, **options, &block)
  filter = datagrid_get_filter(filter_or_attribute)
  if filter.range?
    datagrid_range_filter(filter, options, &block)
  elsif filter.enum_checkboxes?
    datagrid_enum_checkboxes_filter(filter, options, &block)
  elsif filter.type == :dynamic
    datagrid_dynamic_filter(filter, options, &block)
  else
    datagrid_filter_input(filter, **options, &block)
  end
end

#datagrid_filter_input(attribute_or_filter, **options, &block) ⇒ String

Returns an input tag for the corresponding filter name.

Parameters:

  • attribute_or_filter (Datagrid::Filters::BaseFilter, String, Symbol)

    filter object or filter name

  • options (Hash{Symbol => Object})

    HTML attributes to assign to input tag

    • type - special attribute the determines an input tag to be made. Examples: text, select, textarea, number, date etc.

Returns:

  • (String)

    an input tag for the corresponding filter name



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/datagrid/form_builder.rb', line 42

def datagrid_filter_input(attribute_or_filter, **options, &block)
  filter = datagrid_get_filter(attribute_or_filter)
  options = add_filter_options(filter, **options)
  type = options.delete(:type)&.to_sym
  if %i[datetime-local date].include?(type)
    if options.key?(:value) && options[:value].nil?
      # https://github.com/rails/rails/pull/53387
      options[:value] = ""
    end
  elsif options[:value]
    options[:value] = filter.format(options[:value])
  end
  case type
  when :"datetime-local"
    datetime_local_field filter.name, **options, &block
  when :date
    date_field filter.name, **options, &block
  when :textarea
    text_area filter.name, value: object.filter_value_as_string(filter), **options, &block
  when :checkbox
    value = options.fetch(:value, 1).to_s
    options = { checked: true, **options } if filter.enum_checkboxes? && enum_checkbox_checked?(filter, value)
    check_box filter.name, options, value
  when :hidden
    hidden_field filter.name, **options
  when :number
    number_field filter.name, **options
  when :select
    select(
      filter.name,
      object.select_options(filter) || [],
      {
        include_blank: filter.include_blank,
        prompt: filter.prompt,
        include_hidden: false,
      },
      multiple: filter.multiple?,
      **options,
      &block
    )
  else
    text_field filter.name, value: object.filter_value_as_string(filter), **options, &block
  end
end

#datagrid_label(filter_or_attribute, text = nil, **options, &block) ⇒ String

Returns a form label tag for the corresponding filter name.

Parameters:

  • filter_or_attribute (Datagrid::Filters::BaseFilter, String, Symbol)

    filter object or filter name

  • text (String, nil) (defaults to: nil)

    label text, defaults to filter.header

  • options (Hash)

    options of rails label helper

Returns:

  • (String)

    a form label tag for the corresponding filter name



31
32
33
34
35
# File 'lib/datagrid/form_builder.rb', line 31

def datagrid_label(filter_or_attribute, text = nil, **options, &block)
  filter = datagrid_get_filter(filter_or_attribute)
  options = { **filter.label_options, **options }
  label(filter.name, text || filter.header, **options, &block)
end