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



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/datagrid/form_builder.rb', line 22

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



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
86
87
88
89
90
91
92
93
94
95
# File 'lib/datagrid/form_builder.rb', line 50

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 :checkbox
    value = options.fetch(:value, 1).to_s
    if filter.enum_checkboxes? && enum_checkbox_checked?(filter, value) && !options.key?(:checked)
      options[:checked] = true
    end
    check_box filter.name, options, value
  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
    public_send(
      TYPE_METHOD_MAP[type] || :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



39
40
41
42
43
# File 'lib/datagrid/form_builder.rb', line 39

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