public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Refactor DateHelper to use DateTimeSelector presenter pattern
Signed-off-by: Joshua Peek <josh@joshpeek.com>
clemens (author)
Thu Aug 07 11:13:47 -0700 2008
josh (committer)
Thu Aug 07 11:13:47 -0700 2008
commit  105093f90728f81268367bd52581fccfa165f170
tree    3ca826186b5fdcce8708445eb9283743f645034d
parent  be0d235a3b82e72de49592913753a1f291a98f86
...
13
14
15
16
17
18
19
20
21
...
52
53
54
55
 
56
57
58
...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
126
127
...
165
166
167
168
169
170
 
 
 
171
172
173
...
178
179
180
181
 
 
182
183
184
...
210
211
212
213
 
 
214
215
216
...
230
231
232
233
234
235
236
237
238
 
 
 
 
 
 
239
240
241
...
270
271
272
273
274
 
275
276
277
278
279
280
 
 
281
282
283
...
307
308
309
310
311
312
313
314
315
 
316
317
318
...
343
344
345
346
347
348
 
349
350
351
...
366
367
368
369
370
371
372
 
373
374
375
376
377
 
 
378
379
380
...
391
392
393
394
395
396
397
398
 
399
400
401
...
416
417
418
419
420
421
 
422
423
424
...
439
440
441
442
443
444
445
446
 
447
448
449
...
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
 
514
515
516
...
537
538
539
540
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
541
542
543
544
545
546
 
547
548
549
550
 
 
551
552
553
554
555
556
557
558
559
560
561
562
 
 
 
 
 
 
 
563
564
565
566
567
568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
569
570
571
572
 
 
573
574
575
576
577
578
579
580
581
582
583
584
585
 
 
 
 
 
 
 
 
 
 
 
 
586
587
588
 
 
 
589
590
591
592
593
594
 
 
 
 
 
 
 
 
 
 
595
596
597
598
599
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
600
601
602
603
604
605
606
607
 
608
609
610
611
 
612
613
614
615
 
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
 
 
 
 
 
 
 
 
 
 
 
 
673
674
675
676
677
678
679
680
681
682
683
 
 
684
685
686
 
687
688
689
690
 
691
 
 
692
693
694
...
699
700
701
702
703
 
 
 
 
 
704
705
706
...
13
14
15
 
 
 
16
17
18
...
49
50
51
 
52
53
54
55
...
106
107
108
 
 
 
 
 
 
 
 
 
 
 
 
 
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
...
179
180
181
 
 
 
182
183
184
185
186
187
...
192
193
194
 
195
196
197
198
199
...
225
226
227
 
228
229
230
231
232
...
246
247
248
 
 
 
 
 
 
249
250
251
252
253
254
255
256
257
...
286
287
288
 
 
289
290
291
292
293
 
 
294
295
296
297
298
...
322
323
324
 
 
 
 
 
 
325
326
327
328
...
353
354
355
 
 
 
356
357
358
359
...
374
375
376
 
 
 
 
377
378
379
380
 
 
381
382
383
384
385
...
396
397
398
 
 
 
 
 
399
400
401
402
...
417
418
419
 
 
 
420
421
422
423
...
438
439
440
 
 
 
 
 
441
442
443
444
...
476
477
478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
479
480
481
482
...
503
504
505
 
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
 
 
649
 
650
651
652
 
 
653
654
655
 
 
 
 
 
 
 
 
 
 
 
656
657
658
659
660
661
662
663
664
665
666
 
 
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
 
 
744
745
746
747
 
748
749
750
751
752
 
 
 
 
 
753
754
755
756
757
758
759
760
761
762
763
764
765
 
 
766
767
768
769
770
771
 
 
 
772
773
774
775
776
777
778
779
780
781
782
783
 
 
 
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
 
 
831
 
832
833
834
835
 
836
837
838
839
 
840
841
842
843
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
844
845
846
847
848
849
850
851
852
853
854
855
856
857
 
 
 
 
 
 
 
 
 
858
859
860
 
 
861
862
863
864
 
865
866
867
868
869
870
871
...
876
877
878
 
 
879
880
881
882
883
884
885
886
0
@@ -13,9 +13,6 @@ module ActionView
0
     #   the select_month method would use simply "date" (which can be overwritten using <tt>:prefix</tt>) instead of
0
     #   "date[month]".
0
     module DateHelper
0
-      include ActionView::Helpers::TagHelper
0
-      DEFAULT_PREFIX = 'date' unless const_defined?('DEFAULT_PREFIX')
0
-
0
       # Reports the approximate distance in time between two Time or Date objects or integers as seconds.
0
       # Set <tt>include_seconds</tt> to true if you want more detailed approximations when distance < 1 min, 29 secs
0
       # Distances are reported based on the following table:
0
@@ -52,7 +49,7 @@ module ActionView
0
       #   distance_of_time_in_words(from_time, from_time - 45.seconds, true)  # => less than a minute
0
       #   distance_of_time_in_words(from_time, 76.seconds.from_now)           # => 1 minute
0
       #   distance_of_time_in_words(from_time, from_time + 1.year + 3.days)   # => about 1 year
0
-      #   distance_of_time_in_words(from_time, from_time + 4.years + 15.days + 30.minutes + 5.seconds) # => over 4 years
0
+      #   distance_of_time_in_words(from_time, from_time + 4.years + 9.days + 30.minutes + 5.seconds) # => over 4 years
0
       #
0
       #   to_time = Time.now + 6.years + 19.days
0
       #   distance_of_time_in_words(from_time, to_time, true)     # => over 6 years
0
@@ -109,19 +106,36 @@ module ActionView
0
       alias_method :distance_of_time_in_words_to_now, :time_ago_in_words
0
 
0
       # Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based
