Skip to content

Commit

Permalink
Extract DatetimeSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Jan 17, 2012
1 parent b69e449 commit f6fc352
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 54 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_datetime_select_tag).each do |meth|
%w(content_tag).each do |meth|
module_eval "def #{meth}(*) error_wrapping(super) end", __FILE__, __LINE__
end

Expand Down
54 changes: 1 addition & 53 deletions actionpack/lib/action_view/helpers/date_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def time_select(object_name, method, options = {}, html_options = {})
#
# 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
Expand Down Expand Up @@ -974,58 +974,6 @@ def separator(type)
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)
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 @@ -22,6 +22,7 @@ module Tags
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
8 changes: 8 additions & 0 deletions actionpack/lib/action_view/helpers/tags/datetime_select.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module ActionView
module Helpers
module Tags
class DatetimeSelect < DateSelect #:nodoc:
end
end
end
end

0 comments on commit f6fc352

Please sign in to comment.