Skip to content
This repository
Browse code

Extract DateSelect

  • Loading branch information...
commit e70f68fd1ebcb35a9f82b0b3c5a6ea14a4e253cc 1 parent 2a86c14
Rafael Mendonça França authored
2  actionpack/lib/action_view/helpers/active_model_helper.rb
@@ -16,7 +16,7 @@ def object
16 16
         end
17 17
       end
18 18
 
19  
-      %w(content_tag to_date_select_tag to_datetime_select_tag to_time_select_tag).each do |meth|
  19
+      %w(content_tag to_datetime_select_tag to_time_select_tag).each do |meth|
20 20
         module_eval "def #{meth}(*) error_wrapping(super) end", __FILE__, __LINE__
21 21
       end
22 22
 
6  actionpack/lib/action_view/helpers/date_helper.rb
@@ -213,7 +213,7 @@ def time_ago_in_words(from_time, include_seconds = false)
213 213
       # Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that
214 214
       # all month choices are valid.
215 215
       def date_select(object_name, method, options = {}, html_options = {})
216  
-        InstanceTag.new(object_name, method, self, options.delete(:object)).to_date_select_tag(options, html_options)
  216
+        ActionView::Helpers::Tags::DateSelect.new(object_name, method, self, options, html_options).render
217 217
       end
218 218
 
219 219
       # Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a
@@ -975,10 +975,6 @@ def separator(type)
975 975
     end
976 976
 
977 977
     module DateHelperInstanceTag
978  
-      def to_date_select_tag(options = {}, html_options = {})
979  
-        datetime_selector(options, html_options).select_date.html_safe
980  
-      end
981  
-
982 978
       def to_time_select_tag(options = {}, html_options = {})
983 979
         datetime_selector(options, html_options).select_time.html_safe
984 980
       end
1  actionpack/lib/action_view/helpers/tags.rb
@@ -20,6 +20,7 @@ module Tags
20 20
       autoload :CollectionSelect,        'action_view/helpers/tags/collection_select'
21 21
       autoload :GroupedCollectionSelect, 'action_view/helpers/tags/grouped_collection_select'
22 22
       autoload :TimeZoneSelect,          'action_view/helpers/tags/time_zone_select'
  23
+      autoload :DateSelect,              'action_view/helpers/tags/date_select'
23 24
     end
24 25
   end
25 26
 end
60  actionpack/lib/action_view/helpers/tags/date_select.rb
... ...
@@ -0,0 +1,60 @@
  1
+module ActionView
  2
+  module Helpers
  3
+    module Tags
  4
+      class DateSelect < Base #:nodoc:
  5
+        def initialize(object_name, method_name, template_object, options, html_options)
  6
+          @html_options = html_options
  7
+
  8
+          super(object_name, method_name, template_object, options)
  9
+        end
  10
+
  11
+        def render
  12
+          error_wrapping(datetime_selector(@options, @html_options).select_date.html_safe)
  13
+        end
  14
+
  15
+        private
  16
+
  17
+        def datetime_selector(options, html_options)
  18
+          datetime = value(object) || default_datetime(options)
  19
+          @auto_index ||= nil
  20
+
  21
+          options = options.dup
  22
+          options[:field_name]           = @method_name
  23
+          options[:include_position]     = true
  24
+          options[:prefix]             ||= @object_name
  25
+          options[:index]                = @auto_index if @auto_index && !options.has_key?(:index)
  26
+
  27
+          DateTimeSelector.new(datetime, options, html_options)
  28
+        end
  29
+
  30
+        def default_datetime(options)
  31
+          return if options[:include_blank] || options[:prompt]
  32
+
  33
+          case options[:default]
  34
+          when nil
  35
+            Time.current
  36
+          when Date, Time
  37
+            options[:default]
  38
+          else
  39
+            default = options[:default].dup
  40
+
  41
+            # Rename :minute and :second to :min and :sec
  42
+            default[:min] ||= default[:minute]
  43
+            default[:sec] ||= default[:second]
  44
+
  45
+            time = Time.current
  46
+
  47
+            [:year, :month, :day, :hour, :min, :sec].each do |key|
  48
+              default[key] ||= time.send(key)
  49
+            end
  50
+
  51
+            Time.utc_time(
  52
+              default[:year], default[:month], default[:day],
  53
+              default[:hour], default[:min], default[:sec]
  54
+            )
  55
+          end
  56
+        end
  57
+      end
  58
+    end
  59
+  end
  60
+end

0 notes on commit e70f68f

Please sign in to comment.
Something went wrong with that request. Please try again.