Skip to content

Commit 6ed0f85

Browse files
committed
Process input_options.type textarea as related tag
1 parent eeb0fae commit 6ed0f85

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

lib/datagrid/filters.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ def filter_by_name(attribute)
9393
# Accepts a block or a symbol with an instance method name
9494
# * <tt>:unless</tt> - specify the reverse condition when the filter can be dislayed and used.
9595
# Accepts a block or a symbol with an instance method name
96-
# * <tt>:input_options</tt> - options passed when rendering html input tag attributes
96+
# * <tt>:input_options</tt> - options passed when rendering html input tag attributes.
97+
# Use <tt>input_options.type</tt> to control input type including <tt>textarea</tt>.
9798
# * <tt>:label_options</tt> - options passed when rendering html label tag attributes
9899
#
99100
# See: https://github.com/bogdan/datagrid/wiki/Filters for examples

lib/datagrid/form_builder.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ module FormBuilder
1010
# * <tt>text_field</tt> for other filter types
1111
def datagrid_filter(filter_or_attribute, partials: nil, **options, &block)
1212
filter = datagrid_get_filter(filter_or_attribute)
13-
options = {
13+
self.send(
14+
filter.form_builder_helper_name, filter,
1415
**filter.input_options,
1516
**add_html_classes(options, filter.name, datagrid_filter_html_class(filter)),
16-
}
17-
self.send(filter.form_builder_helper_name, filter, options, &block)
17+
&block
18+
)
1819
end
1920

2021
# @param filter_or_attribute [Datagrid::Filters::BaseFilter, String, Symbol] filter object or filter name
@@ -28,7 +29,12 @@ def datagrid_label(filter_or_attribute, text = nil, **options, &block)
2829

2930
def datagrid_filter_input(attribute_or_filter, **options)
3031
filter = datagrid_get_filter(attribute_or_filter)
31-
text_field filter.name, value: object.filter_value_as_string(filter), **options
32+
value = object.filter_value_as_string(filter)
33+
if options[:type]&.to_sym == :textarea
34+
text_area filter.name, value: value, **options, type: nil
35+
else
36+
text_field filter.name, value: value, **options
37+
end
3238
end
3339

3440
protected

spec/datagrid/form_builder_spec.rb

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,30 @@ class MyTemplate
8888
end
8989
end
9090
context "with input_options" do
91-
let(:_filter) { :created_at }
92-
let(:_grid) {
93-
test_report do
94-
scope {Entry}
95-
filter(:created_at, :date, input_options: {type: :date})
96-
end
97-
}
98-
it { should equal_to_dom(
99-
'<input type="date" class="created_at date_filter" name="report[created_at]" id="report_created_at"/>'
100-
)}
91+
context "type is date" do
92+
let(:_filter) { :created_at }
93+
let(:_grid) {
94+
test_report do
95+
scope {Entry}
96+
filter(:created_at, :date, input_options: {type: :date})
97+
end
98+
}
99+
it { should equal_to_dom(
100+
'<input type="date" class="created_at date_filter" name="report[created_at]" id="report_created_at"/>'
101+
)}
102+
end
103+
context "type is textarea" do
104+
let(:_filter) { :name }
105+
let(:_grid) {
106+
test_report do
107+
scope {Entry}
108+
filter(:name, :string, input_options: {type: :textarea})
109+
end
110+
}
111+
it { should equal_to_dom(
112+
'<textarea class="name string_filter" name="report[name]" id="report_name"/>'
113+
)}
114+
end
101115
end
102116

103117
context "with integer filter type and range option" do
@@ -296,7 +310,7 @@ class MyTemplate
296310
let(:_grid) do
297311
test_report do
298312
scope {Entry}
299-
filter(:category, :enum, :select => ["first", "second"], :include_blank => proc { "Choose plz" })
313+
filter(:category, :enum, :select => ["first", "second"], :include_blank => proc { "Choose plz" })
300314
end
301315
end
302316
let(:_filter) { :category }
@@ -379,16 +393,16 @@ class MyTemplate
379393
it {should equal_to_dom('<input class="name string_filter" type="text" name="report[name]" id="report_name">')}
380394

381395
context "when multiple option is set" do
382-
let(:_grid) do
383-
test_report(:name => "one,two") do
384-
scope {Entry}
385-
filter(:name, :string, :multiple => true)
396+
let(:_grid) do
397+
test_report(:name => "one,two") do
398+
scope {Entry}
399+
filter(:name, :string, :multiple => true)
400+
end
386401
end
387-
end
388402

389-
let(:_filter) { :name }
403+
let(:_filter) { :name }
390404

391-
it {should equal_to_dom('<input value="one,two" class="name string_filter" type="text" name="report[name]" id="report_name">')}
405+
it {should equal_to_dom('<input value="one,two" class="name string_filter" type="text" name="report[name]" id="report_name">')}
392406
end
393407
end
394408

@@ -418,7 +432,6 @@ class MyTemplate
418432
it { should equal_to_dom(
419433
'<input class="group_id float_filter" type="text" name="report[group_id]" id="report_group_id"/>'
420434
)}
421-
422435
end
423436

424437
context "with enum multiple filter" do

0 commit comments

Comments
 (0)