diff options
author | Xavier Noria <fxn@hashref.com> | 2012-01-17 14:08:09 -0800 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2012-01-17 14:08:09 -0800 |
commit | 57aaaa61977e82b9de2c43c26b00e636030685c4 (patch) | |
tree | 493351f06c1e8bc9fe5131015e72a963f9f2806d /actionpack/lib/action_view/helpers/tags/date_select.rb | |
parent | ba154bd76fb481d9faca123e988924bd864c0318 (diff) | |
parent | 6f1bf526d7f1869b47f6047c4285c673bb06d0ec (diff) | |
download | rails-57aaaa61977e82b9de2c43c26b00e636030685c4.tar.gz rails-57aaaa61977e82b9de2c43c26b00e636030685c4.tar.bz2 rails-57aaaa61977e82b9de2c43c26b00e636030685c4.zip |
Merge pull request #4488 from rafaelfranca/av-refactor
ActionView::Helpers::FormHelper refactoring
Diffstat (limited to 'actionpack/lib/action_view/helpers/tags/date_select.rb')
-rw-r--r-- | actionpack/lib/action_view/helpers/tags/date_select.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/helpers/tags/date_select.rb b/actionpack/lib/action_view/helpers/tags/date_select.rb new file mode 100644 index 0000000000..5912598ca1 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/date_select.rb @@ -0,0 +1,64 @@ +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).send("select_#{select_type}").html_safe) + end + + private + + def select_type + self.class.name.split("::").last.sub("Select", "").downcase + end + + 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 |