Skip to content

Commit 5a4f4b2

Browse files
Create a partial for dynamic filters (#347)
* Create a partial for dynamic filters * Remove "add_html_classes" method * Update the datagrid_filter_input select method * Remove obsolete dynamic_filter_select method * Deprecate the select_options method * Use the select_choices method instead of select_options
1 parent 833131d commit 5a4f4b2

File tree

6 files changed

+76
-54
lines changed

6 files changed

+76
-54
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [2.0.10]
4+
5+
* Added a partial for the dynamic filter
6+
* The `datagrid_filter_input` method now accepts options for the select input
7+
* Rename the `select_options` method to `select_choices`
8+
39
## [2.0.9]
410

511
* Use new Rails 7.1 `locals` notation in ERB partials
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<%# locals: (filter:, form:, field_options:, operation_options:, value_options:) -%>
2+
3+
<%= form.datagrid_filter_input(
4+
filter,
5+
**field_options,
6+
class: [*field_options[:class], "datagrid-dynamic-field"]
7+
) %>
8+
9+
<%= form.datagrid_filter_input(
10+
filter,
11+
**operation_options,
12+
class: [*operation_options[:class], "datagrid-dynamic-operation"]
13+
) %>
14+
15+
<%= form.datagrid_filter_input(
16+
filter,
17+
**value_options,
18+
class: [*value_options[:class], "datagrid-dynamic-value"]
19+
) %>

lib/datagrid/filters.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,15 @@ def filter_by(*filters)
328328
apply_filters(scope, filters.map { |f| filter_by_name(f) })
329329
end
330330

331+
# @!visibility private
332+
def select_options(filter)
333+
warn "[DEPRECATION] `select_options` is deprecated. Please use `select_choices` instead."
334+
select_choices(filter)
335+
end
336+
331337
# @return [Array] the select options for the filter
332338
# @raise [ArgumentError] if the filter doesn't support select options
333-
def select_options(filter)
339+
def select_choices(filter)
334340
find_select_filter(filter).select(self)
335341
end
336342

lib/datagrid/form_builder.rb

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ def datagrid_filter_input(attribute_or_filter, **options, &block)
7373
when :select
7474
select(
7575
filter.name,
76-
object.select_options(filter) || [],
76+
options.delete(:select_choices) || object.select_choices(filter) || [],
7777
{
7878
include_blank: filter.include_blank,
7979
prompt: filter.prompt,
8080
include_hidden: false,
81+
**options.delete(:select_options),
8182
},
8283
multiple: filter.multiple?,
8384
**options,
@@ -97,7 +98,7 @@ def datagrid_filter_input(attribute_or_filter, **options, &block)
9798
protected
9899

99100
def datagrid_enum_checkboxes_filter(filter, options = {})
100-
elements = object.select_options(filter).map do |element|
101+
elements = object.select_choices(filter).map do |element|
101102
text, value = @template.send(:option_text_and_value, element)
102103
checked = enum_checkbox_checked?(filter, value)
103104
[value, text, checked]
@@ -137,50 +138,55 @@ def enum_checkbox_checked?(filter, option_value)
137138
end
138139

139140
def datagrid_dynamic_filter(filter, options = {})
140-
field, operation, value = object.filter_value(filter)
141141
options = add_filter_options(filter, **options)
142-
field_input = dynamic_filter_select(
143-
filter.name,
144-
object.select_options(filter) || [],
142+
field, operation, value = object.filter_value(filter)
143+
field_options = datagrid_dynamic_field_options(options: options, field: field, filter: filter)
144+
operation_options = datagrid_dynamic_operation_options(options: options, operation: operation, filter: filter)
145+
value_options = datagrid_dynamic_value_options(options: options, value: value, filter: filter)
146+
render_partial(
147+
"dynamic_filter",
148+
{ filter: filter,
149+
form: self,
150+
field_options: field_options,
151+
operation_options: operation_options,
152+
value_options: value_options, },
153+
)
154+
end
155+
156+
def datagrid_dynamic_field_options(options:, field:, filter:)
157+
options.merge(
145158
{
146-
include_blank: filter.include_blank,
147-
prompt: filter.prompt,
148-
include_hidden: false,
149-
selected: field,
159+
type: :select,
160+
select_options: {
161+
selected: field,
162+
},
163+
name: @template.field_name(object_name, filter.name, "field"),
150164
},
151-
**add_html_classes(options, "datagrid-dynamic-field"),
152-
name: @template.field_name(object_name, filter.name, "field"),
153165
)
154-
operation_input = dynamic_filter_select(
155-
filter.name, filter.operations_select,
166+
end
167+
168+
def datagrid_dynamic_operation_options(options:, operation:, filter:)
169+
options.merge(
156170
{
157-
include_blank: false,
158-
include_hidden: false,
159-
prompt: false,
160-
selected: operation,
171+
type: :select,
172+
select_choices: filter.operations_select,
173+
select_options: {
174+
include_blank: false,
175+
prompt: false,
176+
selected: operation,
177+
},
178+
name: @template.field_name(object_name, filter.name, "operation"),
161179
},
162-
**add_html_classes(options, "datagrid-dynamic-operation"),
163-
name: @template.field_name(object_name, filter.name, "operation"),
164-
)
165-
value_input = datagrid_filter_input(
166-
filter.name,
167-
**add_html_classes(options, "datagrid-dynamic-value"),
168-
value: value,
169-
name: @template.field_name(object_name, filter.name, "value"),
170180
)
171-
[field_input, operation_input, value_input].join("\n").html_safe
172181
end
173182

174-
def dynamic_filter_select(name, variants, select_options, html_options)
175-
if variants.size <= 1
176-
value = variants.first
177-
# select options format may vary
178-
value = value.last if value.is_a?(Array)
179-
# don't render any visible input when there is nothing to choose from
180-
hidden_field(name, **html_options, value: value)
181-
else
182-
select(name, variants, select_options, html_options)
183-
end
183+
def datagrid_dynamic_value_options(options:, value:, filter:)
184+
options.merge(
185+
{
186+
value: value,
187+
name: @template.field_name(object_name, filter.name, "value"),
188+
},
189+
)
184190
end
185191

186192
def datagrid_range_filter(filter, options = {})
@@ -205,10 +211,6 @@ def datagrid_get_filter(attribute_or_filter)
205211
raise(ArgumentError, "Datagrid filter #{attribute_or_filter} not found")
206212
end
207213

208-
def add_html_classes(options, *classes)
209-
Datagrid::Utils.add_html_classes(options, *classes)
210-
end
211-
212214
def partial_path(name)
213215
if (partials = options[:partials])
214216
partial_name = File.join(partials, name)

lib/datagrid/rspec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
when :integer
4343
1
4444
when :enum
45-
select = subject.select_options(filter)
45+
select = subject.select_choices(filter)
4646
select.first&.last
4747
else
4848
raise "unknown filter type: #{filter.class}"

lib/datagrid/utils.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,6 @@ def warn_once(message, delay = 5)
4141
true
4242
end
4343

44-
def add_html_classes(options, *classes)
45-
return options if classes.empty?
46-
47-
options = options.clone
48-
options[:class] ||= []
49-
array = options[:class].is_a?(Array)
50-
value = [*options[:class], *classes]
51-
options[:class] = array ? value : value.join(" ")
52-
options
53-
end
54-
5544
def string_like?(value)
5645
value.is_a?(Symbol) || value.is_a?(String)
5746
end

0 commit comments

Comments
 (0)