Skip to content

Commit

Permalink
Extract DateSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Jan 17, 2012
1 parent 2a86c14 commit e70f68f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/active_model_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def object
end
end

%w(content_tag to_date_select_tag to_datetime_select_tag to_time_select_tag).each do |meth|
%w(content_tag to_datetime_select_tag to_time_select_tag).each do |meth|
module_eval "def #{meth}(*) error_wrapping(super) end", __FILE__, __LINE__
end

Expand Down
6 changes: 1 addition & 5 deletions actionpack/lib/action_view/helpers/date_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def time_ago_in_words(from_time, include_seconds = false)
# 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
# all month choices are valid.
def date_select(object_name, method, options = {}, html_options = {})
InstanceTag.new(object_name, method, self, options.delete(:object)).to_date_select_tag(options, html_options)
ActionView::Helpers::Tags::DateSelect.new(object_name, method, self, options, html_options).render
end

# Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a
Expand Down Expand Up @@ -975,10 +975,6 @@ def separator(type)
end

module DateHelperInstanceTag
def to_date_select_tag(options = {}, html_options = {})
datetime_selector(options, html_options).select_date.html_safe
end

def to_time_select_tag(options = {}, html_options = {})
datetime_selector(options, html_options).select_time.html_safe
end
Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_view/helpers/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Tags
autoload :CollectionSelect, 'action_view/helpers/tags/collection_select'
autoload :GroupedCollectionSelect, 'action_view/helpers/tags/grouped_collection_select'
autoload :TimeZoneSelect, 'action_view/helpers/tags/time_zone_select'
autoload :DateSelect, 'action_view/helpers/tags/date_select'
end
end
end
60 changes: 60 additions & 0 deletions actionpack/lib/action_view/helpers/tags/date_select.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module ActionView
module Helpers
module Tags
class DateSelect < Base #:nodoc:
def initialize(object_name, method_name, template_object, options, html_options)
@html_options = html_options

super(object_name, method_name, template_object, options)
end

def render
error_wrapping(datetime_selector(@options, @html_options).select_date.html_safe)
end

private

def datetime_selector(options, html_options)
datetime = value(object) || default_datetime(options)
@auto_index ||= nil

options = options.dup
options[:field_name] = @method_name
options[:include_position] = true
options[:prefix] ||= @object_name
options[:index] = @auto_index if @auto_index && !options.has_key?(:index)

DateTimeSelector.new(datetime, options, html_options)
end

def default_datetime(options)
return if options[:include_blank] || options[:prompt]

case options[:default]
when nil
Time.current
when Date, Time
options[:default]
else
default = options[:default].dup

# Rename :minute and :second to :min and :sec
default[:min] ||= default[:minute]
default[:sec] ||= default[:second]

time = Time.current

[:year, :month, :day, :hour, :min, :sec].each do |key|
default[key] ||= time.send(key)
end

Time.utc_time(
default[:year], default[:month], default[:day],
default[:hour], default[:min], default[:sec]
)
end
end
end
end
end
end

0 comments on commit e70f68f

Please sign in to comment.