Skip to content

Commit 60dabb1

Browse files
Christophpixeltrix
authored andcommitted
Fix week_field returning invalid value
According to the W3 spec[1] the value should use a 1-based index and not a 0-based index for the week number. [1]: http://www.w3.org/TR/html-markup/datatypes.html#form.data.week
1 parent 8d7f0a6 commit 60dabb1

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

actionview/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Generate `week_field` input values using a 1-based index and not a 0-based index
2+
as per the W3 spec: http://www.w3.org/TR/html-markup/datatypes.html#form.data.week
3+
4+
*Christoph Geschwind*
5+
16
* Allow `host` option in `javascript_include_tag` and `stylesheet_link_tag` helpers
27

38
*Grzegorz Witek*

actionview/lib/action_view/helpers/tags/week_field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class WeekField < DatetimeField # :nodoc:
55
private
66

77
def format_date(value)
8-
value.try(:strftime, "%Y-W%W")
8+
value.try(:strftime, "%Y-W%V")
99
end
1010
end
1111
end

actionview/test/template/form_helper_test.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ def test_month_field_with_timewithzone_value
12811281
end
12821282

12831283
def test_week_field
1284-
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W24" />}
1284+
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W25" />}
12851285
assert_dom_equal(expected, week_field("post", "written_on"))
12861286
end
12871287

@@ -1292,13 +1292,13 @@ def test_week_field_with_nil_value
12921292
end
12931293

12941294
def test_week_field_with_datetime_value
1295-
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W24" />}
1295+
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W25" />}
12961296
@post.written_on = DateTime.new(2004, 6, 15, 1, 2, 3)
12971297
assert_dom_equal(expected, week_field("post", "written_on"))
12981298
end
12991299

13001300
def test_week_field_with_extra_attrs
1301-
expected = %{<input id="post_written_on" step="2" max="2010-W51" min="2000-W06" name="post[written_on]" type="week" value="2004-W24" />}
1301+
expected = %{<input id="post_written_on" step="2" max="2010-W51" min="2000-W06" name="post[written_on]" type="week" value="2004-W25" />}
13021302
@post.written_on = DateTime.new(2004, 6, 15, 1, 2, 3)
13031303
min_value = DateTime.new(2000, 2, 13)
13041304
max_value = DateTime.new(2010, 12, 23)
@@ -1308,13 +1308,19 @@ def test_week_field_with_extra_attrs
13081308

13091309
def test_week_field_with_timewithzone_value
13101310
previous_time_zone, Time.zone = Time.zone, 'UTC'
1311-
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W24" />}
1311+
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W25" />}
13121312
@post.written_on = Time.zone.parse('2004-06-15 15:30:45')
13131313
assert_dom_equal(expected, week_field("post", "written_on"))
13141314
ensure
13151315
Time.zone = previous_time_zone
13161316
end
13171317

1318+
def test_week_field_week_number_base
1319+
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2015-W01" />}
1320+
@post.written_on = DateTime.new(2015, 1, 1, 1, 2, 3)
1321+
assert_dom_equal(expected, week_field("post", "written_on"))
1322+
end
1323+
13181324
def test_url_field
13191325
expected = %{<input id="user_homepage" name="user[homepage]" type="url" />}
13201326
assert_dom_equal(expected, url_field("user", "homepage"))

0 commit comments

Comments
 (0)