Skip to content

Commit

Permalink
Add missing support for date_select method.
Browse files Browse the repository at this point in the history
Prior to this commit, a corresponding implementation for
[ActionView::Helpers::FormBuilder#date_select][date_select] was omitted.

This commit corrects that oversight.

[date_select]: https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-date_select
  • Loading branch information
seanpdoyle committed Aug 6, 2020
1 parent c9d000b commit d3bbac5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ version links.

## main

Add missing support for `date_select` method.

[date_select]: https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-date_select

Use [ActionView::Template][] instances to render templates instead of
[ActionView::Helpers::RenderingHelper][]-provided `render()` method.

Expand Down
11 changes: 11 additions & 0 deletions lib/view_partial_form_builder/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ def time_zone_select(method, priority_zones = nil, options = {}, **html_options)
render_partial("time_zone_select", locals, fallback: -> { super })
end

def date_select(method, options = {}, **html_options)
locals = {
method: method,
options: options,
html_options: HtmlAttributes.new(html_options),
arguments: [method, options, html_options],
}

render_partial("date_select", locals, fallback: -> { super })
end

def hidden_field(method, **options)
@emitted_hidden_id = true if method == :id

Expand Down
59 changes: 59 additions & 0 deletions test/view_partial_form_builder/date_select_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require "form_builder_test_case"

class ViewPartialFormBuilderDateSelectTest < FormBuilderTestCase
test "renders defaults when overrides are not declared" do
render(inline: <<~ERB)
<%= form_with(model: Post.new) do |form| %>
<%= form.date_select(
:start_date,
{
prompt: true,
order: [:year],
start_year: 2000,
end_year: 2001,
},
{}
) %>
<% end %>
ERB

assert_select %(option[value=""]), count: 1
assert_select %(option[value="2000"]), text: "2000"
assert_select %(option[value="2001"]), text: "2001"
end

test "renders arguments as local assigns" do
declare_template "form_builder/_date_select.html.erb", <<~'HTML'
<%= form.date_select(
method,
options.merge(
prompt: true,
order: [:year],
start_year: 2000,
),
class: "year #{html_options.delete(:class)}",
**html_options
) %>
HTML

render(inline: <<~ERB)
<%= form_with(model: Post.new) do |form| %>
<%= form.date_select(
:start_date,
{
order: [:year],
end_year: 2001,
},
class: "post-year",
"data-attr": "foo",
) %>
<% end %>
ERB

assert_select %(select[class~="year"][class~="post-year"][data-attr="foo"]) do
assert_select %(option[value=""]), count: 1
assert_select %(option[value="2000"]), text: "2000"
assert_select %(option[value="2001"]), text: "2001"
end
end
end

0 comments on commit d3bbac5

Please sign in to comment.