0
-      # attribute (identified by +method+) on an object assigned to the template (identified by +object+). It's
0
-      # possible to tailor the selects through the +options+ hash, which accepts all the keys that each of the
0
-      # individual select builders do (like <tt>:use_month_numbers</tt> for select_month) as well as a range of discard
0
-      # options. The discard options are <tt>:discard_year</tt>, <tt>:discard_month</tt> and <tt>:discard_day</tt>. Set
0
-      # to true, they'll drop the respective select. Discarding the month select will also automatically discard the
0
-      # day select. It's also possible to explicitly set the order of the tags using the <tt>:order</tt> option with an
0
-      # array of symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. Symbols may be omitted
0
-      # and the respective select is not included.
0
-      #
0
-      # Pass the <tt>:default</tt> option to set the default date. Use a Time object or a Hash of <tt>:year</tt>,
0
-      # <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>, <tt>:minute</tt>, and <tt>:second</tt>.
0
-      #
0
-      # Passing <tt>:disabled => true</tt> as part of the +options+ will make elements inaccessible for change.
0
+      # attribute (identified by +method+) on an object assigned to the template (identified by +object+). You can
0
+      # the output in the +options+ hash.
0
+      #
0
+      # ==== Options
0
+      # * <tt>:use_month_numbers</tt> - Set to true if you want to use month numbers rather than month names (e.g.
0
+      #    "2" instead of "February").
0
+      # * <tt>:use_short_month</tt>   - Set to true if you want to use the abbreviated month name instead of the full
0
+      #   name (e.g. "Feb" instead of "February").
0
+      # * <tt>:add_month_number</tt>  - Set to true if you want to show both, the month's number and name (e.g.
0
+      #   "2 - February" instead of "February").
0
+      # * <tt>:use_month_names</tt>   - Set to an array with 12 month names if you want to customize month names.
0
+      #   Note: You can also use Rails' new i18n functionality for this.
0
+      # * <tt>:date_separator</tt>    - Specifies a string to separate the date fields. Default is "" (i.e. nothing).
0
+      # * <tt>:start_year</tt>        - Set the start year for the year select. Default is <tt>Time.now.year - 5</tt>.
0
+      # * <tt>:end_year</tt>          - Set the end year for the year select. Default is <tt>Time.now.year + 5</tt>.
0
+      # * <tt>:discard_day</tt>       - Set to true if you don't want to show a day select. This includes the day
0
+      #   as a hidden field instead of showing a select field. Also note that this implicitly sets the day to be the
0
+      #   first of the given month in order to not create invalid dates like 31 February.
0
+      # * <tt>:discard_month</tt>     - Set to true if you don't want to show a month select. This includes the month
0
+      #   as a hidden field instead of showing a select field. Also note that this implicitly sets :discard_day to true.
0
+      # * <tt>:discard_year</tt>      - Set to true if you don't want to show a year select. This includes the year
0
+      #   as a hidden field instead of showing a select field.
0
+      # * <tt>:order</tt>             - Set to an array containing <tt>:day</tt>, <tt>:month</tt> and <tt>:year</tt> do
0
+      #   customize the order in which the select fields are shown. If you leave out any of the symbols, the respective
0
+      #   select will not be shown (like when you set <tt>:discard_xxx => true</tt>. Defaults to the order defined in
0
+      #   the respective locale (e.g. [:year, :month, :day] in the en-US locale that ships with Rails).
0
+      # * <tt>:include_blank</tt>     - Include a blank option in every select field so it's possible to set empty
0
+      #   dates.
0
+      # * <tt>:default</tt>           - Set a default date if the affected date isn't set or is nil.
0
+      # * <tt>:disabled</tt>          - Set to true if you want show the select fields as disabled.
0
       #
0
       # If anything is passed in the +html_options+ hash it will be applied to every select tag in the set.
0
       #
0
@@ -165,9 +179,9 @@ module ActionView
0
         InstanceTag.new(object_name, method, self, options.delete(:object)).to_date_select_tag(options, html_options)
0
       end
0
 
0
-      # Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a specified
0
-      # time-based attribute (identified by +method+) on an object assigned to the template (identified by +object+).
0
-      # You can include the seconds with <tt>:include_seconds</tt>.
0
+      # Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a
0
+      # specified time-based attribute (identified by +method+) on an object assigned to the template (identified by
0
+      # +object+). You can include the seconds with <tt>:include_seconds</tt>.
0
       #
0
       # This method will also generate 3 input hidden tags, for the actual year, month and day unless the option
0
       # <tt>:ignore_date</tt> is set to +true+.
0
@@ -178,7 +192,8 @@ module ActionView
0
       #   # Creates a time select tag that, when POSTed, will be stored in the post variable in the sunrise attribute
0
       #   time_select("post", "sunrise")
0
       #
0
-      #   # Creates a time select tag that, when POSTed, will be stored in the order variable in the submitted attribute
0
+      #   # Creates a time select tag that, when POSTed, will be stored in the order variable in the submitted
0
+      #   # attribute
0
       #   time_select("order", "submitted")
0
       #
0
       #   # Creates a time select tag that, when POSTed, will be stored in the mail variable in the sent_at attribute
0
@@ -210,7 +225,8 @@ module ActionView
0
       # If anything is passed in the html_options hash it will be applied to every select tag in the set.
0
       #
0
       # ==== Examples
0
-      #   # Generates a datetime select that, when POSTed, will be stored in the post variable in the written_on attribute
0
+      #   # Generates a datetime select that, when POSTed, will be stored in the post variable in the written_on
0
+      #   # attribute
0
       #   datetime_select("post", "written_on")
0
       #
0
       #   # Generates a datetime select with a year select that starts at 1995 that, when POSTed, will be stored in the
0
@@ -230,12 +246,12 @@ module ActionView
0
         InstanceTag.new(object_name, method, self, options.delete(:object)).to_datetime_select_tag(options, html_options)
0
       end
0
 
0
-      # Returns a set of html select-tags (one for year, month, day, hour, and minute) pre-selected with the +datetime+.
0
-      # It's also possible to explicitly set the order of the tags using the <tt>:order</tt> option with an array of
0
-      # symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. If you do not supply a Symbol,
0
-      # it will be appended onto the <tt>:order</tt> passed in. You can also add <tt>:date_separator</tt>,
0
-      # <tt>:datetime_separator</tt> and <tt>:time_separator</tt> keys to the +options+ to control visual display of
0
-      # the elements.
0
+      # Returns a set of html select-tags (one for year, month, day, hour, and minute) pre-selected with the
0
+      # +datetime+. It's also possible to explicitly set the order of the tags using the <tt>:order</tt> option with
0
+      # an array of symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. If you do not
0
+      # supply a Symbol, it will be appended onto the <tt>:order</tt> passed in. You can also add
0
+      # <tt>:date_separator</tt>, <tt>:datetime_separator</tt> and <tt>:time_separator</tt> keys to the +options+ to
0
+      # control visual display of the elements.
0
       #
0
       # If anything is passed in the html_options hash it will be applied to every select tag in the set.
0
       #
0
@@ -270,14 +286,13 @@ module ActionView
0
       #   select_datetime(my_date_time, :prefix => 'payday')
0
       #
0
       def select_datetime(datetime = Time.current, options = {}, html_options = {})
0
-        separator = options[:datetime_separator] || ''
0
-        select_date(datetime, options, html_options) + separator + select_time(datetime, options, html_options)
0
+        DateTimeSelector.new(datetime, options, html_options).select_datetime
0
       end
0
 
0
       # Returns a set of html select-tags (one for year, month, and day) pre-selected with the +date+.
0
       # It's possible to explicitly set the order of the tags using the <tt>:order</tt> option with an array of
0
-      # symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. If you do not supply a Symbol, it
0
-      # will be appended onto the <tt>:order</tt> passed in.
0
+      # symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. If you do not supply a Symbol,
0
+      # it will be appended onto the <tt>:order</tt> passed in.
0
       #
0
       # If anything is passed in the html_options hash it will be applied to every select tag in the set.
0
       #
0
@@ -307,12 +322,7 @@ module ActionView
0
       #   select_date(my_date, :prefix => 'payday')
0
       #
0
       def select_date(date = Date.current, options = {}, html_options = {})
0
-        options.reverse_merge!(:order => [], :date_separator => '')
0
-        [:year, :month, :day].each { |o| options[:order].push(o) unless options[:order].include?(o) }
0
-
0
-        options[:order].inject([]) { |s, o|
0
-          s << self.send("select_#{o}", date, options, html_options)
0
-        }.join(options[:date_separator])
0
+        DateTimeSelector.new(date, options, html_options).select_date
0
       end
0
 
0
       # Returns a set of html select-tags (one for hour and minute)
0
@@ -343,9 +353,7 @@ module ActionView
0
       #   select_time(my_time, :time_separator => ':', :include_seconds => true)
0
       #
