8000 Allow specifying custom input_html for DateRangeInput (#5867) · activeadmin/activeadmin@634e18c · GitHub
[go: up one dir, main page]

Skip to content

Commit 634e18c

Browse files
mirelonjavierjulio
authored andcommitted
Allow specifying custom input_html for DateRangeInput (#5867)
closes #5864
1 parent 8ecfc77 commit 634e18c

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Use filter label when condition has a predicate. [#5886] by [@ko-lem]
88
* Fix error when routing with array containing symbol. [#5870] by [@jwesorick]
99
* Fix error when there is a model named `Tag` and `meta_tags` have been configured. [#5893] by [@micred], [@FabioRos] and [@deivid-rodriguez]
10+
* Allow specifying custom input_html for DateRangeInput [#5867] by [@mirelon]
1011

1112
### Removals
1213

@@ -530,6 +531,7 @@ Please check [0-6-stable] for previous changes.
530531
[#5886]: https://github.com/activeadmin/activeadmin/pull/5886
531532
[#5870]: https://github.com/activeadmin/activeadmin/pull/5870
532533
[#5893]: https://github.com/activeadmin/activeadmin/pull/5893
534+
[#5867]: https://github.com/activeadmin/activeadmin/pull/5867
533535

534536
[@5t111111]: https://github.com/5t111111
535537
[@aarek]: https://github.com/aarek
@@ -582,6 +584,7 @@ Please check [0-6-stable] for previous changes.
582584
[@mauriciopasquier]: https://github.com/mauriciopasquier
583585
[@mconiglio]: https://github.com/mconiglio
584586
[@micred]: https://github.com/micred
587+
[@mirelon]: https://github.com/mirelon
585588
[@ndbroadbent]: https://github.com/ndbroadbent
586589
[@Nguyenanh]: https://github.com/Nguyenanh
587590
[@panasyuk]: https://github.com/panasyuk

lib/active_admin/inputs/filters/date_range_input.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class DateRangeInput < ::Formtastic::Inputs::StringInput
77
def to_html
88
input_wrapping do
99
[ label_html,
10-
builder.text_field(gt_input_name, input_html_options(gt_input_name, gt_input_placeholder)),
11-
builder.text_field(lt_input_name, input_html_options(lt_input_name, lt_input_placeholder)),
10+
builder.text_field(gt_input_name, input_html_options_for(gt_input_name, gt_input_placeholder)),
11+
builder.text_field(lt_input_name, input_html_options_for(lt_input_name, lt_input_placeholder)),
1212
].join("\n").html_safe
1313
end
1414
end
@@ -22,7 +22,7 @@ def lt_input_name
2222
column && column.type == :date ? "#{method}_lteq" : "#{method}_lteq_datetime"
2323
end
2424

25-
def input_html_options(input_name = gt_input_name, placeholder = gt_input_placeholder)
25+
def input_html_options_for(input_name = gt_input_name, placeholder = gt_input_placeholder)
2626
current_value = begin
2727
#cast value to date object before rendering input
2828
@object.public_send(input_name).to_s.to_date
@@ -33,7 +33,7 @@ def input_html_options(input_name = gt_input_name, placeholder = gt_input_placeh
3333
class: "datepicker",
3434
maxlength: 10,
3535
placeholder: placeholder,
36-
value: current_value ? current_value.strftime("%Y-%m-%d") : "" }
36+
value: current_value ? current_value.strftime("%Y-%m-%d") : "" }.merge(input_html_options)
3737
end
3838

3939
def gt_input_placeholder

spec/unit/filters/filter_form_builder_spec.rb

Lines changed: 36 additions & 0 deletions
< 6D40 td data-grid-cell-id="diff-6ff83ed66ad6c24f933dea18e5fdee1a41e37958c33d408a1567d6b938eb7cbe-184-203-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,24 @@ def filter(name, options = {})
171171
it "should generate a date less than" do
172172
expect(body).to have_selector("input.datepicker[name='q[published_date_lteq]']")
173173
end
174+
175+
context "with input_html" do
176+
let(:body) { filter :published_date, input_html: { 'autocomplete': 'off' } }
177+
178+
it "should generate provided input html for both ends of date range" do
179+
expect(body).to have_selector("input.datepicker[name='q[published_date_gteq]'][autocomplete=off]")
180+
expect(body).to have_selector("input.datepicker[name='q[published_date_lteq]'][autocomplete=off]")
181+
end
182+
end
183+
184+
context "with input_html overriding the defaults" do
185+
let(:body) { filter :published_date, input_html: { 'class': 'custom_class' } }
186+
187+
it "should override the default attribute values for both ends of date range" do
188+
expect(body).to have_selector("input.custom_class[name='q[published_date_gteq]']")
189+
expect(body).to have_selector("input.custom_class[name='q[published_date_lteq]']")
190+
end
191+
end
174192
end
175193

176194
describe "datetime attribute" do
@@ -182,6 +200,24 @@ def filter(name, options = {})
182200
it "should generate a date less than" do
183201
expect(body).to have_selector("input.datepicker[name='q[created_at_lteq_datetime]']")
184202
end
203+
204+
context "with input_html" do
205+
let(:body) { filter :created_at, input_html: { 'autocomplete': 'off' } }
206+
207+
it "should generate provided input html for both ends of date range" do
208+
expect(body).to have_selector("input.datepicker[name='q[created_at_gteq_datetime]'][autocomplete=off]")
209+
expect(body).to have_selector("input.datepicker[name='q[created_at_lteq_datetime]'][autocomplete=off]")
210+
end
211+
end
212+
213+
context "with input_html overriding the defaults" do
214+
let(:body) { filter :created_at, input_html: { 'class': 'custom_class' } }
215+
216+
it "should override the default attribute values for both ends of date range" do
217+
expect(body).to have_selector("input.custom_class[name='q[created_at_gteq_datetime]']")
218+
expect(body).to have_selector("input.custom_class[name='q[created_at_lteq_datetime]']")
219+
end
220 4802 +
end
185221
end
186222

187223
describe "integer attribute" do

0 commit comments

Comments
 (0)
0