Skip to content

Commit 4696e0d

Browse files
Christophpixeltrix
Christoph
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 (cherry picked from commit 60dabb1)
1 parent 3c02257 commit 4696e0d

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

actionview/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
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+
6+
17
## Rails 4.1.14.rc1 (October 30, 2015) ##
28

39
* No changes.

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
@@ -912,7 +912,7 @@ def test_month_field_with_timewithzone_value
912912
end
913913

914914
def test_week_field
915-
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W24" />}
915+
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W25" />}
916916
assert_dom_equal(expected, week_field("post", "written_on"))
917917
end
918918

@@ -923,13 +923,13 @@ def test_week_field_with_nil_value
923923
end
924924

925925
def test_week_field_with_datetime_value
926-
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W24" />}
926+
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W25" />}
927927
@post.written_on = DateTime.new(2004, 6, 15, 1, 2, 3)
928928
assert_dom_equal(expected, week_field("post", "written_on"))
929929
end
930930

931931
def test_week_field_with_extra_attrs
932-
expected = %{<input id="post_written_on" step="2" max="2010-W51" min="2000-W06" name="post[written_on]" type="week" value="2004-W24" />}
932+
expected = %{<input id="post_written_on" step="2" max="2010-W51" min="2000-W06" name="post[written_on]" type="week" value="2004-W25" />}
933933
@post.written_on = DateTime.new(2004, 6, 15, 1, 2, 3)
934934
min_value = DateTime.new(2000, 2, 13)
935935
max_value = DateTime.new(2010, 12, 23)
@@ -939,13 +939,19 @@ def test_week_field_with_extra_attrs
939939

940940
def test_week_field_with_timewithzone_value
941941
previous_time_zone, Time.zone = Time.zone, 'UTC'
942-
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W24" />}
942+
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2004-W25" />}
943943
@post.written_on = Time.zone.parse('2004-06-15 15:30:45')
944944
assert_dom_equal(expected, week_field("post", "written_on"))
945945
ensure
946946
Time.zone = previous_time_zone
947947
end
948948

949+
def test_week_field_week_number_base
950+
expected = %{<input id="post_written_on" name="post[written_on]" type="week" value="2015-W01" />}
951+
@post.written_on = DateTime.new(2015, 1, 1, 1, 2, 3)
952+
assert_dom_equal(expected, week_field("post", "written_on"))
953+
end
954+
949955
def test_url_field
950956
expected = %{<input id="user_homepage" name="user[homepage]" type="url" />}
951957
assert_dom_equal(expected, url_field("user", "homepage"))

0 commit comments

Comments
 (0)