0
       def select_time(datetime = Time.current, options = {}, html_options = {})
0
-        separator = options[:time_separator] || ''
0
-        select_hour(datetime, options, html_options) + separator + select_minute(datetime, options, html_options) +
0
-          (options[:include_seconds] ? separator + select_second(datetime, options, html_options) : '')
0
+        DateTimeSelector.new(datetime, options, html_options).select_time
0
       end
0
 
0
       # Returns a select tag with options for each of the seconds 0 through 59 with the current second selected.
0
@@ -366,15 +374,12 @@ module ActionView
0
       #   select_second(my_time, :field_name => 'interval')
0
       #
0
       def select_second(datetime, options = {}, html_options = {})
0
-        val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.sec) : ''
0
-        options[:use_hidden] ?
0
-          (options[:include_seconds] ? _date_hidden_html(options[:field_name] || 'second', val, options) : '') :
0
-          _date_select_html(options[:field_name] || 'second', _date_build_options(val), options, html_options)
0
+        DateTimeSelector.new(datetime, options, html_options).select_second
0
       end
0
 
0
       # Returns a select tag with options for each of the minutes 0 through 59 with the current minute selected.
0
-      # Also can return a select tag with options by <tt>minute_step</tt> from 0 through 59 with the 00 minute selected
0
-      # The <tt>minute</tt> can also be substituted for a minute number.
0
+      # Also can return a select tag with options by <tt>minute_step</tt> from 0 through 59 with the 00 minute
0
+      # selected. The <tt>minute</tt> can also be substituted for a minute number.
0
       # Override the field name using the <tt>:field_name</tt> option, 'minute' by default.
0
       #
0
       # ==== Examples
0
@@ -391,11 +396,7 @@ module ActionView
0
       #   select_minute(my_time, :field_name => 'stride')
0
       #
0
       def select_minute(datetime, options = {}, html_options = {})
0
-        val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.min) : ''
0
-        options[:use_hidden] ?
0
-          _date_hidden_html(options[:field_name] || 'minute', val, options) :
0
-          _date_select_html(options[:field_name] || 'minute',
0
-            _date_build_options(val, :step => options[:minute_step]), options, html_options)
0
+        DateTimeSelector.new(datetime, options, html_options).select_minute
0
       end
0
 
0
       # Returns a select tag with options for each of the hours 0 through 23 with the current hour selected.
0
@@ -416,9 +417,7 @@ module ActionView
0
       #   select_minute(my_time, :field_name => 'stride')
0
       #
0
       def select_hour(datetime, options = {}, html_options = {})
0
-        val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.hour) : ''
0
-        options[:use_hidden] ? _date_hidden_html(options[:field_name] || 'hour', val, options) :
0
-          _date_select_html(options[:field_name] || 'hour', _date_build_options(val, :end => 23), options, html_options)
0
+        DateTimeSelector.new(datetime, options, html_options).select_hour
0
       end
0
 
0
       # Returns a select tag with options for each of the days 1 through 31 with the current day selected.
0
@@ -439,11 +438,7 @@ module ActionView
0
       #   select_day(my_time, :field_name => 'due')
0
       #
0
       def select_day(date, options = {}, html_options = {})
0
-        val = date ? (date.kind_of?(Fixnum) ? date : date.day) : ''
0
-        options[:use_hidden] ? _date_hidden_html(options[:field_name] || 'day', val, options) :
0
-          _date_select_html(options[:field_name] || 'day',
0
-            _date_build_options(val, :start => 1, :end => 31, :leading_zeros => false),
0
-            options, html_options)
0
+        DateTimeSelector.new(date, options, html_options).select_day
0
       end
0
 
0
       # Returns a select tag with options for each of the months January through December with the current month
0
@@ -481,36 +476,7 @@ module ActionView
0
       #   select_month(Date.today, :use_month_names => %w(Januar Februar Marts ...))
0
       #
0
       def select_month(date, options = {}, html_options = {})
0
-        locale = options[:locale]
0
-
0
-        val = date ? (date.kind_of?(Fixnum) ? date : date.month) : ''
0
-        if options[:use_hidden]
0
-          _date_hidden_html(options[:field_name] || 'month', val, options)
0
-        else
0
-          month_options = []
0
-          month_names = options[:use_month_names] || begin
0
-            key = options[:use_short_month] ? :'date.abbr_month_names' : :'date.month_names'
0
-            I18n.translate key, :locale => locale
0
-          end
0
-          month_names.unshift(nil) if month_names.size < 13
0
-
0
-          1.upto(12) do |month_number|
0
-            month_name = if options[:use_month_numbers]
0
-              month_number
0
-            elsif options[:add_month_numbers]
0
-              month_number.to_s + ' - ' + month_names[month_number]
0
-            else
0
-              month_names[month_number]
0
-            end
0
-
0
-            month_options << ((val == month_number) ?
0
-              content_tag(:option, month_name, :value => month_number, :selected => "selected") :
0
-              content_tag(:option, month_name, :value => month_number)
0
-            )
0
-            month_options << "\n"
0
-          end
0
-          _date_select_html(options[:field_name] || 'month', month_options.join, options, html_options)
0
-        end
0
+        DateTimeSelector.new(date, options, html_options).select_month
0
       end
0
 
0
       # Returns a select tag with options for each of the five years on each side of the current, which is selected.
0
@@ -537,158 +503,369 @@ module ActionView
0
       #   select_year(2006, :start_year => 2000, :end_year => 2010)
0
       #
0
       def select_year(date, options = {}, html_options = {})
