mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Extract DatetimeSelect
This commit is contained in:
parent
b69e449c51
commit
f6fc352927
4 changed files with 11 additions and 54 deletions
|
@ -16,7 +16,7 @@ module ActionView
|
|||
end
|
||||
end
|
||||
|
||||
%w(content_tag to_datetime_select_tag).each do |meth|
|
||||
%w(content_tag).each do |meth|
|
||||
module_eval "def #{meth}(*) error_wrapping(super) end", __FILE__, __LINE__
|
||||
end
|
||||
|
||||
|
|
|
@ -287,7 +287,7 @@ module ActionView
|
|||
#
|
||||
# The selects are prepared for multi-parameter assignment to an Active Record object.
|
||||
def datetime_select(object_name, method, options = {}, html_options = {})
|
||||
InstanceTag.new(object_name, method, self, options.delete(:object)).to_datetime_select_tag(options, html_options)
|
||||
ActionView::Helpers::Tags::DatetimeSelect.new(object_name, method, self, options, html_options).render
|
||||
end
|
||||
|
||||
# Returns a set of html select-tags (one for year, month, day, hour, minute, and second) pre-selected with the
|
||||
|
@ -974,58 +974,6 @@ module ActionView
|
|||
end
|
||||
end
|
||||
|
||||
module DateHelperInstanceTag
|
||||
def to_datetime_select_tag(options = {}, html_options = {})
|
||||
datetime_selector(options, html_options).select_datetime.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
|
||||
|
||||
class InstanceTag #:nodoc:
|
||||
include DateHelperInstanceTag
|
||||
end
|
||||
|
||||
class FormBuilder
|
||||
def date_select(method, options = {}, html_options = {})
|
||||
@template.date_select(@object_name, method, objectify_options(options), html_options)
|
||||
|
|
|
@ -22,6 +22,7 @@ module ActionView
|
|||
autoload :TimeZoneSelect, 'action_view/helpers/tags/time_zone_select'
|
||||
autoload :DateSelect, 'action_view/helpers/tags/date_select'
|
||||
autoload :TimeSelect, 'action_view/helpers/tags/time_select'
|
||||
autoload :DatetimeSelect, 'action_view/helpers/tags/datetime_select'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
module ActionView
|
||||
module Helpers
|
||||
module Tags
|
||||
class DatetimeSelect < DateSelect #:nodoc:
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue