Skip to content

Commit

Permalink
Process input_options.type textarea as related tag
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Dec 11, 2023
1 parent eeb0fae commit 6ed0f85
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
3 changes: 2 additions & 1 deletion lib/datagrid/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def filter_by_name(attribute)
# Accepts a block or a symbol with an instance method name
# * <tt>:unless</tt> - specify the reverse condition when the filter can be dislayed and used.
# Accepts a block or a symbol with an instance method name
# * <tt>:input_options</tt> - options passed when rendering html input tag attributes
# * <tt>:input_options</tt> - options passed when rendering html input tag attributes.
# Use <tt>input_options.type</tt> to control input type including <tt>textarea</tt>.
# * <tt>:label_options</tt> - options passed when rendering html label tag attributes
#
# See: https://github.com/bogdan/datagrid/wiki/Filters for examples
Expand Down
14 changes: 10 additions & 4 deletions lib/datagrid/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ module FormBuilder
# * <tt>text_field</tt> for other filter types
def datagrid_filter(filter_or_attribute, partials: nil, **options, &block)
filter = datagrid_get_filter(filter_or_attribute)
options = {
self.send(
filter.form_builder_helper_name, filter,
**filter.input_options,
**add_html_classes(options, filter.name, datagrid_filter_html_class(filter)),
}
self.send(filter.form_builder_helper_name, filter, options, &block)
&block
)
end

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

def datagrid_filter_input(attribute_or_filter, **options)
filter = datagrid_get_filter(attribute_or_filter)
text_field filter.name, value: object.filter_value_as_string(filter), **options
value = object.filter_value_as_string(filter)
if options[:type]&.to_sym == :textarea
text_area filter.name, value: value, **options, type: nil
else
text_field filter.name, value: value, **options
end
end

protected
Expand Down
51 changes: 32 additions & 19 deletions spec/datagrid/form_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,30 @@ class MyTemplate
end
end
context "with input_options" do
let(:_filter) { :created_at }
let(:_grid) {
test_report do
scope {Entry}
filter(:created_at, :date, input_options: {type: :date})
end
}
it { should equal_to_dom(
'<input type="date" class="created_at date_filter" name="report[created_at]" id="report_created_at"/>'
)}
context "type is date" do
let(:_filter) { :created_at }
let(:_grid) {
test_report do
scope {Entry}
filter(:created_at, :date, input_options: {type: :date})
end
}
it { should equal_to_dom(
'<input type="date" class="created_at date_filter" name="report[created_at]" id="report_created_at"/>'
)}
end
context "type is textarea" do
let(:_filter) { :name }
let(:_grid) {
test_report do
scope {Entry}
filter(:name, :string, input_options: {type: :textarea})
end
}
it { should equal_to_dom(
'<textarea class="name string_filter" name="report[name]" id="report_name"/>'
)}
end
end

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

context "when multiple option is set" do
let(:_grid) do
test_report(:name => "one,two") do
scope {Entry}
filter(:name, :string, :multiple => true)
let(:_grid) do
test_report(:name => "one,two") do
scope {Entry}
filter(:name, :string, :multiple => true)
end
end
end

let(:_filter) { :name }
let(:_filter) { :name }

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

Expand Down Expand Up @@ -418,7 +432,6 @@ class MyTemplate
it { should equal_to_dom(
'<input class="group_id float_filter" type="text" name="report[group_id]" id="report_group_id"/>'
)}

end

context "with enum multiple filter" do
Expand Down

0 comments on commit 6ed0f85

Please sign in to comment.