0
-        if !date || date == 0
0
+        DateTimeSelector.new(date, options, html_options).select_year
0
+      end
0
+    end
0
+
0
+    class DateTimeSelector #:nodoc:
0
+      extend ActiveSupport::Memoizable
0
+      include ActionView::Helpers::TagHelper
0
+
0
+      DEFAULT_PREFIX = 'date'.freeze unless const_defined?('DEFAULT_PREFIX')
0
+      POSITION = {
0
+        :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6
0
+      }.freeze unless const_defined?('POSITION')
0
+
0
+      def initialize(datetime, options = {}, html_options = {})
0
+        @options      = options.dup
0
+        @html_options = html_options.dup
0
+        @datetime     = datetime
0
+      end
0
+
0
+      def select_datetime
0
+        # TODO: Remove tag conditional
0
+        # Ideally we could just join select_date and select_date for the tag case
0
+        if @options[:tag] && @options[:ignore_date]
0
+          select_time
0
+        elsif @options[:tag]
0
+          order = date_order.dup
0
+          order -= [:hour, :minute, :second]
0
+
0
+          @options[:discard_year]   ||= true unless order.include?(:year)
0
+          @options[:discard_month]  ||= true unless order.include?(:month)
0
+          @options[:discard_day]    ||= true if @options[:discard_month] || !order.include?(:day)
0
+          @options[:discard_minute] ||= true if @options[:discard_hour]
0
+          @options[:discard_second] ||= true unless @options[:include_seconds] && !@options[:discard_minute]
0
+
0
+          # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are
0
+          # valid (otherwise it could be 31 and february wouldn't be a valid date)
0
+          if @options[:discard_day] && !@options[:discard_month]
0
+            @datetime = @datetime.change(:day => 1)
0
+          end
0
+
0
+          [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }
0
+          order += [:hour, :minute, :second] unless @options[:discard_hour]
0
+
0
+          build_selects_from_types(order)
0
+        else
0
+          "#{select_date}#{@options[:datetime_separator]}#{select_time}"
0
+        end
0
+      end
0
+
0
+      def select_date
0
+        order = date_order.dup
0
+
0
+        # TODO: Remove tag conditional
0
+        if @options[:tag]
0
+          @options[:discard_hour]     = true
0
+          @options[:discard_minute]   = true
0
+          @options[:discard_second]   = true
0
+
0
+          @options[:discard_year]   ||= true unless order.include?(:year)
0
+          @options[:discard_month]  ||= true unless order.include?(:month)
0
+          @options[:discard_day]    ||= true if @options[:discard_month] || !order.include?(:day)
0
+
0
+          # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are
0
+          # valid (otherwise it could be 31 and february wouldn't be a valid date)
0
+          if @options[:discard_day] && !@options[:discard_month]
0
+            @datetime = @datetime.change(:day => 1)
0
+          end
0
+        end
0
+
0
+        [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }
0
+
0
+        build_selects_from_types(order)
0
+      end
0
+
0
+      def select_time
0
+        order = []
0
+
0
+        # TODO: Remove tag conditional
0
+        if @options[:tag]
0
+          @options[:discard_month]    = true
0
+          @options[:discard_year]     = true
0
+          @options[:discard_day]      = true
0
+          @options[:discard_second] ||= true unless @options[:include_seconds]
0
+
0
+          order += [:year, :month, :day] unless @options[:ignore_date]
0
+        end
0
+
0
+        order += [:hour, :minute]
0
+        order << :second if @options[:include_seconds]
0
+
0
+        build_selects_from_types(order)
0
+      end
0
+
0
+      def select_second
0
+        if @options[:use_hidden] || @options[:discard_second]
0
+          build_hidden(:second, sec) if @options[:include_seconds]
0
+        else
0
+          build_options_and_select(:second, sec)
0
+        end
0
+      end
0
+
0
+      def select_minute
0
+        if @options[:use_hidden] || @options[:discard_minute]
0
+          build_hidden(:minute, min)
0
+        else
0
+          build_options_and_select(:minute, min, :step => @options[:minute_step])
0
+        end
0
+      end
0
+
0
+      def select_hour
0
+        if @options[:use_hidden] || @options[:discard_hour]
0
+          build_hidden(:hour, hour)
0
+        else
0
+          build_options_and_select(:hour, hour, :end => 23)
0
+        end
0
+      end
0
+
0
+      def select_day
0
+        if @options[:use_hidden] || @options[:discard_day]
0
+          build_hidden(:day, day)
0
+        else
0
+          build_options_and_select(:day, day, :start => 1, :end => 31, :leading_zeros => false)
0
+        end
0
+      end
0
+
0
+      def select_month
0
+        if @options[:use_hidden] || @options[:discard_month]
0
+          build_hidden(:month, month)
0
+        else
0
+          month_options = []
0
+          1.upto(12) do |month_number|
0
+            options = { :value => month_number }
0
+            options[:selected] = "selected" if month == month_number
0
+            month_options << content_tag(:option, month_name(month_number), options) + "\n"
0
+          end
0
+          build_select(:month, month_options.join)
0
+        end
0
+      end
0
+
0
+      def select_year
0
+        if !@datetime || @datetime == 0
0
           val = ''
0
           middle_year = Date.today.year
0
-        elsif date.kind_of?(Fixnum)
0
-          val = middle_year = date
0
         else
0
-          val = middle_year = date.year
0
+          val = middle_year = year
0
         end
0
 
0
-        if options[:use_hidden]
0
-          _date_hidden_html(options[:field_name] || 'year', val, options)
0
+        if @options[:use_hidden] || @options[:discard_year]
0
+          build_hidden(:year, val)
0
         else
0
-          options[:start_year] ||= middle_year - 5
0
-          options[:end_year]   ||= middle_year + 5
0
-          step                   = options[:start_year] < options[:end_year] ? 1 : -1
0
-
0
-          _date_select_html(options[:field_name] || 'year',
0
-            _date_build_options(val,
0
-              :start => options[:start_year],
0
-              :end => options[:end_year],
0
-              :step => step,
0
-              :leading_zeros => false
0
-            ), options, html_options)
0
+          options                 = {}
0
+          options[:start]         = @options[:start_year] || middle_year - 5
0
+          options[:end]           = @options[:end_year] || middle_year + 5
0
+          options[:step]          = options[:start] < options[:end] ? 1 : -1
0
+          options[:leading_zeros] = false
0
+
0
+          build_options_and_select(:year, val, options)
0
         end
0
       end
0
 
0
       private
0
-        def _date_build_options(selected, options={})
0
-          options.reverse_merge!(:start => 0, :end => 59, :step => 1, :leading_zeros => true)
0
+        %w( sec min hour day month year ).each do |method|
0
+          define_method(method) do
0
+            @datetime.kind_of?(Fixnum) ? @datetime : @datetime.send(method) if @datetime
0
+          end
0
+        end
0
+
0
+        # Returns translated month names, but also ensures that a custom month
0
+        # name array has a leading nil element
0
+        def month_names
0
+          month_names = @options[:use_month_names] || translated_month_names
0
+          month_names.unshift(nil) if month_names.size < 13
0
+          month_names
0
+        end
0
+        memoize :month_names
0
+
0
+        # Returns translated month names
0
+        #  => [nil, "January", "February", "March",
0
+        #           "April", "May", "June", "July",
0
+        #           "August", "September", "October",
0
+        #           "November", "December"]
0
+        #
0
+        # If :use_short_month option is set
0
+        #  => [nil, "Jan", "Feb", "Mar", "Apr", "May", "Jun",
0
+        #           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
0
+        def translated_month_names
0
+          begin
0
+            key = @options[:use_short_month] ? :'date.abbr_month_names' : :'date.month_names'
0
+            I18n.translate(key, :locale => @options[:locale])
0
+          end
0
+        end
0
+
0
+        # Lookup month name for number
0
+        #  month_name(1) => "January"
0
+        #
0
+        # If :use_month_numbers option is passed
0
+        #  month_name(1) => 1
0
+        #
0
+        # If :add_month_numbers option is passed
0
+        #  month_name(1) => "1 - January"
0
+        def month_name(number)
0
+          if @options[:use_month_numbers]
0
+            number
0
+          elsif @options[:add_month_numbers]
0
+            "#{number} - #{month_names[number]}"
0
+          else
0
+            month_names[number]
0
+          end
0
+        end
0
+
0
+        def date_order
0
+          @options[:order] || translated_date_order
0
+        end
0
+        memoize :date_order
0
+
0
+        def translated_date_order
0
+          begin
0
+            I18n.translate(:'date.order', :locale => @options[:locale]) || []
0
+          end
0
+        end
0
+
0
+        # Build full select tag from date type and options
0
+        def build_options_and_select(type, selected, options = {})
0
+          build_select(type, build_options(selected, options))
0
+        end
0
+
0
+        # Build select option html from date value and options
0
+        #  build_options(15, :start => 1, :end => 31)
0
+        #  => "<option value="1">1</option>
0
+        #      <option value=\"2\">2</option>
0
+        #      <option value=\"3\">3</option>..."
0
+        def build_options(selected, options = {})
0
+          start         = options.delete(:start) || 0
0
+          stop          = options.delete(:end) || 59
0
+          step          = options.delete(:step) || 1
0
+          leading_zeros = options.delete(:leading_zeros).nil? ? true : false
0
 
