aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/CHANGELOG2
-rwxr-xr-xactionpack/lib/action_view/helpers/date_helper.rb2
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb82
-rw-r--r--actionpack/lib/action_view/helpers/form_options_helper.rb4
4 files changed, 61 insertions, 29 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index ee0ea8d2e6..b35eb80978 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Refactor various InstanceTag instance method to class methods. Closes #4800. [skaes@web.de]
+
* Remove all remaining references to @params in the documentation. [Marcel Molina Jr.]
* Add documentation for redirect_to :back's RedirectBackError exception. [Marcel Molina Jr.]
diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb
index 0040095669..4920763b50 100755
--- a/actionpack/lib/action_view/helpers/date_helper.rb
+++ b/actionpack/lib/action_view/helpers/date_helper.rb
@@ -258,6 +258,7 @@ module ActionView
defaults = { :discard_type => true }
options = defaults.merge(options)
options_with_prefix = Proc.new { |position| options.merge(:prefix => "#{@object_name}[#{@method_name}(#{position}i)]") }
+ value = value(object)
date = options[:include_blank] ? (value || 0) : (value || Date.today)
date_select = ''
@@ -282,6 +283,7 @@ module ActionView
defaults = { :discard_type => true }
options = defaults.merge(options)
options_with_prefix = Proc.new { |position| options.merge(:prefix => "#{@object_name}[#{@method_name}(#{position}i)]") }
+ value = value(object)
datetime = options[:include_blank] ? (value || nil) : (value || Time.now)
datetime_select = select_year(datetime, options_with_prefix.call(1))
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 7c8748d604..fd4fe5f12e 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -250,7 +250,7 @@ module ActionView
options.delete("size")
end
options["type"] = field_type
- options["value"] ||= value_before_type_cast unless field_type == "file"
+ options["value"] ||= value_before_type_cast(object) unless field_type == "file"
add_default_name_and_id(options)
tag("input", options)
end
@@ -259,7 +259,13 @@ module ActionView
options = DEFAULT_RADIO_OPTIONS.merge(options.stringify_keys)
options["type"] = "radio"
options["value"] = tag_value
- options["checked"] = "checked" if value.to_s == tag_value.to_s
+ if options.has_key?("checked")
+ cv = options.delete "checked"
+ checked = cv == true || cv == "checked"
+ else
+ checked = self.class.radio_button_checked?(value(object), tag_value)
+ end
+ options["checked"] = "checked" if checked
pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase
options["id"] = @auto_index ?
"#{@object_name}_#{@auto_index}_#{@method_name}_#{pretty_tag_value}" :
@@ -271,37 +277,27 @@ module ActionView
def to_text_area_tag(options = {})
options = DEFAULT_TEXT_AREA_OPTIONS.merge(options.stringify_keys)
add_default_name_and_id(options)
- content_tag("textarea", html_escape(options.delete('value') || value_before_type_cast), options)
+ content_tag("textarea", html_escape(options.delete('value') || value_before_type_cast(object)), options)
end
def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0")
options = options.stringify_keys
options["type"] = "checkbox"
options["value"] = checked_value
- checked = case value
- when TrueClass, FalseClass
- value
- when NilClass
- false
- when Integer
- value != 0
- when String
- value == checked_value
- else
- value.to_i != 0
- end
- if checked || options["checked"] == "checked"
- options["checked"] = "checked"
+ if options.has_key?("checked")
+ cv = options.delete "checked"
+ checked = cv == true || cv == "checked"
else
- options.delete("checked")
+ checked = self.class.check_box_checked?(value(object), checked_value)
end
+ options["checked"] = "checked" if checked
add_default_name_and_id(options)
tag("input", options) << tag("input", "name" => options["name"], "type" => "hidden", "value" => unchecked_value)
end
def to_date_tag()
defaults = DEFAULT_DATE_OPTIONS.dup
- date = value || Date.today
+ date = value(object) || Date.today
options = Proc.new { |position| defaults.merge(:prefix => "#{@object_name}[#{@method_name}(#{position}i)]") }
html_day_select(date, options.call(3)) +
html_month_select(date, options.call(2)) +
@@ -311,6 +307,7 @@ module ActionView
def to_boolean_select_tag(options = {})
options = options.stringify_keys
add_default_name_and_id(options)
+ value = value(object)
tag_text = "<select"
tag_text << tag_options(options)
tag_text << "><option value=\"false\""
@@ -321,24 +318,51 @@ module ActionView
end
def to_content_tag(tag_name, options = {})
- content_tag(tag_name, value, options)
+ content_tag(tag_name, value(object), options)
end
def object
@object || @template_object.instance_variable_get("@#{@object_name}")
end
- def value
- unless object.nil?
- object.send(@method_name)
- end
+ def value(object)
+ self.class.value(object, @method_name)
end
- def value_before_type_cast
- unless object.nil?
- object.respond_to?(@method_name + "_before_type_cast") ?
- object.send(@method_name + "_before_type_cast") :
- object.send(@method_name)
+ def value_before_type_cast(object)
+ self.class.value_before_type_cast(object, @method_name)
+ end
+
+ class << self
+ def value(object, method_name)
+ object.send method_name unless object.nil?
+ end
+
+ def value_before_type_cast(object, method_name)
+ unless object.nil?
+ object.respond_to?(method_name + "_before_type_cast") ?
+ object.send(method_name + "_before_type_cast") :
+ object.send(method_name)
+ end
+ end
+
+ def check_box_checked?(value, checked_value)
+ case value
+ when TrueClass, FalseClass
+ value
+ when NilClass
+ false
+ when Integer
+ value != 0
+ when String
+ value == checked_value
+ else
+ value.to_i != 0
+ end
+ end
+
+ def radio_button_checked?(value, checked_value)
+ value.to_s == checked_value.to_s
end
end
diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb
index 53b39305fa..4c830afb02 100644
--- a/actionpack/lib/action_view/helpers/form_options_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -299,6 +299,7 @@ module ActionView
def to_select_tag(choices, options, html_options)
html_options = html_options.stringify_keys
add_default_name_and_id(html_options)
+ value = value(object)
selected_value = options.has_key?(:selected) ? options[:selected] : value
content_tag("select", add_options(options_for_select(choices, selected_value), options, value), html_options)
end
@@ -306,6 +307,7 @@ module ActionView
def to_collection_select_tag(collection, value_method, text_method, options, html_options)
html_options = html_options.stringify_keys
add_default_name_and_id(html_options)
+ value = value(object)
content_tag(
"select", add_options(options_from_collection_for_select(collection, value_method, text_method, value), options, value), html_options
)
@@ -314,12 +316,14 @@ module ActionView
def to_country_select_tag(priority_countries, options, html_options)
html_options = html_options.stringify_keys
add_default_name_and_id(html_options)
+ value = value(object)
content_tag("select", add_options(country_options_for_select(value, priority_countries), options, value), html_options)
end
def to_time_zone_select_tag(priority_zones, options, html_options)
html_options = html_options.stringify_keys
add_default_name_and_id(html_options)
+ value = value(object)
content_tag("select",
add_options(
time_zone_options_for_select(value, priority_zones, options[:model] || TimeZone),