require "abstract_unit" class DateHelperTest < ActionView::TestCase tests ActionView::Helpers::DateHelper silence_warnings do Post = Struct.new("Post", :id, :written_on, :updated_at) Post.class_eval do def id 123 end def id_before_type_cast 123 end def to_param "123" end end end def assert_distance_of_time_in_words(from, to=nil) to ||= from # 0..1 minute with :include_seconds => true assert_equal "less than 5 seconds", distance_of_time_in_words(from, to + 0.seconds, include_seconds: true) assert_equal "less than 5 seconds", distance_of_time_in_words(from, to + 4.seconds, include_seconds: true) assert_equal "less than 10 seconds", distance_of_time_in_words(from, to + 5.seconds, include_seconds: true) assert_equal "less than 10 seconds", distance_of_time_in_words(from, to + 9.seconds, include_seconds: true) assert_equal "less than 20 seconds", distance_of_time_in_words(from, to + 10.seconds, include_seconds: true) assert_equal "less than 20 seconds", distance_of_time_in_words(from, to + 19.seconds, include_seconds: true) assert_equal "half a minute", distance_of_time_in_words(from, to + 20.seconds, include_seconds: true) assert_equal "half a minute", distance_of_time_in_words(from, to + 39.seconds, include_seconds: true) assert_equal "less than a minute", distance_of_time_in_words(from, to + 40.seconds, include_seconds: true) assert_equal "less than a minute", distance_of_time_in_words(from, to + 59.seconds, include_seconds: true) assert_equal "1 minute", distance_of_time_in_words(from, to + 60.seconds, include_seconds: true) assert_equal "1 minute", distance_of_time_in_words(from, to + 89.seconds, include_seconds: true) # 0..1 minute with :include_seconds => false assert_equal "less than a minute", distance_of_time_in_words(from, to + 0.seconds, include_seconds: false) assert_equal "less than a minute", distance_of_time_in_words(from, to + 4.seconds, include_seconds: false) assert_equal "less than a minute", distance_of_time_in_words(from, to + 5.seconds, include_seconds: false) assert_equal "less than a minute", distance_of_time_in_words(from, to + 9.seconds, include_seconds: false) assert_equal "less than a minute", distance_of_time_in_words(from, to + 10.seconds, include_seconds: false) assert_equal "less than a minute", distance_of_time_in_words(from, to + 19.seconds, include_seconds: false) assert_equal "less than a minute", distance_of_time_in_words(from, to + 20.seconds, include_seconds: false) assert_equal "1 minute", distance_of_time_in_words(from, to + 39.seconds, include_seconds: false) assert_equal "1 minute", distance_of_time_in_words(from, to + 40.seconds, include_seconds: false) assert_equal "1 minute", distance_of_time_in_words(from, to + 59.seconds, include_seconds: false) assert_equal "1 minute", distance_of_time_in_words(from, to + 60.seconds, include_seconds: false) assert_equal "1 minute", distance_of_time_in_words(from, to + 89.seconds, include_seconds: false) # Note that we are including a 30-second boundary around the interval we # want to test. For instance, "1 minute" is actually 30s to 1m29s. The # reason for doing this is simple -- in `distance_of_time_to_words`, when we # take the distance between our two Time objects in seconds and convert it # to minutes, we round the number. So 29s gets rounded down to 0m, 30s gets # rounded up to 1m, and 1m29s gets rounded down to 1m. A similar thing # happens with the other cases. # First case 0..1 minute assert_equal "less than a minute", distance_of_time_in_words(from, to + 0.seconds) assert_equal "less than a minute", distance_of_time_in_words(from, to + 29.seconds) assert_equal "1 minute", distance_of_time_in_words(from, to + 30.seconds) assert_equal "1 minute", distance_of_time_in_words(from, to + 1.minutes + 29.seconds) # 2 minutes up to 45 minutes assert_equal "2 minutes", distance_of_time_in_words(from, to + 1.minutes + 30.seconds) assert_equal "44 minutes", distance_of_time_in_words(from, to + 44.minutes + 29.seconds) # 45 minutes up to 90 minutes assert_equal "about 1 hour", distance_of_time_in_words(from, to + 44.minutes + 30.seconds) assert_equal "about 1 hour", distance_of_time_in_words(from, to + 89.minutes + 29.seconds) # 90 minutes up to 24 hours assert_equal "about 2 hours", distance_of_time_in_words(from, to + 89.minutes + 30.seconds) assert_equal "about 24 hours", distance_of_time_in_words(from, to + 23.hours + 59.minutes + 29.seconds) # 24 hours up to 42 hours assert_equal "1 day", distance_of_time_in_words(from, to + 23.hours + 59.minutes + 30.seconds) assert_equal "1 day", distance_of_time_in_words(from, to + 41.hours + 59.minutes + 29.seconds) # 42 hours up to 30 days assert_equal "2 days", distance_of_time_in_words(from, to + 41.hours + 59.minutes + 30.seconds) assert_equal "3 days", distance_of_time_in_words(from, to + 2.days + 12.hours) assert_equal "30 days", distance_of_time_in_words(from, to + 29.days + 23.hours + 59.minutes + 29.seconds) # 30 days up to 60 days assert_equal "about 1 month", distance_of_time_in_words(from, to + 29.days + 23.hours + 59.minutes + 30.seconds) assert_equal "about 1 month", distance_of_time_in_words(from, to + 44.days + 23.hours + 59.minutes + 29.seconds) assert_equal "about 2 months", distance_of_time_in_words(from, to + 44.days + 23.hours + 59.minutes + 30.seconds) assert_equal "about 2 months", distance_of_time_in_words(from, to + 59.days + 23.hours + 59.minutes + 29.seconds) # 60 days up to 365 days assert_equal "2 months", distance_of_time_in_words(from, to + 59.days + 23.hours + 59.minutes + 30.seconds) assert_equal "12 months", distance_of_time_in_words(from, to + 1.years - 31.seconds) # >= 365 days assert_equal "about 1 year", distance_of_time_in_words(from, to + 1.years - 30.seconds) assert_equal "about 1 year", distance_of_time_in_words(from, to + 1.years + 3.months - 1.day) assert_equal "over 1 year", distance_of_time_in_words(from, to + 1.years + 6.months) assert_equal "almost 2 years", distance_of_time_in_words(from, to + 2.years - 3.months + 1.day) assert_equal "about 2 years", distance_of_time_in_words(from, to + 2.years + 3.months - 1.day) assert_equal "over 2 years", distance_of_time_in_words(from, to + 2.years + 3.months + 1.day) assert_equal "over 2 years", distance_of_time_in_words(from, to + 2.years + 9.months - 1.day) assert_equal "almost 3 years", distance_of_time_in_words(from, to + 2.years + 9.months + 1.day) assert_equal "almost 5 years", distance_of_time_in_words(from, to + 5.years - 3.months + 1.day) assert_equal "about 5 years", distance_of_time_in_words(from, to + 5.years + 3.months - 1.day) assert_equal "over 5 years", distance_of_time_in_words(from, to + 5.years + 3.months + 1.day) assert_equal "over 5 years", distance_of_time_in_words(from, to + 5.years + 9.months - 1.day) assert_equal "almost 6 years", distance_of_time_in_words(from, to + 5.years + 9.months + 1.day) assert_equal "almost 10 years", distance_of_time_in_words(from, to + 10.years - 3.months + 1.day) assert_equal "about 10 years", distance_of_time_in_words(from, to + 10.years + 3.months - 1.day) assert_equal "over 10 years", distance_of_time_in_words(from, to + 10.years + 3.months + 1.day) assert_equal "over 10 years", distance_of_time_in_words(from, to + 10.years + 9.months - 1.day) assert_equal "almost 11 years", distance_of_time_in_words(from, to + 10.years + 9.months + 1.day) # test to < from assert_equal "about 4 hours", distance_of_time_in_words(from + 4.hours, to) assert_equal "less than 20 seconds", distance_of_time_in_words(from + 19.seconds, to, include_seconds: true) assert_equal "less than a minute", distance_of_time_in_words(from + 19.seconds, to, include_seconds: false) end def test_distance_in_words from = Time.utc(2004, 6, 6, 21, 45, 0) assert_distance_of_time_in_words(from) end def test_distance_in_words_with_mathn_required # test we avoid Integer#/ (redefined by mathn) silence_warnings { require "mathn" } from = Time.utc(2004, 6, 6, 21, 45, 0) assert_distance_of_time_in_words(from) end def test_time_ago_in_words_passes_include_seconds assert_equal "less than 20 seconds", time_ago_in_words(15.seconds.ago, include_seconds: true) assert_equal "less than a minute", time_ago_in_words(15.seconds.ago, include_seconds: false) end def test_distance_in_words_with_time_zones from = Time.mktime(2004, 6, 6, 21, 45, 0) assert_distance_of_time_in_words(from.in_time_zone("Alaska")) assert_distance_of_time_in_words(from.in_time_zone("Hawaii")) end def test_distance_in_words_with_different_time_zones from = Time.mktime(2004, 6, 6, 21, 45, 0) assert_distance_of_time_in_words( from.in_time_zone("Alaska"), from.in_time_zone("Hawaii") ) end def test_distance_in_words_with_dates start_date = Date.new 1975, 1, 31 end_date = Date.new 1977, 1, 31 assert_equal("about 2 years", distance_of_time_in_words(start_date, end_date)) start_date = Date.new 1982, 12, 3 end_date = Date.new 2010, 11, 30 assert_equal("almost 28 years", distance_of_time_in_words(start_date, end_date)) assert_equal("almost 28 years", distance_of_time_in_words(end_date, start_date)) end def test_distance_in_words_with_integers assert_equal "1 minute", distance_of_time_in_words(59) assert_equal "about 1 hour", distance_of_time_in_words(60*60) assert_equal "1 minute", distance_of_time_in_words(0, 59) assert_equal "about 1 hour", distance_of_time_in_words(60*60, 0) assert_equal "about 3 years", distance_of_time_in_words(10**8) assert_equal "about 3 years", distance_of_time_in_words(0, 10**8) end def test_distance_in_words_with_times assert_equal "1 minute", distance_of_time_in_words(30.seconds) assert_equal "1 minute", distance_of_time_in_words(59.seconds) assert_equal "2 minutes", distance_of_time_in_words(119.seconds) assert_equal "2 minutes", distance_of_time_in_words(1.minute + 59.seconds) assert_equal "3 minutes", distance_of_time_in_words(2.minute + 30.seconds) assert_equal "44 minutes", distance_of_time_in_words(44.minutes + 29.seconds) assert_equal "about 1 hour", distance_of_time_in_words(44.minutes + 30.seconds) assert_equal "about 1 hour", distance_of_time_in_words(60.minutes) # include seconds assert_equal "half a minute", distance_of_time_in_words(39.seconds, 0, include_seconds: true) assert_equal "less than a minute", distance_of_time_in_words(40.seconds, 0, include_seconds: true) assert_equal "less than a minute", distance_of_time_in_words(59.seconds, 0, include_seconds: true) assert_equal "1 minute", distance_of_time_in_words(60.seconds, 0, include_seconds: true) end def test_time_ago_in_words assert_equal "about 1 year", time_ago_in_words(1.year.ago - 1.day) end def test_select_day expected = %(\n" assert_dom_equal expected, select_day(Time.mktime(2003, 8, 16)) assert_dom_equal expected, select_day(16) end def test_select_day_with_blank expected = %(\n" assert_dom_equal expected, select_day(Time.mktime(2003, 8, 16), include_blank: true) assert_dom_equal expected, select_day(16, include_blank: true) end def test_select_day_nil_with_blank expected = %(\n" assert_dom_equal expected, select_day(nil, include_blank: true) end def test_select_day_with_two_digit_numbers expected = %(\n" assert_dom_equal expected, select_day(Time.mktime(2011, 8, 2), use_two_digit_numbers: true) assert_dom_equal expected, select_day(2, use_two_digit_numbers: true) end def test_select_day_with_html_options expected = %(\n" assert_dom_equal expected, select_day(Time.mktime(2003, 8, 16), {}, class: "selector") assert_dom_equal expected, select_day(16, {}, class: "selector") end def test_select_day_with_default_prompt expected = %(\n" assert_dom_equal expected, select_day(16, prompt: true) end def test_select_day_with_custom_prompt expected = %(\n" assert_dom_equal expected, select_day(16, prompt: "Choose day") end def test_select_day_with_generic_with_css_classes expected = %(\n" assert_dom_equal expected, select_day(16, with_css_classes: true) end def test_select_day_with_custom_with_css_classes expected = %(\n" assert_dom_equal expected, select_day(16, with_css_classes: { day: "my-day" }) end def test_select_month expected = %(\n" assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16)) assert_dom_equal expected, select_month(8) end def test_select_month_with_two_digit_numbers expected = %(\n" assert_dom_equal expected, select_month(Time.mktime(2011, 8, 16), use_two_digit_numbers: true) assert_dom_equal expected, select_month(8, use_two_digit_numbers: true) end def test_select_month_with_disabled expected = %(\n" assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), disabled: true) assert_dom_equal expected, select_month(8, disabled: true) end def test_select_month_with_field_name_override expected = %(\n" assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), field_name: "mois") assert_dom_equal expected, select_month(8, field_name: "mois") end def test_select_month_with_blank expected = %(\n" assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), include_blank: true) assert_dom_equal expected, select_month(8, include_blank: true) end def test_select_month_nil_with_blank expected = %(\n" assert_dom_equal expected, select_month(nil, include_blank: true) end def test_select_month_with_numbers expected = %(\n" assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), use_month_numbers: true) assert_dom_equal expected, select_month(8, use_month_numbers: true) end def test_select_month_with_numbers_and_names expected = %(\n" assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), add_month_numbers: true) assert_dom_equal expected, select_month(8, add_month_numbers: true) end def test_select_month_with_format_string expected = %(\n" format_string = "%{name} (%02d)" assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), month_format_string: format_string) assert_dom_equal expected, select_month(8, month_format_string: format_string) end def test_select_month_with_numbers_and_names_with_abbv expected = %(\n" assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), add_month_numbers: true, use_short_month: true) assert_dom_equal expected, select_month(8, add_month_numbers: true, use_short_month: true) end def test_select_month_with_abbv expected = %(\n" assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), use_short_month: true) assert_dom_equal expected, select_month(8, use_short_month: true) end def test_select_month_with_custom_names month_names = %w(nil Januar Februar Marts April Maj Juni Juli August September Oktober November December) expected = %(\n" assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), use_month_names: month_names) assert_dom_equal expected, select_month(8, use_month_names: month_names) end def test_select_month_with_zero_indexed_custom_names month_names = %w(Januar Februar Marts April Maj Juni Juli August September Oktober November December) expected = %(\n" assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), use_month_names: month_names) assert_dom_equal expected, select_month(8, use_month_names: month_names) end def test_select_month_with_hidden assert_dom_equal "\n", select_month(8, use_hidden: true) end def test_select_month_with_hidden_and_field_name assert_dom_equal "\n", select_month(8, use_hidden: true, field_name: "mois") end def test_select_month_with_html_options expected = %(\n" assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), {}, class: "selector", accesskey: "M") end def test_select_month_with_default_prompt expected = %(\n" assert_dom_equal expected, select_month(8, prompt: true) end def test_select_month_with_custom_prompt expected = %(\n" assert_dom_equal expected, select_month(8, prompt: "Choose month") end def test_select_month_with_generic_with_css_classes expected = %(\n" assert_dom_equal expected, select_month(8, with_css_classes: true) end def test_select_month_with_custom_with_css_classes expected = %(\n" assert_dom_equal expected, select_month(8, with_css_classes: { month: "my-month" }) end def test_select_year expected = %(\n" assert_dom_equal expected, select_year(Time.mktime(2003, 8, 16), start_year: 2003, end_year: 2005) assert_dom_equal expected, select_year(2003, start_year: 2003, end_year: 2005) end def test_select_year_with_disabled expected = %(\n" assert_dom_equal expected, select_year(Time.mktime(2003, 8, 16), disabled: true, start_year: 2003, end_year: 2005) assert_dom_equal expected, select_year(2003, disabled: true, start_year: 2003, end_year: 2005) end def test_select_year_with_field_name_override expected = %(\n" assert_dom_equal expected, select_year(Time.mktime(2003, 8, 16), start_year: 2003, end_year: 2005, field_name: "annee") assert_dom_equal expected, select_year(2003, start_year: 2003, end_year: 2005, field_name: "annee") end def test_select_year_with_type_discarding expected = %(\n" assert_dom_equal expected, select_year( Time.mktime(2003, 8, 16), prefix: "date_year", discard_type: true, start_year: 2003, end_year: 2005) assert_dom_equal expected, select_year( 2003, prefix: "date_year", discard_type: true, start_year: 2003, end_year: 2005) end def test_select_year_descending expected = %(\n" assert_dom_equal expected, select_year(Time.mktime(2005, 8, 16), start_year: 2005, end_year: 2003) assert_dom_equal expected, select_year(2005, start_year: 2005, end_year: 2003) end def test_select_year_with_hidden assert_dom_equal "\n", select_year(2007, use_hidden: true) end def test_select_year_with_hidden_and_field_name assert_dom_equal "\n", select_year(2007, use_hidden: true, field_name: "anno") end def test_select_year_with_html_options expected = %(\n" assert_dom_equal expected, select_year(Time.mktime(2003, 8, 16), { start_year: 2003, end_year: 2005 }, class: "selector", accesskey: "M") end def test_select_year_with_default_prompt expected = %(\n" assert_dom_equal expected, select_year(nil, start_year: 2003, end_year: 2005, prompt: true) end def test_select_year_with_custom_prompt expected = %(\n" assert_dom_equal expected, select_year(nil, start_year: 2003, end_year: 2005, prompt: "Choose year") end def test_select_year_with_generic_with_css_classes expected = %(\n" assert_dom_equal expected, select_year(nil, start_year: 2003, end_year: 2005, with_css_classes: true) end def test_select_year_with_custom_with_css_classes expected = %(\n" assert_dom_equal expected, select_year(nil, start_year: 2003, end_year: 2005, with_css_classes: { year: "my-year" }) end def test_select_year_with_position expected = %(\n" assert_dom_equal expected, select_year(Date.current, include_position: true, start_year: 2003, end_year: 2005) end def test_select_hour expected = %(\n" assert_dom_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18)) end def test_select_hour_with_ampm expected = %(\n" assert_dom_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18), ampm: true) end def test_select_hour_with_disabled expected = %(\n" assert_dom_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18), disabled: true) end def test_select_hour_with_field_name_override expected = %(\n" assert_dom_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18), field_name: "heure") end def test_select_hour_with_blank expected = %(\n" assert_dom_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18), include_blank: true) end def test_select_hour_nil_with_blank expected = %(\n" assert_dom_equal expected, select_hour(nil, include_blank: true) end def test_select_hour_with_html_options expected = %(\n" assert_dom_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18), {}, class: "selector", accesskey: "M") end def test_select_hour_with_default_prompt expected = %(\n" assert_dom_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18), prompt: true) end def test_select_hour_with_custom_prompt expected = %(\n" assert_dom_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18), prompt: "Choose hour") end def test_select_hour_with_generic_with_css_classes expected = %(\n" assert_dom_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18), with_css_classes: true) end def test_select_hour_with_custom_with_css_classes expected = %(\n" assert_dom_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18), with_css_classes: { hour: "my-hour" }) end def test_select_minute expected = %(\n" assert_dom_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18)) end def test_select_minute_with_disabled expected = %(\n" assert_dom_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18), disabled: true) end def test_select_minute_with_field_name_override expected = %(\n" assert_dom_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18), field_name: "minuto") end def test_select_minute_with_blank expected = %(\n" assert_dom_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18), include_blank: true) end def test_select_minute_with_blank_and_step expected = %(\n" assert_dom_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18), include_blank: true , minute_step: 15) end def test_select_minute_nil_with_blank expected = %(\n" assert_dom_equal expected, select_minute(nil, include_blank: true) end def test_select_minute_nil_with_blank_and_step expected = %(\n" assert_dom_equal expected, select_minute(nil, include_blank: true , minute_step: 15) end def test_select_minute_with_hidden assert_dom_equal "\n", select_minute(8, use_hidden: true) end def test_select_minute_with_hidden_and_field_name assert_dom_equal "\n", select_minute(8, use_hidden: true, field_name: "minuto") end def test_select_minute_with_html_options expected = %(\n" assert_dom_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18), {}, class: "selector", accesskey: "M") end def test_select_minute_with_default_prompt expected = %(\n" assert_dom_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18), prompt: true) end def test_select_minute_with_custom_prompt expected = %(\n" assert_dom_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18), prompt: "Choose minute") end def test_select_minute_with_generic_with_css_classes expected = %(\n" assert_dom_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18), with_css_classes: true) end def test_select_minute_with_custom_with_css_classes expected = %(\n" assert_dom_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18), with_css_classes: { minute: "my-minute" }) end def test_select_second expected = %(\n" assert_dom_equal expected, select_second(Time.mktime(2003, 8, 16, 8, 4, 18)) end def test_select_second_with_disabled expected = %(\n" assert_dom_equal expected, select_second(Time.mktime(2003, 8, 16, 8, 4, 18), disabled: true) end def test_select_second_with_field_name_override expected = %(\n" assert_dom_equal expected, select_second(Time.mktime(2003, 8, 16, 8, 4, 18), field_name: "segundo") end def test_select_second_with_blank expected = %(\n" assert_dom_equal expected, select_second(Time.mktime(2003, 8, 16, 8, 4, 18), include_blank: true) end def test_select_second_nil_with_blank expected = %(\n" assert_dom_equal expected, select_second(nil, include_blank: true) end def test_select_second_with_html_options expected = %(\n" assert_dom_equal expected, select_second(Time.mktime(2003, 8, 16, 8, 4, 18), {}, class: "selector", accesskey: "M") end def test_select_second_with_default_prompt expected = %(\n" assert_dom_equal expected, select_second(Time.mktime(2003, 8, 16, 8, 4, 18), prompt: true) end def test_select_second_with_custom_prompt expected = %(\n" assert_dom_equal expected, select_second(Time.mktime(2003, 8, 16, 8, 4, 18), prompt: "Choose seconds") end def test_select_second_with_generic_with_css_classes expected = %(\n" assert_dom_equal expected, select_second(Time.mktime(2003, 8, 16, 8, 4, 18), with_css_classes: true) end def test_select_second_with_custom_with_css_classes expected = %(\n" assert_dom_equal expected, select_second(Time.mktime(2003, 8, 16, 8, 4, 18), with_css_classes: { second: "my-second" }) end def test_select_date expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), start_year: 2003, end_year: 2005, prefix: "date[first]") end def test_select_date_with_too_big_range_between_start_year_and_end_year assert_raise(ArgumentError) { select_date(Time.mktime(2003, 8, 16), start_year: 2000, end_year: 20000, prefix: "date[first]", order: [:month, :day, :year]) } assert_raise(ArgumentError) { select_date(Time.mktime(2003, 8, 16), start_year: Date.today.year - 100.years, end_year: 2000, prefix: "date[first]", order: [:month, :day, :year]) } end def test_select_date_can_have_more_then_1000_years_interval_if_forced_via_parameter assert_nothing_raised { select_date(Time.mktime(2003, 8, 16), start_year: 2000, end_year: 3100, max_years_allowed: 2000) } end def test_select_date_with_order expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), start_year: 2003, end_year: 2005, prefix: "date[first]", order: [:month, :day, :year]) end def test_select_date_with_incomplete_order # Since the order is incomplete nothing will be shown expected = %(\n) expected << %(\n) expected << %(\n) assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), start_year: 2003, end_year: 2005, prefix: "date[first]", order: [:day]) end def test_select_date_with_disabled expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), start_year: 2003, end_year: 2005, prefix: "date[first]", disabled: true) end def test_select_date_with_no_start_year expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date( Time.mktime(Date.today.year, 8, 16), end_year: Date.today.year+1, prefix: "date[first]" ) end def test_select_date_with_no_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date( Time.mktime(2003, 8, 16), start_year: 2003, prefix: "date[first]" ) end def test_select_date_with_no_start_or_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date( Time.mktime(Date.today.year, 8, 16), prefix: "date[first]" ) end def test_select_date_with_zero_value expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(0, start_year: 2003, end_year: 2005, prefix: "date[first]") end def test_select_date_with_zero_value_and_no_start_year expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(0, end_year: Date.today.year+1, prefix: "date[first]") end def test_select_date_with_zero_value_and_no_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(0, start_year: 2003, prefix: "date[first]") end def test_select_date_with_zero_value_and_no_start_and_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(0, prefix: "date[first]") end def test_select_date_with_nil_value_and_no_start_and_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(nil, prefix: "date[first]") end def test_select_date_with_html_options expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), { start_year: 2003, end_year: 2005, prefix: "date[first]" }, class: "selector") end def test_select_date_with_separator expected = %(\n" expected << " / " expected << %(\n" expected << " / " expected << %(\n" assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), date_separator: " / ", start_year: 2003, end_year: 2005, prefix: "date[first]") end def test_select_date_with_separator_and_discard_day expected = %(\n" expected << " / " expected << %(\n" expected << %(\n) assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), date_separator: " / ", discard_day: true, start_year: 2003, end_year: 2005, prefix: "date[first]") end def test_select_date_with_separator_discard_month_and_day expected = %(\n" expected << %(\n) expected << %(\n) assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), date_separator: " / ", discard_month: true, discard_day: true, start_year: 2003, end_year: 2005, prefix: "date[first]") end def test_select_date_with_hidden expected = %(\n) expected << %(\n) expected << %(\n) assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), prefix: "date[first]", use_hidden: true) assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), date_separator: " / ", prefix: "date[first]", use_hidden: true) end def test_select_date_with_css_classes_option expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), start_year: 2003, end_year: 2005, prefix: "date[first]", with_css_classes: true) end def test_select_date_with_custom_with_css_classes expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), start_year: 2003, end_year: 2005, with_css_classes: { year: "my-year", month: "my-month", day: "my-day" }) end def test_select_date_with_css_classes_option_and_html_class_option expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), { start_year: 2003, end_year: 2005, prefix: "date[first]", with_css_classes: true }, class: "datetime optional") end def test_select_date_with_custom_with_css_classes_and_html_class_option expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), { start_year: 2003, end_year: 2005, with_css_classes: { year: "my-year", month: "my-month", day: "my-day" } }, class: "date optional") end def test_select_date_with_partial_with_css_classes_and_html_class_option expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), { start_year: 2003, end_year: 2005, with_css_classes: { month: "my-month custom-grid" } }, class: "date optional") end def test_select_date_with_html_class_option expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), { start_year: 2003, end_year: 2005 }, class: "date optional custom-grid") end def test_select_datetime expected = %(\n" expected << %(\n" expected << %(\n" expected << " — " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), start_year: 2003, end_year: 2005, prefix: "date[first]") end def test_select_datetime_with_ampm expected = %(\n" expected << %(\n" expected << %(\n" expected << " — " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), start_year: 2003, end_year: 2005, prefix: "date[first]", ampm: true) end def test_select_datetime_with_separators expected = %(\n" expected << %(\n" expected << %(\n" expected << " — " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), start_year: 2003, end_year: 2005, prefix: "date[first]", datetime_separator: " — ", time_separator: " : ") end def test_select_datetime_with_nil_value_and_no_start_and_end_year expected = %(\n" expected << %(\n" expected << %(\n" expected << " — " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_datetime(nil, prefix: "date[first]") end def test_select_datetime_with_html_options expected = %(\n" expected << %(\n" expected << %(\n" expected << " — " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), { start_year: 2003, end_year: 2005, prefix: "date[first]" }, class: "selector") end def test_select_datetime_with_all_separators expected = %(\n" expected << "/" expected << %(\n" expected << "/" expected << %(\n" expected << "—" expected << %(\n" expected << ":" expected << %(\n" assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), { datetime_separator: "—", date_separator: "/", time_separator: ":", start_year: 2003, end_year: 2005, prefix: "date[first]" }, class: "selector") end def test_select_datetime_should_work_with_date assert_nothing_raised { select_datetime(Date.today) } end def test_select_datetime_with_default_prompt expected = %(\n" expected << %(\n" expected << %(\n" expected << " — " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), start_year: 2003, end_year: 2005, prefix: "date[first]", prompt: true) end def test_select_datetime_with_custom_prompt expected = %(\n" expected << %(\n" expected << %(\n" expected << " — " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), start_year: 2003, end_year: 2005, prefix: "date[first]", prompt: { day: "Choose day", month: "Choose month", year: "Choose year", hour: "Choose hour", minute: "Choose minute" }) end def test_select_datetime_with_generic_with_css_classes expected = %(\n" expected << %(\n" expected << %(\n" expected << " — " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), start_year: 2003, end_year: 2005, with_css_classes: true) end def test_select_datetime_with_custom_with_css_classes expected = %(\n" expected << %(\n" expected << %(\n" expected << " — " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), start_year: 2003, end_year: 2005, with_css_classes: { day: "my-day", month: "my-month", year: "my-year", hour: "my-hour", minute: "my-minute" }) end def test_select_datetime_with_custom_hours expected = %(\n" expected << %(\n" expected << %(\n" expected << " — " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), start_year: 2003, end_year: 2005, start_hour: 1, end_hour: 9, prefix: "date[first]", prompt: { day: "Choose day", month: "Choose month", year: "Choose year", hour: "Choose hour", minute: "Choose minute" }) end def test_select_datetime_with_hidden expected = %(\n) expected << %(\n) expected << %(\n) expected << %(\n) expected << %(\n) assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), prefix: "date[first]", use_hidden: true) assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), datetime_separator: "—", date_separator: "/", time_separator: ":", prefix: "date[first]", use_hidden: true) end def test_select_time expected = %(\n) expected << %(\n) expected << %(\n) expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18)) assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), include_seconds: false) end def test_select_time_with_ampm expected = %(\n) expected << %(\n) expected << %(\n) expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), include_seconds: false, ampm: true) end def test_select_time_with_separator expected = %(\n) expected << %(\n) expected << %(\n) expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), time_separator: " : ") assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), time_separator: " : ", include_seconds: false) end def test_select_time_with_seconds expected = %(\n) expected << %(\n) expected << %(\n) expected << %(\n" expected << " : " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), include_seconds: true) end def test_select_time_with_seconds_and_separator expected = %(\n) expected << %(\n) expected << %(\n) expected << %(\n" expected << " : " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), include_seconds: true, time_separator: " : ") end def test_select_time_with_html_options expected = %(\n) expected << %(\n) expected << %(\n) expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), {}, class: "selector") assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), { include_seconds: false }, class: "selector") end def test_select_time_should_work_with_date assert_nothing_raised { select_time(Date.today) } end def test_select_time_with_default_prompt expected = %(\n) expected << %(\n) expected << %(\n) expected << %(\n" expected << " : " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), include_seconds: true, prompt: true) end def test_select_time_with_custom_prompt expected = %(\n) expected << %(\n) expected << %(\n) expected << %(\n" expected << " : " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), include_seconds: true, prompt: { hour: "Choose hour", minute: "Choose minute", second: "Choose seconds" }) end def test_select_time_with_generic_with_css_classes expected = %(\n) expected << %(\n) expected << %(\n) expected << %(\n" expected << " : " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), include_seconds: true, with_css_classes: true) end def test_select_time_with_custom_with_css_classes expected = %(\n) expected << %(\n) expected << %(\n) expected << %(\n" expected << " : " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), include_seconds: true, with_css_classes: { hour: "my-hour", minute: "my-minute", second: "my-second" }) end def test_select_time_with_hidden expected = %(\n) expected << %(\n) expected << %(\n) expected << %(\n) expected << %(\n) assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), prefix: "date[first]", use_hidden: true) assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), time_separator: ":", prefix: "date[first]", use_hidden: true) end def test_date_select @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on") end def test_date_select_with_selected @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", selected: Date.new(2004, 07, 10)) end def test_date_select_with_selected_in_hash @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", selected: { day: 10, month: 07, year: 2004 }) end def test_date_select_with_selected_nil @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = '' + "\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", include_blank: true, discard_year: true, selected: nil) end def test_date_select_without_day @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = "\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", order: [ :month, :year ]) end def test_date_select_without_day_and_month @post = Post.new @post.written_on = Date.new(2004, 2, 29) expected = "\n" expected << "\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", order: [ :year ]) end def test_date_select_without_day_with_separator @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = "\n" expected << %{\n" expected << "/" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", date_separator: "/", order: [ :month, :year ]) end def test_date_select_without_day_and_with_disabled_html_option @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = "\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", { order: [ :month, :year ] }, disabled: true) end def test_date_select_within_fields_for @post = Post.new @post.written_on = Date.new(2004, 6, 15) output_buffer = fields_for :post, @post do |f| concat f.date_select(:written_on) end expected = %{\n} expected << %{\n} expected << %{\n} assert_dom_equal(expected, output_buffer) end def test_date_select_within_fields_for_with_index @post = Post.new @post.written_on = Date.new(2004, 6, 15) id = 27 output_buffer = fields_for :post, @post, index: id do |f| concat f.date_select(:written_on) end expected = %{\n} expected << %{\n} expected << %{\n} assert_dom_equal(expected, output_buffer) end def test_date_select_within_fields_for_with_blank_index @post = Post.new @post.written_on = Date.new(2004, 6, 15) id = nil output_buffer = fields_for :post, @post, index: id do |f| concat f.date_select(:written_on) end expected = %{\n} expected << %{\n} expected << %{\n} assert_dom_equal(expected, output_buffer) end def test_date_select_with_index @post = Post.new @post.written_on = Date.new(2004, 6, 15) id = 456 expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", index: id) end def test_date_select_with_auto_index @post = Post.new @post.written_on = Date.new(2004, 6, 15) id = 123 expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post[]", "written_on") end def test_date_select_with_different_order @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", order: [:day, :month, :year]) end def test_date_select_with_nil @post = Post.new start_year = Time.now.year-5 end_year = Time.now.year+5 expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on") end def test_date_select_with_nil_and_blank @post = Post.new start_year = Time.now.year-5 end_year = Time.now.year+5 expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", include_blank: true) end def test_date_select_with_nil_and_blank_and_order @post = Post.new start_year = Time.now.year-5 end_year = Time.now.year+5 expected = '' + "\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", order: [:year, :month], include_blank: true) end def test_date_select_with_nil_and_blank_and_discard_month @post = Post.new start_year = Time.now.year-5 end_year = Time.now.year+5 expected = %{\n" expected << '' + "\n" expected << '' + "\n" assert_dom_equal expected, date_select("post", "written_on", discard_month: true, include_blank: true) end def test_date_select_with_nil_and_blank_and_discard_year @post = Post.new expected = '' + "\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", discard_year: true, include_blank: true) end def test_date_select_cant_override_discard_hour @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", discard_hour: false) end def test_date_select_with_html_options @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", {}, class: "selector") end def test_date_select_with_html_options_within_fields_for @post = Post.new @post.written_on = Date.new(2004, 6, 15) output_buffer = fields_for :post, @post do |f| concat f.date_select(:written_on, {}, class: "selector") end expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, output_buffer end def test_date_select_with_separator @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << " / " expected << %{\n" expected << " / " expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", date_separator: " / ") end def test_date_select_with_separator_and_order @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << " / " expected << %{\n" expected << " / " expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", order: [:day, :month, :year], date_separator: " / ") end def test_date_select_with_separator_and_order_and_year_discarded @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << " / " expected << %{\n" expected << %{\n} assert_dom_equal expected, date_select("post", "written_on", order: [:day, :month, :year], discard_year: true, date_separator: " / ") end def test_date_select_with_default_prompt @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", prompt: true) end def test_date_select_with_custom_prompt @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", prompt: { year: "Choose year", month: "Choose month", day: "Choose day" }) end def test_date_select_with_generic_with_css_classes @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", with_css_classes: true) end def test_date_select_with_custom_with_css_classes @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "written_on", with_css_classes: { year: "my-year", month: "my-month", day: "my-day" }) end def test_time_select @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, time_select("post", "written_on") end def test_time_select_with_selected @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, time_select("post", "written_on", selected: Time.local(2004, 6, 15, 12, 20, 30)) end def test_time_select_with_selected_nil @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, time_select("post", "written_on", discard_year: true, discard_month: true, discard_day: true, selected: nil) end def test_time_select_without_date_hidden_fields @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, time_select("post", "written_on", ignore_date: true) end def test_time_select_with_seconds @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %(\n" expected << " : " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, time_select("post", "written_on", include_seconds: true) end def test_time_select_with_html_options @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, time_select("post", "written_on", {}, class: "selector") end def test_time_select_with_html_options_within_fields_for @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) output_buffer = fields_for :post, @post do |f| concat f.time_select(:written_on, {}, class: "selector") end expected = %{\n} expected << %{\n} expected << %{\n} expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, output_buffer end def test_time_select_with_separator @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %(\n" expected << " - " expected << %(\n" expected << " - " expected << %(\n" assert_dom_equal expected, time_select("post", "written_on", time_separator: " - ", include_seconds: true) end def test_time_select_with_default_prompt @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, time_select("post", "written_on", prompt: true) end def test_time_select_with_custom_prompt @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, time_select("post", "written_on", prompt: { hour: "Choose hour", minute: "Choose minute" }) end def test_time_select_with_generic_with_css_classes @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, time_select("post", "written_on", with_css_classes: true) end def test_time_select_with_custom_with_css_classes @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, time_select("post", "written_on", with_css_classes: { hour: "my-hour", minute: "my-minute" }) end def test_time_select_with_disabled_html_option @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, time_select("post", "written_on", {}, disabled: true) end def test_datetime_select @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at") end def test_datetime_select_with_selected @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", selected: Time.local(2004, 3, 10, 12, 30)) end def test_datetime_select_with_selected_nil @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) expected = '' + "\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", discard_year: true, selected: nil) end def test_datetime_select_defaults_to_time_zone_now_when_config_time_zone_is_set # The love zone is UTC+0 mytz = Class.new(ActiveSupport::TimeZone) { attr_accessor :now }.create("tenderlove", 0, ActiveSupport::TimeZone.find_tzinfo("UTC")) now = Time.mktime(2004, 6, 15, 16, 35, 0) mytz.now = now Time.zone = mytz assert_equal mytz, Time.zone @post = Post.new expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at") ensure Time.zone = nil end def test_datetime_select_with_html_options_within_fields_for @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) output_buffer = fields_for :post, @post do |f| concat f.datetime_select(:updated_at, {}, class: "selector") end expected = %{\n} expected << %{\n} expected << %{\n} expected << %{ — \n} expected << %{ : \n} assert_dom_equal expected, output_buffer end def test_datetime_select_with_separators @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n" expected << " / " expected << %{\n" expected << " / " expected << %{\n" expected << " , " expected << %(\n" expected << " - " expected << %(\n" expected << " - " expected << %(\n" assert_dom_equal expected, datetime_select("post", "updated_at", date_separator: " / ", datetime_separator: " , ", time_separator: " - ", include_seconds: true) end def test_datetime_select_with_integer @post = Post.new @post.updated_at = 3 datetime_select("post", "updated_at") end def test_datetime_select_with_infinity # Float @post = Post.new @post.updated_at = (-1.0/0) datetime_select("post", "updated_at") end def test_datetime_select_with_default_prompt @post = Post.new @post.updated_at = nil expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", start_year: 1999, end_year: 2009, prompt: true) end def test_datetime_select_with_custom_prompt @post = Post.new @post.updated_at = nil expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", start_year: 1999, end_year: 2009, prompt: { year: "Choose year", month: "Choose month", day: "Choose day", hour: "Choose hour", minute: "Choose minute" }) end def test_datetime_select_with_generic_with_css_classes @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "written_on", start_year: 1999, end_year: 2009, with_css_classes: true) end def test_datetime_select_with_custom_with_css_classes @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "written_on", start_year: 1999, end_year: 2009, with_css_classes: { year: "my-year", month: "my-month", day: "my-day", hour: "my-hour", minute: "my-minute" }) end def test_date_select_with_zero_value_and_no_start_year expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(0, end_year: Date.today.year+1, prefix: "date[first]") end def test_date_select_with_zero_value_and_no_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(0, start_year: 2003, prefix: "date[first]") end def test_date_select_with_zero_value_and_no_start_and_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(0, prefix: "date[first]") end def test_date_select_with_nil_value_and_no_start_and_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_dom_equal expected, select_date(nil, prefix: "date[first]") end def test_datetime_select_with_nil_value_and_no_start_and_end_year expected = %(\n" expected << %(\n" expected << %(\n" expected << " — " expected << %(\n" expected << " : " expected << %(\n" assert_dom_equal expected, select_datetime(nil, prefix: "date[first]") end def test_datetime_select_with_options_index @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) id = 456 expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", index: id) end def test_datetime_select_within_fields_for_with_options_index @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) id = 456 output_buffer = fields_for :post, @post, index: id do |f| concat f.datetime_select(:updated_at) end expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, output_buffer end def test_datetime_select_with_auto_index @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) id = @post.id expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post[]", "updated_at") end def test_datetime_select_with_seconds @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", include_seconds: true) end def test_datetime_select_discard_year @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", discard_year: true) end def test_datetime_select_discard_month @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n" expected << %{\n} expected << %{\n} expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", discard_month: true) end def test_datetime_select_discard_year_and_month @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", discard_year: true, discard_month: true) end def test_datetime_select_discard_year_and_month_with_disabled_html_option @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", { discard_year: true, discard_month: true }, disabled: true) end def test_datetime_select_discard_hour @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", discard_hour: true) end def test_datetime_select_discard_minute @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << %{\n} assert_dom_equal expected, datetime_select("post", "updated_at", discard_minute: true) end def test_datetime_select_disabled_and_discard_minute @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << %{\n} assert_dom_equal expected, datetime_select("post", "updated_at", discard_minute: true, disabled: true) end def test_datetime_select_invalid_order @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", order: [:minute, :day, :hour, :month, :year, :second]) end def test_datetime_select_discard_with_order @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", order: [:day, :month]) end def test_datetime_select_with_default_value_as_time @post = Post.new @post.updated_at = nil expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", default: Time.local(2006, 9, 19, 15, 16, 35)) end def test_include_blank_overrides_default_option @post = Post.new @post.updated_at = nil expected = %{\n" expected << %{\n" expected << %{\n" assert_dom_equal expected, date_select("post", "updated_at", default: Time.local(2006, 9, 19, 15, 16, 35), include_blank: true) end def test_datetime_select_with_default_value_as_hash @post = Post.new @post.updated_at = nil expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", default: { month: 10, minute: 42, hour: 9 }) end def test_datetime_select_with_html_options @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_dom_equal expected, datetime_select("post", "updated_at", {}, class: "selector") end def test_date_select_should_not_change_passed_options_hash @post = Post.new @post.updated_at = Time.local(2008, 7, 16, 23, 30) options = { order: [ :year, :month, :day ], default: { year: 2008, month: 7, day: 16, hour: 23, minute: 30, second: 1 }, discard_type: false, include_blank: false, ignore_date: false, include_seconds: true } date_select(@post, :updated_at, options) # note: the literal hash is intentional to show that the actual options hash isn't modified # don't change this! assert_equal({ order: [ :year, :month, :day ], default: { year: 2008, month: 7, day: 16, hour: 23, minute: 30, second: 1 }, discard_type: false, include_blank: false, ignore_date: false, include_seconds: true }, options) end def test_datetime_select_should_not_change_passed_options_hash @post = Post.new @post.updated_at = Time.local(2008, 7, 16, 23, 30) options = { order: [ :year, :month, :day ], default: { year: 2008, month: 7, day: 16, hour: 23, minute: 30, second: 1 }, discard_type: false, include_blank: false, ignore_date: false, include_seconds: true } datetime_select(@post, :updated_at, options) # note: the literal hash is intentional to show that the actual options hash isn't modified # don't change this! assert_equal({ order: [ :year, :month, :day ], default: { year: 2008, month: 7, day: 16, hour: 23, minute: 30, second: 1 }, discard_type: false, include_blank: false, ignore_date: false, include_seconds: true }, options) end def test_time_select_should_not_change_passed_options_hash @post = Post.new @post.updated_at = Time.local(2008, 7, 16, 23, 30) options = { order: [ :year, :month, :day ], default: { year: 2008, month: 7, day: 16, hour: 23, minute: 30, second: 1 }, discard_type: false, include_blank: false, ignore_date: false, include_seconds: true } time_select(@post, :updated_at, options) # note: the literal hash is intentional to show that the actual options hash isn't modified # don't change this! assert_equal({ order: [ :year, :month, :day ], default: { year: 2008, month: 7, day: 16, hour: 23, minute: 30, second: 1 }, discard_type: false, include_blank: false, ignore_date: false, include_seconds: true }, options) end def test_select_date_should_not_change_passed_options_hash options = { order: [ :year, :month, :day ], default: { year: 2008, month: 7, day: 16, hour: 23, minute: 30, second: 1 }, discard_type: false, include_blank: false, ignore_date: false, include_seconds: true } select_date(Date.today, options) # note: the literal hash is intentional to show that the actual options hash isn't modified # don't change this! assert_equal({ order: [ :year, :month, :day ], default: { year: 2008, month: 7, day: 16, hour: 23, minute: 30, second: 1 }, discard_type: false, include_blank: false, ignore_date: false, include_seconds: true }, options) end def test_select_datetime_should_not_change_passed_options_hash options = { order: [ :year, :month, :day ], default: { year: 2008, month: 7, day: 16, hour: 23, minute: 30, second: 1 }, discard_type: false, include_blank: false, ignore_date: false, include_seconds: true } select_datetime(Time.now, options) # note: the literal hash is intentional to show that the actual options hash isn't modified # don't change this! assert_equal({ order: [ :year, :month, :day ], default: { year: 2008, month: 7, day: 16, hour: 23, minute: 30, second: 1 }, discard_type: false, include_blank: false, ignore_date: false, include_seconds: true }, options) end def test_select_time_should_not_change_passed_options_hash options = { order: [ :year, :month, :day ], default: { year: 2008, month: 7, day: 16, hour: 23, minute: 30, second: 1 }, discard_type: false, include_blank: false, ignore_date: false, include_seconds: true } select_time(Time.now, options) # note: the literal hash is intentional to show that the actual options hash isn't modified # don't change this! assert_equal({ order: [ :year, :month, :day ], default: { year: 2008, month: 7, day: 16, hour: 23, minute: 30, second: 1 }, discard_type: false, include_blank: false, ignore_date: false, include_seconds: true }, options) end def test_select_html_safety assert select_day(16).html_safe? assert select_month(8).html_safe? assert select_year(Time.mktime(2003, 8, 16, 8, 4, 18)).html_safe? assert select_minute(Time.mktime(2003, 8, 16, 8, 4, 18)).html_safe? assert select_second(Time.mktime(2003, 8, 16, 8, 4, 18)).html_safe? assert select_minute(8, use_hidden: true).html_safe? assert select_month(8, prompt: "Choose month").html_safe? assert select_time(Time.mktime(2003, 8, 16, 8, 4, 18), {}, class: "selector").html_safe? assert select_date(Time.mktime(2003, 8, 16), date_separator: " / ", start_year: 2003, end_year: 2005, prefix: "date[first]").html_safe? end def test_object_select_html_safety @post = Post.new @post.written_on = Date.new(2004, 6, 15) assert date_select("post", "written_on", default: Time.local(2006, 9, 19, 15, 16, 35), include_blank: true).html_safe? assert time_select("post", "written_on", ignore_date: true).html_safe? end def test_time_tag_with_date date = Date.new(2013, 2, 20) expected = '' assert_equal expected, time_tag(date) end def test_time_tag_with_time time = Time.new(2013, 2, 20, 0, 0, 0, "+00:00") expected = '' assert_equal expected, time_tag(time) end def test_time_tag_with_given_text assert_match(/Right now<\/time>/, time_tag(Time.now, "Right now")) end def test_time_tag_with_given_block assert_match(/Right now<\/span><\/time>/, time_tag(Time.now) { raw("Right now") }) end def test_time_tag_with_different_format time = Time.new(2013, 2, 20, 0, 0, 0, "+00:00") expected = '' assert_equal expected, time_tag(time, format: :short) end end