0
           select_options = []
0
-          (options[:start] || 0).step((options[:end] || 59), options[:step] || 1) do |i|
0
-            value = options[:leading_zeros] ? sprintf("%02d", i) : i
0
+          start.step(stop, step) do |i|
0
+            value = leading_zeros ? sprintf("%02d", i) : i
0
             tag_options = { :value => value }
0
             tag_options[:selected] = "selected" if selected == i
0
-
0
             select_options << content_tag(:option, value, tag_options)
0
           end
0
           select_options.join("\n") + "\n"
0
         end
0
 
0
-        def _date_select_html(type, html_options, options, select_tag_options = {})
0
-          _date_name_and_id_from_options(options, type)
0
-          select_options = {:id => options[:id], :name => options[:name]}
0
-          select_options.merge!(:disabled => 'disabled') if options[:disabled]
0
-          select_options.merge!(select_tag_options) unless select_tag_options.empty?
0
+        # Builds select tag from date type and html select options
0
+        #  build_select(:month, "<option value="1">January</option>...")
0
+        #  => "<select id="post_written_on_2i" name="post[written_on(2i)]">
0
+        #        <option value="1">January</option>...
0
+        #      </select>"
0
+        def build_select(type, select_options_as_html)
0
+          select_options = {
0
+            :id => input_id_from_type(type),
0
+            :name => input_name_from_type(type)
0
+          }.merge(@html_options)
0
+          select_options.merge!(:disabled => 'disabled') if @options[:disabled]
0
+
0
           select_html = "\n"
0
-          select_html << content_tag(:option, '', :value => '') + "\n" if options[:include_blank]
0
-          select_html << html_options.to_s
0
+          select_html << content_tag(:option, '', :value => '') + "\n" if @options[:include_blank]
0
+          select_html << select_options_as_html.to_s
0
+
0
           content_tag(:select, select_html, select_options) + "\n"
0
         end
0
 
0
-        def _date_hidden_html(type, value, options)
0
-          _date_name_and_id_from_options(options, type)
0
-          hidden_html = tag(:input, :type => "hidden", :id => options[:id], :name => options[:name], :value => value) + "\n"
0
+        # Builds hidden input tag for date part and value
0
+        #  build_hidden(:year, 2008)
0
+        #  => "<input id="post_written_on_1i" name="post[written_on(1i)]" type="hidden" value="2008" />"
0
+        def build_hidden(type, value)
0
+          tag(:input, {
0
+            :type => "hidden",
0
+            :id => input_id_from_type(type),
0
+            :name => input_name_from_type(type),
0
+            :value => value
0
+          }) + "\n"
0
         end
0
 
0
-        def _date_name_and_id_from_options(options, type)
0
-          options[:name] = (options[:prefix] || DEFAULT_PREFIX) + (options[:discard_type] ? '' : "[#{type}]")
0
-          options[:id] = options[:name].gsub(/([\[\(])|(\]\[)/, '_').gsub(/[\]\)]/, '')
0
+        # Returns the name attribute for the input tag
0
+        #  => post[written_on(1i)]
0
+        def input_name_from_type(type)
0
+          prefix = @options[:prefix] || ActionView::Helpers::DateTimeSelector::DEFAULT_PREFIX
0
+          prefix += "[#{@options[:index]}]" if @options[:index]
0
+
0
+          field_name = @options[:field_name] || type
0
+          if @options[:include_position]
0
+            field_name += "(#{ActionView::Helpers::DateTimeSelector::POSITION[type]}i)"
0
+          end
0
+
0
+          @options[:discard_type] ? prefix : "#{prefix}[#{field_name}]"
0
+        end
0
+
0
+        # Returns the id attribute for the input tag
0
+        #  => "post_written_on_1i"
0
+        def input_id_from_type(type)
0
+          input_name_from_type(type).gsub(/([\[\(])|(\]\[)/, '_').gsub(/[\]\)]/, '')
0
+        end
0
+
0
+        # Given an ordering of datetime components, create the selection html
0
+        # and join them with their appropriate seperators
0
+        def build_selects_from_types(order)
0
+          select = ''
0
+          order.reverse.each do |type|
0
+            separator = separator(type) unless type == order.first # don't add on last field
0
+            select.insert(0, separator.to_s + send("select_#{type}").to_s)
0
+          end
0
+          select
0
+        end
0
+
0
+        # Returns the separator for a given datetime component
0
+        def separator(type)
0
+          case type
0
+            when :month, :day
0
+              @options[:date_separator]
0
+            when :hour
0
+              (@options[:discard_year] && @options[:discard_day]) ? "" : @options[:datetime_separator]
0
+            when :minute
0
+              @options[:time_separator]
0
+            when :second
0
+              @options[:include_seconds] ? @options[:time_separator] : ""
0
+          end
0
         end
0
     end
0
 
0
     class InstanceTag #:nodoc:
0
-      include DateHelper
0
-
0
       def to_date_select_tag(options = {}, html_options = {})
0
-        date_or_time_select(options.merge(:discard_hour => true), html_options)
0
+        datetime_selector(options, html_options).select_date
0
       end
0
 
0
       def to_time_select_tag(options = {}, html_options = {})
0
-        date_or_time_select(options.merge(:discard_year => true, :discard_month => true), html_options)
0
+        datetime_selector(options, html_options).select_time
0
       end
0
 
0
       def to_datetime_select_tag(options = {}, html_options = {})
0
-        date_or_time_select(options, html_options)
0
+        datetime_selector(options, html_options).select_datetime
0
       end
0
 
0
       private
0
-        def date_or_time_select(options, html_options = {})
0
-          locale = options[:locale]
0
-
0
-          defaults = { :discard_type => true }
0
-          options  = defaults.merge(options)
0
-          datetime = value(object)
0
-          datetime ||= default_time_from_options(options[:default]) unless options[:include_blank]
0
-
0
-          position = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 }
0
-
0
-          order = options[:order] ||= I18n.translate(:'date.order', :locale => locale)
0
-
0
-          # Discard explicit and implicit by not being included in the :order
0
-          discard = {}
0
-          discard[:year]   = true if options[:discard_year] or !order.include?(:year)
0
-          discard[:month]  = true if options[:discard_month] or !order.include?(:month)
0
-          discard[:day]    = true if options[:discard_day] or discard[:month] or !order.include?(:day)
0
-          discard[:hour]   = true if options[:discard_hour]
0
-          discard[:minute] = true if options[:discard_minute] or discard[:hour]
0
-          discard[:second] = true unless options[:include_seconds] && !discard[:minute]
0
-
0
-          # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are valid
0
-          # (otherwise it could be 31 and february wouldn't be a valid date)
0
-          if datetime && discard[:day] && !discard[:month]
0
-            datetime = datetime.change(:day => 1)
0
-          end
0
-
0
-          # Maintain valid dates by including hidden fields for discarded elements
0
-          [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }
0
-
0
-          # Ensure proper ordering of :hour, :minute and :second
0
-          [:hour, :minute, :second].each { |o| order.delete(o); order.push(o) }
0
-
0
-          date_or_time_select = ''
0
-          order.reverse.each do |param|
0
-            # Send hidden fields for discarded elements once output has started
0
-            # This ensures AR can reconstruct valid dates using ParseDate
0
-            next if discard[param] && (date_or_time_select.empty? || options[:ignore_date])
0
-
0
-            date_or_time_select.insert(0,
0
-              self.send("select_#{param}",
0
-                datetime,
0
-                options_with_prefix(position[param], options.merge(:use_hidden => discard[param])),
0
-                html_options))
0
-            date_or_time_select.insert(0,
0
-              case param
0
-                when :hour then (discard[:year] && discard[:day] ? "" : " &mdash; ")
0
-                when :minute then " : "
0
-                when :second then options[:include_seconds] ? " : " : ""
0
-                else ""
0
-              end)
0
-          end
0
-
0
-          date_or_time_select
0
+        def datetime_selector(options, html_options)
0
+          datetime = value(object) || default_datetime(options)
0
+
0
+          options = options.dup
0
+          options[:field_name]           = @method_name
0
+          options[:include_position]     = true
0
+          options[:prefix]             ||= @object_name
0
+          options[:index]              ||= @auto_index
0
+          options[:datetime_separator] ||= ' &mdash; '
0
+          options[:time_separator]     ||= ' : '
0
+
0
+          DateTimeSelector.new(datetime, options.merge(:tag => true), html_options)
0
         end
