Skip to content

Commit 13a714f

Browse files
authored
Merge pull request rails#42979 from DRBragg/drbragg/add-weekday-select
Add `weekday_options_for_select` method
2 parents ef65eee + 592570f commit 13a714f

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

actionview/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Add `weekday_options_for_select` and `weekday_select` helper methods. Also adds `weekday_select` to `FormBuilder`.
2+
3+
*Drew Bragg*, *Dana Kashubeck*, *Kasper Timm Hansen*
4+
15
* Add `caching?` helper that returns whether the current code path is being cached and `uncacheable!` to denote helper methods that can't participate in fragment caching.
26

37
*Ben Toews*, *John Hawthorn*, *Kasper Timm Hansen*, *Joel Hawksley*

actionview/lib/action_view/helpers/form_options_helper.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ def time_zone_select(object, method, priority_zones = nil, options = {}, html_op
297297
Tags::TimeZoneSelect.new(object, method, self, priority_zones, options, html_options).render
298298
end
299299

300+
# Returns select and option tags for the given object and method, using
301+
# #weekday_options_for_select to generate the list of option tags.
302+
def weekday_select(object, method, options = {}, html_options = {}, &block)
303+
Tags::WeekdaySelect.new(object, method, self, options, html_options, &block).render
304+
end
305+
300306
# Accepts a container (hash, array, enumerable, your type) and returns a string of option tags. Given a container
301307
# where the elements respond to first and last (such as a two-element array), the "lasts" serve as option values and
302308
# the "firsts" as option text. Hashes are turned into this form automatically, so the keys become "firsts" and values
@@ -593,6 +599,22 @@ def time_zone_options_for_select(selected = nil, priority_zones = nil, model = :
593599
zone_options.safe_concat options_for_select(convert_zones[zones], selected)
594600
end
595601

602+
# Returns a string of option tags for the days of the week.
603+
#
604+
# Options:
605+
# * <tt>:index_as_value</tt> - Defaults to false, set to true to use the index of the weekday as the value.
606+
# * <tt>:day_format</tt> - The I18n key of the array to use for the weekday options.
607+
# Defaults to :day_names, set to :abbr_day_names for abbreviations.
608+
#
609+
# NOTE: Only the option tags are returned, you have to wrap this call in
610+
# a regular HTML select tag.
611+
def weekday_options_for_select(selected = nil, index_as_value: false, day_format: :day_names)
612+
day_names = I18n.translate("date.#{day_format}")
613+
day_names = day_names.map.with_index.to_h if index_as_value
614+
615+
options_for_select(day_names, selected)
616+
end
617+
596618
# Returns radio button tags for the collection of existing return values
597619
# of +method+ for +object+'s class. The value returned from calling
598620
# +method+ on the instance +object+ will be selected. If calling +method+
@@ -859,6 +881,18 @@ def time_zone_select(method, priority_zones = nil, options = {}, html_options =
859881
@template.time_zone_select(@object_name, method, priority_zones, objectify_options(options), @default_html_options.merge(html_options))
860882
end
861883

884+
# Wraps ActionView::Helpers::FormOptionsHelper#weekday_select for form builders:
885+
#
886+
# <%= form_for @user do |f| %>
887+
# <%= f.weekday_select :weekday, include_blank: true %>
888+
# <%= f.submit %>
889+
# <% end %>
890+
#
891+
# Please refer to the documentation of the base helper for details.
892+
def weekday_select(method, options = {}, html_options = {})
893+
@template.weekday_select(@object_name, method, objectify_options(options), @default_html_options.merge(html_options))
894+
end
895+
862896
# Wraps ActionView::Helpers::FormOptionsHelper#collection_check_boxes for form builders:
863897
#
864898
# <%= form_for @post do |f| %>

actionview/lib/action_view/helpers/tags.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module Tags # :nodoc:
3838
autoload :TimeZoneSelect
3939
autoload :UrlField
4040
autoload :WeekField
41+
autoload :WeekdaySelect
4142
end
4243
end
4344
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
module ActionView
4+
module Helpers
5+
module Tags # :nodoc:
6+
class WeekdaySelect < Base # :nodoc:
7+
def initialize(object_name, method_name, template_object, options, html_options)
8+
@html_options = html_options
9+
10+
super(object_name, method_name, template_object, options)
11+
end
12+
13+
def render
14+
select_content_tag(
15+
weekday_options_for_select(
16+
value || @options[:selected],
17+
index_as_value: @options.fetch(:index_as_value, false),
18+
day_format: @options.fetch(:day_format, :day_names)
19+
),
20+
@options,
21+
@html_options
22+
)
23+
end
24+
end
25+
end
26+
end
27+
end

actionview/test/template/form_options_helper_test.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def secret
3131
Continent = Struct.new("Continent", :continent_name, :countries)
3232
Country = Struct.new("Country", :country_id, :country_name)
3333
Album = Struct.new("Album", :id, :title, :genre)
34+
Digest = Struct.new("Digest", :send_day)
3435
end
3536

3637
class Firm
@@ -1507,6 +1508,75 @@ def test_grouped_collection_select_under_fields_for
15071508
)
15081509
end
15091510

1511+
def test_weekday_options_for_select_with_no_params
1512+
assert_dom_equal(
1513+
"<option value=\"Sunday\">Sunday</option>\n<option value=\"Monday\">Monday</option>\n<option value=\"Tuesday\">Tuesday</option>\n<option value=\"Wednesday\">Wednesday</option>\n<option value=\"Thursday\">Thursday</option>\n<option value=\"Friday\">Friday</option>\n<option value=\"Saturday\">Saturday</option>",
1514+
weekday_options_for_select
1515+
)
1516+
end
1517+
1518+
def test_weekday_options_for_select_with_index_as_value
1519+
assert_dom_equal(
1520+
"<option value=\"0\">Sunday</option>\n<option value=\"1\">Monday</option>\n<option value=\"2\">Tuesday</option>\n<option value=\"3\">Wednesday</option>\n<option value=\"4\">Thursday</option>\n<option value=\"5\">Friday</option>\n<option value=\"6\">Saturday</option>",
1521+
weekday_options_for_select(index_as_value: true)
1522+
)
1523+
end
1524+
1525+
def test_weekday_options_for_select_with_abberviated_day_names
1526+
assert_dom_equal(
1527+
"<option value=\"Sun\">Sun</option>\n<option value=\"Mon\">Mon</option>\n<option value=\"Tue\">Tue</option>\n<option value=\"Wed\">Wed</option>\n<option value=\"Thu\">Thu</option>\n<option value=\"Fri\">Fri</option>\n<option value=\"Sat\">Sat</option>",
1528+
weekday_options_for_select(day_format: :abbr_day_names)
1529+
)
1530+
end
1531+
1532+
def test_weekday_options_for_select_with_selected_value
1533+
assert_dom_equal(
1534+
"<option value=\"Sunday\">Sunday</option>\n<option value=\"Monday\">Monday</option>\n<option value=\"Tuesday\">Tuesday</option>\n<option value=\"Wednesday\">Wednesday</option>\n<option value=\"Thursday\">Thursday</option>\n<option selected=\"selected\" value=\"Friday\">Friday</option>\n<option value=\"Saturday\">Saturday</option>",
1535+
weekday_options_for_select("Friday")
1536+
)
1537+
end
1538+
1539+
def test_weekday_select
1540+
assert_dom_equal(
1541+
"<select name=\"model[weekday]\" id=\"model_weekday\"><option value=\"Sunday\">Sunday</option>\n<option value=\"Monday\">Monday</option>\n<option value=\"Tuesday\">Tuesday</option>\n<option value=\"Wednesday\">Wednesday</option>\n<option value=\"Thursday\">Thursday</option>\n<option value=\"Friday\">Friday</option>\n<option value=\"Saturday\">Saturday</option></select>",
1542+
weekday_select(:model, :weekday)
1543+
)
1544+
end
1545+
1546+
def test_weekday_select_with_selected_value
1547+
assert_dom_equal(
1548+
"<select name=\"model[weekday]\" id=\"model_weekday\"><option value=\"Sunday\">Sunday</option>\n<option value=\"Monday\">Monday</option>\n<option value=\"Tuesday\">Tuesday</option>\n<option value=\"Wednesday\">Wednesday</option>\n<option value=\"Thursday\">Thursday</option>\n<option selected=\"selected\" value=\"Friday\">Friday</option>\n<option value=\"Saturday\">Saturday</option></select>",
1549+
weekday_select(:model, :weekday, selected: "Friday")
1550+
)
1551+
end
1552+
1553+
def test_weekday_select_under_fields_for
1554+
@digest = Digest.new
1555+
1556+
output_buffer = fields_for :digest, @digest do |f|
1557+
concat f.weekday_select(:send_day)
1558+
end
1559+
1560+
assert_dom_equal(
1561+
"<select id=\"digest_send_day\" name=\"digest[send_day]\"><option value=\"Sunday\">Sunday</option>\n<option value=\"Monday\">Monday</option>\n<option value=\"Tuesday\">Tuesday</option>\n<option value=\"Wednesday\">Wednesday</option>\n<option value=\"Thursday\">Thursday</option>\n<option value=\"Friday\">Friday</option>\n<option value=\"Saturday\">Saturday</option></select>",
1562+
output_buffer
1563+
)
1564+
end
1565+
1566+
def test_weekday_select_under_fields_for_with_value
1567+
@digest = Digest.new
1568+
@digest.send_day = "Monday"
1569+
1570+
output_buffer = fields_for :digest, @digest do |f|
1571+
concat f.weekday_select(:send_day)
1572+
end
1573+
1574+
assert_dom_equal(
1575+
"<select name=\"digest[send_day]\" id=\"digest_send_day\"><option value=\"Sunday\">Sunday</option>\n<option selected=\"selected\" value=\"Monday\">Monday</option>\n<option value=\"Tuesday\">Tuesday</option>\n<option value=\"Wednesday\">Wednesday</option>\n<option value=\"Thursday\">Thursday</option>\n<option value=\"Friday\">Friday</option>\n<option value=\"Saturday\">Saturday</option></select>",
1576+
output_buffer
1577+
)
1578+
end
1579+
15101580
private
15111581
def dummy_posts
15121582
[ Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),

0 commit comments

Comments
 (0)