0
 
0
-        def options_with_prefix(position, options)
0
-          prefix = "#{@object_name}"
0
-          if options[:index]
0
-            prefix << "[#{options[:index]}]"
0
-          elsif @auto_index
0
-            prefix << "[#{@auto_index}]"
0
-          end
0
-          options.merge(:prefix => "#{prefix}[#{@method_name}(#{position}i)]")
0
-        end
0
+        def default_datetime(options)
0
+          return if options[:include_blank]
0
 
0
-        def default_time_from_options(default)
0
-          case default
0
+          case options[:default]
0
             when nil
0
               Time.current
0
             when Date, Time
0
-              default
0
+              options[:default]
0
             else
0
+              default = options[:default].dup
0
+
0
               # Rename :minute and :second to :min and :sec
0
               default[:min] ||= default[:minute]
0
               default[:sec] ||= default[:second]
0
@@ -699,8 +876,11 @@ module ActionView
0
                 default[key] ||= time.send(key)
0
               end
0
 
0
-              Time.utc_time(default[:year], default[:month], default[:day], default[:hour], default[:min], default[:sec])
0
-            end
0
+              Time.utc_time(
0
+                default[:year], default[:month], default[:day],
0
+                default[:hour], default[:min], default[:sec]
0
+              )
0
+          end
0
         end
0
     end
0
 
...
3
4
5
6
 
7
8
9
10
 
11
12
13
14
15
16
17
18
19
20
21
 
 
 
 
 
 
22
23
24
...
38
39
40
41
 
42
43
44
...
49
50
51
52
 
53
54
55
56
 
57
58
59
...
3
4
5
 
6
7
8
9
 
10
11
12
13
14
15
 
 
 
 
 
 
16
17
18
19
20
21
22
23
24
...
38
39
40
 
41
42
43
44
...
49
50
51
 
52
53
54
55
 
56
57
58
59
0
@@ -3,22 +3,22 @@ require 'abstract_unit'
0
 class DateHelperDistanceOfTimeInWordsI18nTests < Test::Unit::TestCase
0
   include ActionView::Helpers::DateHelper
0
   attr_reader :request
0
-  
0
+
0
   def setup
0
     @from = Time.mktime(2004, 6, 6, 21, 45, 0)
0
   end
0
-  
0
+
0
   uses_mocha 'date_helper_distance_of_time_in_words_i18n_test' do
0
     # distance_of_time_in_words
0
 
0
     def test_distance_of_time_in_words_calls_i18n
0
       { # with include_seconds
0
-        [2.seconds,  true]  => [:'less_than_x_seconds', 5],   
0
-        [9.seconds,  true]  => [:'less_than_x_seconds', 10],  
0
-        [19.seconds, true]  => [:'less_than_x_seconds', 20],  
0
-        [30.seconds, true]  => [:'half_a_minute',       nil], 
0
-        [59.seconds, true]  => [:'less_than_x_minutes', 1], 
0
-        [60.seconds, true]  => [:'x_minutes',           1], 
0
+        [2.seconds,  true]  => [:'less_than_x_seconds', 5],
0
+        [9.seconds,  true]  => [:'less_than_x_seconds', 10],
0
+        [19.seconds, true]  => [:'less_than_x_seconds', 20],
0
+        [30.seconds, true]  => [:'half_a_minute',       nil],
0
+        [59.seconds, true]  => [:'less_than_x_minutes', 1],
0
+        [60.seconds, true]  => [:'x_minutes',           1],
0
 
0
         # without include_seconds
0
         [29.seconds, false] => [:'less_than_x_minutes', 1],
0
@@ -38,7 +38,7 @@ class DateHelperDistanceOfTimeInWordsI18nTests < Test::Unit::TestCase
0
 
0
     def assert_distance_of_time_in_words_translates_key(passed, expected)
0
       diff, include_seconds = *passed
0
-      key, count = *expected    
0
+      key, count = *expected
0
       to = @from + diff
0
 
0
       options = {:locale => 'en-US', :scope => :'datetime.distance_in_words'}
0
@@ -49,11 +49,11 @@ class DateHelperDistanceOfTimeInWordsI18nTests < Test::Unit::TestCase
0
     end
0
   end
0
 end
0
-  
0
+
0
 class DateHelperSelectTagsI18nTests < Test::Unit::TestCase
0
   include ActionView::Helpers::DateHelper
0
   attr_reader :request
0
-  
0
+
0
   uses_mocha 'date_helper_select_tags_i18n_tests' do
0
     def setup
0
       I18n.stubs(:translate).with(:'date.month_names', :locale => 'en-US').returns Date::MONTHNAMES
...
557
558
559
560
561
562
563
564
 
 
565
566
567
...
569
570
571
 
 
 
 
572
573
574
...
909
910
911
 
 
 
 
912
913
914
...
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
 
 
1014
1015
1016
...
1231
1232
1233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1234
1235
1236
...
1330
1331
1332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1333
1334
1335
...
1412
1413
1414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1415
1416
1417
...
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1837
1838
1839
...
557
558
559
 
 
 
 
 
560
561
562
563
564
...
566
567
568
569
570
571
572
573
574
575
...
910
911
912
913
914
915
916
917
918
919
...
991
992
993
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
994
995
996
997
998
...
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
...
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
...
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
...
1888
1889
1890
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
0
@@ -557,11 +557,8 @@ class DateHelperTest < ActionView::TestCase
0
   end
0
 
0
   def test_select_date_with_incomplete_order
0
-    expected = %(<select id="date_first_day" name="date[first][day]">\n)
0
-    expected << %(<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16" selected="selected">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n)
0
-    expected << "</select>\n"
0
-
0
-    expected <<  %(<select id="date_first_year" name="date[first][year]">\n)
0
+    # NOTE: modified this test because of minimal API change
0
+    expected =  %(<select id="date_first_year" name="date[first][year]">\n)
0
     expected << %(<option value="2003" selected="selected">2003</option>\n<option value="2004">2004</option>\n<option value="2005">2005</option>\n)
0
     expected << "</select>\n"
0
 
0
@@ -569,6 +566,10 @@ class DateHelperTest < ActionView::TestCase
0
     expected << %(<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6">June</option>\n<option value="7">July</option>\n<option value="8" selected="selected">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n)
0
     expected << "</select>\n"
0
 
0
+    expected << %(<select id="date_first_day" name="date[first][day]">\n)
0
+    expected << %(<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16" selected="selected">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n)
0
+    expected << "</select>\n"
0
+
0
     assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), :start_year => 2003, :end_year => 2005, :prefix => "date[first]", :order => [:day])
0
   end
0
 
0
@@ -909,6 +910,10 @@ class DateHelperTest < ActionView::TestCase
0
     assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), { :datetime_separator => "&mdash;", :date_separator => "/", :time_separator => ":", :start_year => 2003, :end_year => 2005, :prefix => "date[first]"}, :class => 'selector')
0
   end
0
 
0
+  def test_select_datetime_should_work_with_date
0
+    assert_nothing_raised { select_datetime(Date.today) }
0
+  end
0
+
0
   def test_select_time
0
     expected = %(<select id="date_hour" name="date[hour]">\n)
0
     expected << %(<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08" selected="selected">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n)
0
@@ -986,31 +991,8 @@ class DateHelperTest < ActionView::TestCase
0
     assert_dom_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), {:include_seconds => false}, :class => 'selector')
0
   end
0
 
0
-  uses_mocha 'TestDatetimeAndTimeSelectUseTimeCurrentAsDefault' do
0
-    def test_select_datetime_uses_time_current_as_default
0
-      time = stub(:year => 2004, :month => 6, :day => 15, :hour => 16, :min => 35, :sec => 0)
0
-      Time.expects(:current).returns time
0
-      expects(:select_date).with(time, anything, anything).returns('')
0
-      expects(:select_time).with(time, anything, anything).returns('')
0
-      select_datetime
0
-    end
0
-
0
-    def test_select_time_uses_time_current_as_default
0
-      time = stub(:year => 2004, :month => 6, :day => 15, :hour => 16, :min => 35, :sec => 0)
0
-      Time.expects(:current).returns time
0
-      expects(:select_hour).with(time, anything, anything).returns('')
0
-      expects(:select_minute).with(time, anything, anything).returns('')
0
-      select_time
0
-    end
0
-
0
-    def test_select_date_uses_date_current_as_default
0
-      date = stub(:year => 2004, :month => 6, :day => 15)
0
-      Date.expects(:current).returns date
0
-      expects(:select_year).with(date, anything, anything).returns('')
0
-      expects(:select_month).with(date, anything, anything).returns('')
0
-      expects(:select_day).with(date, anything, anything).returns('')
0
-      select_date
0
-    end
0
+  def test_select_time_should_work_with_date
0
+    assert_nothing_raised { select_time(Date.today) }
0
   end
0
 
0
   def test_date_select
0
@@ -1231,6 +1213,30 @@ class DateHelperTest < ActionView::TestCase
0
     assert_dom_equal expected, output_buffer
0
   end
0
 
0
+  def test_date_select_with_separator
0
+    @post = Post.new
0
+    @post.written_on = Date.new(2004, 6, 15)
0
+
0
+    expected = %{<select id="post_written_on_1i" name="post[written_on(1i)]">\n}
0
+    expected << %{<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n}
0
+    expected << "</select>\n"
0
+
0
+    expected << " / "
0
+
0
+    expected << %{<select id="post_written_on_2i" name="post[written_on(2i)]">\n}
0
+    expected << %{<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n}
0
+    expected << "</select>\n"
0
+
0
+    expected << " / "
0
+
0
+    expected << %{<select id="post_written_on_3i" name="post[written_on(3i)]">\n}
0
+    expected << %{<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15" selected="selected">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n}
0
+
0
+    expected << "</select>\n"
0
+
0
+    assert_dom_equal expected, date_select("post", "written_on", { :date_separator => " / " })
0
+  end
0
+
0
   def test_time_select
0
     @post = Post.new
0
     @post.written_on = Time.local(2004, 6, 15, 15, 16, 35)
0
@@ -1330,6 +1336,33 @@ class DateHelperTest < ActionView::TestCase
0
     assert_dom_equal expected, output_buffer
0
   end
0
 
0
+  def test_time_select_with_separator
0
+    @post = Post.new
0
+    @post.written_on = Time.local(2004, 6, 15, 15, 16, 35)
0
+
0
+    expected = %{<input type="hidden" id="post_written_on_1i" name="post[written_on(1i)]" value="2004" />\n}
0
+    expected << %{<input type="hidden" id="post_written_on_2i" name="post[written_on(2i)]" value="6" />\n}
0
+    expected << %{<input type="hidden" id="post_written_on_3i" name="post[written_on(3i)]" value="15" />\n}
0
+
0
+    expected << %(<select id="post_written_on_4i" name="post[written_on(4i)]">\n)
0
+    0.upto(23) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 15}>#{sprintf("%02d", i)}</option>\n) }
0
+    expected << "</select>\n"
0
+
0
+    expected << " - "
0
+
0
+    expected << %(<select id="post_written_on_5i" name="post[written_on(5i)]">\n)
0
+    0.upto(59) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 16}>#{sprintf("%02d", i)}</option>\n) }
0
+    expected << "</select>\n"
0
+
0
+    expected << " - "
0
+
0
+    expected << %(<select id="post_written_on_6i" name="post[written_on(6i)]">\n)
0
+    0.upto(59) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 35}>#{sprintf("%02d", i)}</option>\n) }
0
+    expected << "</select>\n"
0
+
0
+    assert_dom_equal expected, time_select("post", "written_on", { :time_separator => " - ", :include_seconds => true })
0
+  end
0
+
0
   def test_datetime_select
0
     @post = Post.new
0
     @post.updated_at = Time.local(2004, 6, 15, 16, 35)
0
@@ -1412,6 +1445,47 @@ class DateHelperTest < ActionView::TestCase
0
     assert_dom_equal expected, output_buffer
0
   end
0
 
0
+  def test_datetime_select_with_separators
0
+    @post = Post.new
0
+    @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35)
0
+
0
+    expected = %{<select id="post_updated_at_1i" name="post[updated_at(1i)]">\n}
0
+    expected << %{<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n}
0
+    expected << "</select>\n"
0
+
0
+    expected << " / "
0
+
0
+    expected << %{<select id="post_updated_at_2i" name="post[updated_at(2i)]">\n}
0
+    expected << %{<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n}
0
+    expected << "</select>\n"
0
+
0
+    expected << " / "
0
+
0
+    expected << %{<select id="post_updated_at_3i" name="post[updated_at(3i)]">\n}
0
+    expected << %{<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15" selected="selected">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n}
0
+    expected << "</select>\n"
0
+
0
+    expected << " , "
0
+
0
+    expected << %(<select id="post_updated_at_4i" name="post[updated_at(4i)]">\n)
0
+    0.upto(23) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 15}>#{sprintf("%02d", i)}</option>\n) }
0
+    expected << "</select>\n"
0
+
0
+    expected << " - "
0
+
0
+    expected << %(<select id="post_updated_at_5i" name="post[updated_at(5i)]">\n)
0
+    0.upto(59) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 16}>#{sprintf("%02d", i)}</option>\n) }
0
+    expected << "</select>\n"
0
+
0
+    expected << " - "
0
+
0
+    expected << %(<select id="post_updated_at_6i" name="post[updated_at(6i)]">\n)
0
+    0.upto(59) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 35}>#{sprintf("%02d", i)}</option>\n) }
0
+    expected << "</select>\n"
0
+
0
+    assert_dom_equal expected, datetime_select("post", "updated_at", { :date_separator => " / ", :datetime_separator => " , ", :time_separator => " - ", :include_seconds => true })
0
+  end
0
+
0
   def test_date_select_with_zero_value_and_no_start_year
0
     expected =  %(<select id="date_first_year" name="date[first][year]">\n)
0
     (Date.today.year-5).upto(Date.today.year+1) { |y| expected << %(<option value="#{y}">#{y}</option>\n) }
0
@@ -1814,26 +1888,151 @@ class DateHelperTest < ActionView::TestCase
0
     assert_dom_equal expected, datetime_select("post", "updated_at", {}, :class => 'selector')
0
   end
0
 
0
-  uses_mocha 'TestInstanceTagDefaultTimeFromOptions' do
0
-    def test_instance_tag_default_time_from_options_uses_time_current_as_default_when_hash_passed_as_arg
0
-      dummy_instance_tag = ActionView::Helpers::InstanceTag.new(1,2,3)
0
-      Time.expects(:current).returns Time.now
0
-      dummy_instance_tag.send!(:default_time_from_options, :hour => 2)
0
-    end
0
-
0
-    def test_instance_tag_default_time_from_options_respects_hash_arg_settings_when_time_falls_in_system_local_dst_spring_gap
0
-      with_env_tz('US/Central') do
0
-        dummy_instance_tag = ActionView::Helpers::InstanceTag.new(1,2,3)
0
-        Time.stubs(:now).returns Time.local(2006, 4, 2, 1)
0
-        assert_equal 2, dummy_instance_tag.send!(:default_time_from_options, :hour => 2).hour
0
-      end
0
-    end
0
-
0
-    def test_instance_tag_default_time_from_options_handles_far_future_date
0
-      dummy_instance_tag = ActionView::Helpers::InstanceTag.new(1,2,3)
0
-      time = dummy_instance_tag.send!(:default_time_from_options, :year => 2050, :month => 2, :day => 10, :hour => 15, :min => 30, :sec => 45)
0
-      assert_equal 2050, time.year
0
-    end
0
+  def test_date_select_should_not_change_passed_options_hash
0
+    @post = Post.new
0
+    @post.updated_at = Time.local(2008, 7, 16, 23, 30)
0
+
0
+    options = { 
0
+      :order => [ :year, :month, :day ],
0
+      :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
0
+      :discard_type => false,
0
+      :include_blank => false,
0
+      :ignore_date => false,
0
+      :include_seconds => true
0
+    }
0
+    date_select(@post, :updated_at, options)
0
+
0
+    # note: the literal hash is intentional to show that the actual options hash isn't modified
0
+    #       don't change this!
0
+    assert_equal({ 
0
+      :order => [ :year, :month, :day ],
0
+      :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
0
+      :discard_type => false,
0
+      :include_blank => false,
0
+      :ignore_date => false,
0
+      :include_seconds => true
0
+    }, options)
0
+  end
0
+
0
+  def test_datetime_select_should_not_change_passed_options_hash
0
+    @post = Post.new
0
+    @post.updated_at = Time.local(2008, 7, 16, 23, 30)
0
+
0
+    options = { 
0
+      :order => [ :year, :month, :day ],
0
+      :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
0
+      :discard_type => false,
0
+      :include_blank => false,
0
+      :ignore_date => false,
0
+      :include_seconds => true
0
+    }
0
+    datetime_select(@post, :updated_at, options)
0
+
0
+    # note: the literal hash is intentional to show that the actual options hash isn't modified
0
+    #       don't change this!
0
+    assert_equal({ 
0
+      :order => [ :year, :month, :day ],
0
+      :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
0
+      :discard_type => false,
0
+      :include_blank => false,
0
+      :ignore_date => false,
0
+      :include_seconds => true
0
+    }, options)
0
+  end
0
+
0
+  def test_time_select_should_not_change_passed_options_hash
0
+    @post = Post.new
0
+    @post.updated_at = Time.local(2008, 7, 16, 23, 30)
0
+
0
+    options = { 
0
+      :order => [ :year, :month, :day ],
0
+      :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
0
+      :discard_type => false,
0
+      :include_blank => false,
0
+      :ignore_date => false,
0
+      :include_seconds => true
0
+    }
0
+    time_select(@post, :updated_at, options)
0
+
0
+    # note: the literal hash is intentional to show that the actual options hash isn't modified
0
+    #       don't change this!
0
+    assert_equal({ 
0
+      :order => [ :year, :month, :day ],
0
+      :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
0
+      :discard_type => false,
0
+      :include_blank => false,
0
+      :ignore_date => false,
0
+      :include_seconds => true
0
+    }, options)
0
+  end
0
+
0
+  def test_select_date_should_not_change_passed_options_hash
0
+    options = { 
0
+      :order => [ :year, :month, :day ],
0
+      :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
0
+      :discard_type => false,
0
+      :include_blank => false,
0
+      :ignore_date => false,
0
+      :include_seconds => true
0
+    }
0
+    select_date(Date.today, options)
0
+
0
+    # note: the literal hash is intentional to show that the actual options hash isn't modified
0
+    #       don't change this!
0
+    assert_equal({ 
0
+      :order => [ :year, :month, :day ],
0
+      :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
0
+      :discard_type => false,
0
+      :include_blank => false,
0
+      :ignore_date => false,
0
+      :include_seconds => true
0
+    }, options)
0
+  end
0
+
0
+  def test_select_datetime_should_not_change_passed_options_hash
0
+    options = { 
0
+      :order => [ :year, :month, :day ],
0
+      :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
0
+      :discard_type => false,
0
+      :include_blank => false,
0
+      :ignore_date => false,
0
+      :include_seconds => true
0
+    }
0
+    select_datetime(Time.now, options)
0
+
0
+    # note: the literal hash is intentional to show that the actual options hash isn't modified
0
+    #       don't change this!
0
+    assert_equal({ 
0
+      :order => [ :year, :month, :day ],
0
+      :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
0
+      :discard_type => false,
0
+      :include_blank => false,
0
+      :ignore_date => false,
0
+      :include_seconds => true
0
+    }, options)
0
+  end
0
+
0
+  def test_select_time_should_not_change_passed_options_hash
0
+    options = { 
0
+      :order => [ :year, :month, :day ],
0
+      :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
0
+      :discard_type => false,
0
+      :include_blank => false,
0
+      :ignore_date => false,
0
+      :include_seconds => true
0
+    }
0
+    select_time(Time.now, options)
0
+
0
+    # note: the literal hash is intentional to show that the actual options hash isn't modified
0
+    #       don't change this!
0
+    assert_equal({ 
0
+      :order => [ :year, :month, :day ],
0
+      :default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
0
+      :discard_type => false,
0
+      :include_blank => false,
0
+      :ignore_date => false,
0
+      :include_seconds => true
0
+    }, options)
0
   end
0
 
0
   protected

Comments