From e749fb5826d9f119847d2e1d5ec2aff30b00632e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 17:19:07 -0300 Subject: Extract LabelTag --- .../lib/action_view/helpers/tags/label_tag.rb | 147 +++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/label_tag.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/label_tag.rb b/actionpack/lib/action_view/helpers/tags/label_tag.rb new file mode 100644 index 0000000000..0141f0ab10 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/label_tag.rb @@ -0,0 +1,147 @@ +module ActionView + module Helpers + module Tags + class LabelTag + include Helpers::TagHelper, Helpers::FormTagHelper + + attr_reader :object + + def initialize(object_name, method_name, template_object, content, options) + content_is_options = content.is_a?(Hash) + if content_is_options + options = content + @content = nil + else + @content = content + end + + options ||= {} + + @object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup + @template_object = template_object + + @object_name.sub!(/\[\]$/,"") || @object_name.sub!(/\[\]\]$/,"]") + @object = retrieve_object(options.delete(:object)) + @options = options + @auto_index = retrieve_autoindex(Regexp.last_match.pre_match) if Regexp.last_match + end + + def render(&block) + options = @options.stringify_keys + tag_value = options.delete("value") + name_and_id = options.dup + + if name_and_id["for"] + name_and_id["id"] = name_and_id["for"] + else + name_and_id.delete("id") + end + + add_default_name_and_id_for_value(tag_value, name_and_id) + options.delete("index") + options.delete("namespace") + options["for"] ||= name_and_id["id"] + + if block_given? + @template_object.label_tag(name_and_id["id"], options, &block) + else + content = if @content.blank? + @object_name.gsub!(/\[(.*)_attributes\]\[\d\]/, '.\1') + method_and_value = tag_value.present? ? "#{@method_name}.#{tag_value}" : @method_name + + if object.respond_to?(:to_model) + key = object.class.model_name.i18n_key + i18n_default = ["#{key}.#{method_and_value}".to_sym, ""] + end + + i18n_default ||= "" + I18n.t("#{@object_name}.#{method_and_value}", :default => i18n_default, :scope => "helpers.label").presence + else + @content.to_s + end + + content ||= if object && object.class.respond_to?(:human_attribute_name) + object.class.human_attribute_name(@method_name) + end + + content ||= @method_name.humanize + + label_tag(name_and_id["id"], content, options) + end + end + + private + + def retrieve_object(object) + if object + object + elsif @template_object.instance_variable_defined?("@#{@object_name}") + @template_object.instance_variable_get("@#{@object_name}") + end + rescue NameError + # As @object_name may contain the nested syntax (item[subobject]) we need to fallback to nil. + nil + end + + def retrieve_autoindex(pre_match) + object = self.object || @template_object.instance_variable_get("@#{pre_match}") + if object && object.respond_to?(:to_param) + object.to_param + else + raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}" + end + end + + def add_default_name_and_id_for_value(tag_value, options) + unless tag_value.nil? + pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase + specified_id = options["id"] + add_default_name_and_id(options) + options["id"] += "_#{pretty_tag_value}" if specified_id.blank? && options["id"].present? + else + add_default_name_and_id(options) + end + end + + def add_default_name_and_id(options) + if options.has_key?("index") + options["name"] ||= tag_name_with_index(options["index"]) + options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) } + options.delete("index") + elsif defined?(@auto_index) + options["name"] ||= tag_name_with_index(@auto_index) + options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) } + else + options["name"] ||= tag_name + (options['multiple'] ? '[]' : '') + options["id"] = options.fetch("id"){ tag_id } + end + options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence + end + + def tag_name + "#{@object_name}[#{sanitized_method_name}]" + end + + def tag_name_with_index(index) + "#{@object_name}[#{index}][#{sanitized_method_name}]" + end + + def tag_id + "#{sanitized_object_name}_#{sanitized_method_name}" + end + + def tag_id_with_index(index) + "#{sanitized_object_name}_#{index}_#{sanitized_method_name}" + end + + def sanitized_object_name + @sanitized_object_name ||= @object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "") + end + + def sanitized_method_name + @sanitized_method_name ||= @method_name.sub(/\?$/,"") + end + end + end + end +end -- cgit v1.2.3 From 520fe7defea2271cc540e8190a7b9d83d8dfb8d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 17:41:24 -0300 Subject: Extract TextFieldTag --- .../lib/action_view/helpers/tags/text_field_tag.rb | 114 +++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/text_field_tag.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/text_field_tag.rb b/actionpack/lib/action_view/helpers/tags/text_field_tag.rb new file mode 100644 index 0000000000..996a153cf8 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/text_field_tag.rb @@ -0,0 +1,114 @@ +module ActionView + module Helpers + module Tags + class TextFieldTag + include Helpers::ActiveModelInstanceTag, Helpers::TagHelper, Helpers::FormTagHelper + + DEFAULT_FIELD_OPTIONS = { "size" => 30 } + + attr_reader :object + + def initialize(object_name, method_name, template_object, options = {}) + @object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup + @template_object = template_object + + @object_name.sub!(/\[\]$/,"") || @object_name.sub!(/\[\]\]$/,"]") + @object = retrieve_object(options.delete(:object)) + @options = options + @auto_index = retrieve_autoindex(Regexp.last_match.pre_match) if Regexp.last_match + end + + def render(&block) + options = @options.stringify_keys + options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size") + options = DEFAULT_FIELD_OPTIONS.merge(options) + options["type"] ||= "text" + options["value"] = options.fetch("value"){ value_before_type_cast(object) } + options["value"] &&= ERB::Util.html_escape(options["value"]) + add_default_name_and_id(options) + tag("input", options) + end + + private + + def value_before_type_cast(object) + 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 retrieve_object(object) + if object + object + elsif @template_object.instance_variable_defined?("@#{@object_name}") + @template_object.instance_variable_get("@#{@object_name}") + end + rescue NameError + # As @object_name may contain the nested syntax (item[subobject]) we need to fallback to nil. + nil + end + + def retrieve_autoindex(pre_match) + object = self.object || @template_object.instance_variable_get("@#{pre_match}") + if object && object.respond_to?(:to_param) + object.to_param + else + raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}" + end + end + + def add_default_name_and_id_for_value(tag_value, options) + unless tag_value.nil? + pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase + specified_id = options["id"] + add_default_name_and_id(options) + options["id"] += "_#{pretty_tag_value}" if specified_id.blank? && options["id"].present? + else + add_default_name_and_id(options) + end + end + + def add_default_name_and_id(options) + if options.has_key?("index") + options["name"] ||= tag_name_with_index(options["index"]) + options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) } + options.delete("index") + elsif defined?(@auto_index) + options["name"] ||= tag_name_with_index(@auto_index) + options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) } + else + options["name"] ||= tag_name + (options['multiple'] ? '[]' : '') + options["id"] = options.fetch("id"){ tag_id } + end + options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence + end + + def tag_name + "#{@object_name}[#{sanitized_method_name}]" + end + + def tag_name_with_index(index) + "#{@object_name}[#{index}][#{sanitized_method_name}]" + end + + def tag_id + "#{sanitized_object_name}_#{sanitized_method_name}" + end + + def tag_id_with_index(index) + "#{sanitized_object_name}_#{index}_#{sanitized_method_name}" + end + + def sanitized_object_name + @sanitized_object_name ||= @object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "") + end + + def sanitized_method_name + @sanitized_method_name ||= @method_name.sub(/\?$/,"") + end + end + end + end +end -- cgit v1.2.3 From 801dcb0a391a2145a38a7eb8a3879f59a654a825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 17:49:39 -0300 Subject: Extract duplicated code to a class --- .../lib/action_view/helpers/tags/base_tag.rb | 107 +++++++++++++++++++++ .../lib/action_view/helpers/tags/label_tag.rb | 94 ++---------------- .../lib/action_view/helpers/tags/text_field_tag.rb | 100 +------------------ 3 files changed, 115 insertions(+), 186 deletions(-) create mode 100644 actionpack/lib/action_view/helpers/tags/base_tag.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/base_tag.rb b/actionpack/lib/action_view/helpers/tags/base_tag.rb new file mode 100644 index 0000000000..b9cb5f1a93 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/base_tag.rb @@ -0,0 +1,107 @@ +module ActionView + module Helpers + module Tags + class BaseTag + include Helpers::ActiveModelInstanceTag, Helpers::TagHelper, Helpers::FormTagHelper + + DEFAULT_FIELD_OPTIONS = { "size" => 30 } + + attr_reader :object + + def initialize(object_name, method_name, template_object, options = {}) + @object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup + @template_object = template_object + + @object_name.sub!(/\[\]$/,"") || @object_name.sub!(/\[\]\]$/,"]") + @object = retrieve_object(options.delete(:object)) + @options = options + @auto_index = retrieve_autoindex(Regexp.last_match.pre_match) if Regexp.last_match + end + + def render(&block) + raise "Abstract Method called" + end + + private + + def value_before_type_cast(object) + 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 retrieve_object(object) + if object + object + elsif @template_object.instance_variable_defined?("@#{@object_name}") + @template_object.instance_variable_get("@#{@object_name}") + end + rescue NameError + # As @object_name may contain the nested syntax (item[subobject]) we need to fallback to nil. + nil + end + + def retrieve_autoindex(pre_match) + object = self.object || @template_object.instance_variable_get("@#{pre_match}") + if object && object.respond_to?(:to_param) + object.to_param + else + raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}" + end + end + + def add_default_name_and_id_for_value(tag_value, options) + unless tag_value.nil? + pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase + specified_id = options["id"] + add_default_name_and_id(options) + options["id"] += "_#{pretty_tag_value}" if specified_id.blank? && options["id"].present? + else + add_default_name_and_id(options) + end + end + + def add_default_name_and_id(options) + if options.has_key?("index") + options["name"] ||= tag_name_with_index(options["index"]) + options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) } + options.delete("index") + elsif defined?(@auto_index) + options["name"] ||= tag_name_with_index(@auto_index) + options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) } + else + options["name"] ||= tag_name + (options['multiple'] ? '[]' : '') + options["id"] = options.fetch("id"){ tag_id } + end + options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence + end + + def tag_name + "#{@object_name}[#{sanitized_method_name}]" + end + + def tag_name_with_index(index) + "#{@object_name}[#{index}][#{sanitized_method_name}]" + end + + def tag_id + "#{sanitized_object_name}_#{sanitized_method_name}" + end + + def tag_id_with_index(index) + "#{sanitized_object_name}_#{index}_#{sanitized_method_name}" + end + + def sanitized_object_name + @sanitized_object_name ||= @object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "") + end + + def sanitized_method_name + @sanitized_method_name ||= @method_name.sub(/\?$/,"") + end + end + end + end +end diff --git a/actionpack/lib/action_view/helpers/tags/label_tag.rb b/actionpack/lib/action_view/helpers/tags/label_tag.rb index 0141f0ab10..8a7b15de59 100644 --- a/actionpack/lib/action_view/helpers/tags/label_tag.rb +++ b/actionpack/lib/action_view/helpers/tags/label_tag.rb @@ -1,29 +1,19 @@ module ActionView module Helpers module Tags - class LabelTag - include Helpers::TagHelper, Helpers::FormTagHelper - - attr_reader :object - - def initialize(object_name, method_name, template_object, content, options) - content_is_options = content.is_a?(Hash) + class LabelTag < BaseTag + def initialize(object_name, method_name, template_object, content_or_options = nil, options = nil) + content_is_options = content_or_options.is_a?(Hash) if content_is_options - options = content + options = content_or_options @content = nil else - @content = content + @content = content_or_options end options ||= {} - @object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup - @template_object = template_object - - @object_name.sub!(/\[\]$/,"") || @object_name.sub!(/\[\]\]$/,"]") - @object = retrieve_object(options.delete(:object)) - @options = options - @auto_index = retrieve_autoindex(Regexp.last_match.pre_match) if Regexp.last_match + super(object_name, method_name, template_object, options) end def render(&block) @@ -69,78 +59,6 @@ module ActionView label_tag(name_and_id["id"], content, options) end end - - private - - def retrieve_object(object) - if object - object - elsif @template_object.instance_variable_defined?("@#{@object_name}") - @template_object.instance_variable_get("@#{@object_name}") - end - rescue NameError - # As @object_name may contain the nested syntax (item[subobject]) we need to fallback to nil. - nil - end - - def retrieve_autoindex(pre_match) - object = self.object || @template_object.instance_variable_get("@#{pre_match}") - if object && object.respond_to?(:to_param) - object.to_param - else - raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}" - end - end - - def add_default_name_and_id_for_value(tag_value, options) - unless tag_value.nil? - pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase - specified_id = options["id"] - add_default_name_and_id(options) - options["id"] += "_#{pretty_tag_value}" if specified_id.blank? && options["id"].present? - else - add_default_name_and_id(options) - end - end - - def add_default_name_and_id(options) - if options.has_key?("index") - options["name"] ||= tag_name_with_index(options["index"]) - options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) } - options.delete("index") - elsif defined?(@auto_index) - options["name"] ||= tag_name_with_index(@auto_index) - options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) } - else - options["name"] ||= tag_name + (options['multiple'] ? '[]' : '') - options["id"] = options.fetch("id"){ tag_id } - end - options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence - end - - def tag_name - "#{@object_name}[#{sanitized_method_name}]" - end - - def tag_name_with_index(index) - "#{@object_name}[#{index}][#{sanitized_method_name}]" - end - - def tag_id - "#{sanitized_object_name}_#{sanitized_method_name}" - end - - def tag_id_with_index(index) - "#{sanitized_object_name}_#{index}_#{sanitized_method_name}" - end - - def sanitized_object_name - @sanitized_object_name ||= @object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "") - end - - def sanitized_method_name - @sanitized_method_name ||= @method_name.sub(/\?$/,"") - end end end end diff --git a/actionpack/lib/action_view/helpers/tags/text_field_tag.rb b/actionpack/lib/action_view/helpers/tags/text_field_tag.rb index 996a153cf8..1dee28203f 100644 --- a/actionpack/lib/action_view/helpers/tags/text_field_tag.rb +++ b/actionpack/lib/action_view/helpers/tags/text_field_tag.rb @@ -1,24 +1,8 @@ module ActionView module Helpers module Tags - class TextFieldTag - include Helpers::ActiveModelInstanceTag, Helpers::TagHelper, Helpers::FormTagHelper - - DEFAULT_FIELD_OPTIONS = { "size" => 30 } - - attr_reader :object - - def initialize(object_name, method_name, template_object, options = {}) - @object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup - @template_object = template_object - - @object_name.sub!(/\[\]$/,"") || @object_name.sub!(/\[\]\]$/,"]") - @object = retrieve_object(options.delete(:object)) - @options = options - @auto_index = retrieve_autoindex(Regexp.last_match.pre_match) if Regexp.last_match - end - - def render(&block) + class TextFieldTag < BaseTag + def render options = @options.stringify_keys options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size") options = DEFAULT_FIELD_OPTIONS.merge(options) @@ -28,86 +12,6 @@ module ActionView add_default_name_and_id(options) tag("input", options) end - - private - - def value_before_type_cast(object) - 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 retrieve_object(object) - if object - object - elsif @template_object.instance_variable_defined?("@#{@object_name}") - @template_object.instance_variable_get("@#{@object_name}") - end - rescue NameError - # As @object_name may contain the nested syntax (item[subobject]) we need to fallback to nil. - nil - end - - def retrieve_autoindex(pre_match) - object = self.object || @template_object.instance_variable_get("@#{pre_match}") - if object && object.respond_to?(:to_param) - object.to_param - else - raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}" - end - end - - def add_default_name_and_id_for_value(tag_value, options) - unless tag_value.nil? - pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase - specified_id = options["id"] - add_default_name_and_id(options) - options["id"] += "_#{pretty_tag_value}" if specified_id.blank? && options["id"].present? - else - add_default_name_and_id(options) - end - end - - def add_default_name_and_id(options) - if options.has_key?("index") - options["name"] ||= tag_name_with_index(options["index"]) - options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) } - options.delete("index") - elsif defined?(@auto_index) - options["name"] ||= tag_name_with_index(@auto_index) - options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) } - else - options["name"] ||= tag_name + (options['multiple'] ? '[]' : '') - options["id"] = options.fetch("id"){ tag_id } - end - options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence - end - - def tag_name - "#{@object_name}[#{sanitized_method_name}]" - end - - def tag_name_with_index(index) - "#{@object_name}[#{index}][#{sanitized_method_name}]" - end - - def tag_id - "#{sanitized_object_name}_#{sanitized_method_name}" - end - - def tag_id_with_index(index) - "#{sanitized_object_name}_#{index}_#{sanitized_method_name}" - end - - def sanitized_object_name - @sanitized_object_name ||= @object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "") - end - - def sanitized_method_name - @sanitized_method_name ||= @method_name.sub(/\?$/,"") - end end end end -- cgit v1.2.3 From 514308a49553e3a41c306d2df703ef7086ee054b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 18:38:13 -0300 Subject: Rename classes --- actionpack/lib/action_view/helpers/tags/base.rb | 107 +++++++++++++++++++++ .../lib/action_view/helpers/tags/base_tag.rb | 107 --------------------- actionpack/lib/action_view/helpers/tags/label.rb | 65 +++++++++++++ .../lib/action_view/helpers/tags/label_tag.rb | 65 ------------- .../lib/action_view/helpers/tags/text_field.rb | 18 ++++ .../lib/action_view/helpers/tags/text_field_tag.rb | 18 ---- 6 files changed, 190 insertions(+), 190 deletions(-) create mode 100644 actionpack/lib/action_view/helpers/tags/base.rb delete mode 100644 actionpack/lib/action_view/helpers/tags/base_tag.rb create mode 100644 actionpack/lib/action_view/helpers/tags/label.rb delete mode 100644 actionpack/lib/action_view/helpers/tags/label_tag.rb create mode 100644 actionpack/lib/action_view/helpers/tags/text_field.rb delete mode 100644 actionpack/lib/action_view/helpers/tags/text_field_tag.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/base.rb b/actionpack/lib/action_view/helpers/tags/base.rb new file mode 100644 index 0000000000..24f66f764e --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/base.rb @@ -0,0 +1,107 @@ +module ActionView + module Helpers + module Tags + class Base #:nodoc: + include Helpers::ActiveModelInstanceTag, Helpers::TagHelper, Helpers::FormTagHelper + + DEFAULT_FIELD_OPTIONS = { "size" => 30 } + + attr_reader :object + + def initialize(object_name, method_name, template_object, options = {}) + @object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup + @template_object = template_object + + @object_name.sub!(/\[\]$/,"") || @object_name.sub!(/\[\]\]$/,"]") + @object = retrieve_object(options.delete(:object)) + @options = options + @auto_index = retrieve_autoindex(Regexp.last_match.pre_match) if Regexp.last_match + end + + def render(&block) + raise "Abstract Method called" + end + + private + + def value_before_type_cast(object) + 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 retrieve_object(object) + if object + object + elsif @template_object.instance_variable_defined?("@#{@object_name}") + @template_object.instance_variable_get("@#{@object_name}") + end + rescue NameError + # As @object_name may contain the nested syntax (item[subobject]) we need to fallback to nil. + nil + end + + def retrieve_autoindex(pre_match) + object = self.object || @template_object.instance_variable_get("@#{pre_match}") + if object && object.respond_to?(:to_param) + object.to_param + else + raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}" + end + end + + def add_default_name_and_id_for_value(tag_value, options) + unless tag_value.nil? + pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase + specified_id = options["id"] + add_default_name_and_id(options) + options["id"] += "_#{pretty_tag_value}" if specified_id.blank? && options["id"].present? + else + add_default_name_and_id(options) + end + end + + def add_default_name_and_id(options) + if options.has_key?("index") + options["name"] ||= tag_name_with_index(options["index"]) + options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) } + options.delete("index") + elsif defined?(@auto_index) + options["name"] ||= tag_name_with_index(@auto_index) + options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) } + else + options["name"] ||= tag_name + (options['multiple'] ? '[]' : '') + options["id"] = options.fetch("id"){ tag_id } + end + options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence + end + + def tag_name + "#{@object_name}[#{sanitized_method_name}]" + end + + def tag_name_with_index(index) + "#{@object_name}[#{index}][#{sanitized_method_name}]" + end + + def tag_id + "#{sanitized_object_name}_#{sanitized_method_name}" + end + + def tag_id_with_index(index) + "#{sanitized_object_name}_#{index}_#{sanitized_method_name}" + end + + def sanitized_object_name + @sanitized_object_name ||= @object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "") + end + + def sanitized_method_name + @sanitized_method_name ||= @method_name.sub(/\?$/,"") + end + end + end + end +end diff --git a/actionpack/lib/action_view/helpers/tags/base_tag.rb b/actionpack/lib/action_view/helpers/tags/base_tag.rb deleted file mode 100644 index b9cb5f1a93..0000000000 --- a/actionpack/lib/action_view/helpers/tags/base_tag.rb +++ /dev/null @@ -1,107 +0,0 @@ -module ActionView - module Helpers - module Tags - class BaseTag - include Helpers::ActiveModelInstanceTag, Helpers::TagHelper, Helpers::FormTagHelper - - DEFAULT_FIELD_OPTIONS = { "size" => 30 } - - attr_reader :object - - def initialize(object_name, method_name, template_object, options = {}) - @object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup - @template_object = template_object - - @object_name.sub!(/\[\]$/,"") || @object_name.sub!(/\[\]\]$/,"]") - @object = retrieve_object(options.delete(:object)) - @options = options - @auto_index = retrieve_autoindex(Regexp.last_match.pre_match) if Regexp.last_match - end - - def render(&block) - raise "Abstract Method called" - end - - private - - def value_before_type_cast(object) - 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 retrieve_object(object) - if object - object - elsif @template_object.instance_variable_defined?("@#{@object_name}") - @template_object.instance_variable_get("@#{@object_name}") - end - rescue NameError - # As @object_name may contain the nested syntax (item[subobject]) we need to fallback to nil. - nil - end - - def retrieve_autoindex(pre_match) - object = self.object || @template_object.instance_variable_get("@#{pre_match}") - if object && object.respond_to?(:to_param) - object.to_param - else - raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}" - end - end - - def add_default_name_and_id_for_value(tag_value, options) - unless tag_value.nil? - pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase - specified_id = options["id"] - add_default_name_and_id(options) - options["id"] += "_#{pretty_tag_value}" if specified_id.blank? && options["id"].present? - else - add_default_name_and_id(options) - end - end - - def add_default_name_and_id(options) - if options.has_key?("index") - options["name"] ||= tag_name_with_index(options["index"]) - options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) } - options.delete("index") - elsif defined?(@auto_index) - options["name"] ||= tag_name_with_index(@auto_index) - options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) } - else - options["name"] ||= tag_name + (options['multiple'] ? '[]' : '') - options["id"] = options.fetch("id"){ tag_id } - end - options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence - end - - def tag_name - "#{@object_name}[#{sanitized_method_name}]" - end - - def tag_name_with_index(index) - "#{@object_name}[#{index}][#{sanitized_method_name}]" - end - - def tag_id - "#{sanitized_object_name}_#{sanitized_method_name}" - end - - def tag_id_with_index(index) - "#{sanitized_object_name}_#{index}_#{sanitized_method_name}" - end - - def sanitized_object_name - @sanitized_object_name ||= @object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "") - end - - def sanitized_method_name - @sanitized_method_name ||= @method_name.sub(/\?$/,"") - end - end - end - end -end diff --git a/actionpack/lib/action_view/helpers/tags/label.rb b/actionpack/lib/action_view/helpers/tags/label.rb new file mode 100644 index 0000000000..74ac92ee18 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/label.rb @@ -0,0 +1,65 @@ +module ActionView + module Helpers + module Tags + class Label < Base #:nodoc: + def initialize(object_name, method_name, template_object, content_or_options = nil, options = nil) + content_is_options = content_or_options.is_a?(Hash) + if content_is_options + options = content_or_options + @content = nil + else + @content = content_or_options + end + + options ||= {} + + super(object_name, method_name, template_object, options) + end + + def render(&block) + options = @options.stringify_keys + tag_value = options.delete("value") + name_and_id = options.dup + + if name_and_id["for"] + name_and_id["id"] = name_and_id["for"] + else + name_and_id.delete("id") + end + + add_default_name_and_id_for_value(tag_value, name_and_id) + options.delete("index") + options.delete("namespace") + options["for"] ||= name_and_id["id"] + + if block_given? + @template_object.label_tag(name_and_id["id"], options, &block) + else + content = if @content.blank? + @object_name.gsub!(/\[(.*)_attributes\]\[\d\]/, '.\1') + method_and_value = tag_value.present? ? "#{@method_name}.#{tag_value}" : @method_name + + if object.respond_to?(:to_model) + key = object.class.model_name.i18n_key + i18n_default = ["#{key}.#{method_and_value}".to_sym, ""] + end + + i18n_default ||= "" + I18n.t("#{@object_name}.#{method_and_value}", :default => i18n_default, :scope => "helpers.label").presence + else + @content.to_s + end + + content ||= if object && object.class.respond_to?(:human_attribute_name) + object.class.human_attribute_name(@method_name) + end + + content ||= @method_name.humanize + + label_tag(name_and_id["id"], content, options) + end + end + end + end + end +end diff --git a/actionpack/lib/action_view/helpers/tags/label_tag.rb b/actionpack/lib/action_view/helpers/tags/label_tag.rb deleted file mode 100644 index 8a7b15de59..0000000000 --- a/actionpack/lib/action_view/helpers/tags/label_tag.rb +++ /dev/null @@ -1,65 +0,0 @@ -module ActionView - module Helpers - module Tags - class LabelTag < BaseTag - def initialize(object_name, method_name, template_object, content_or_options = nil, options = nil) - content_is_options = content_or_options.is_a?(Hash) - if content_is_options - options = content_or_options - @content = nil - else - @content = content_or_options - end - - options ||= {} - - super(object_name, method_name, template_object, options) - end - - def render(&block) - options = @options.stringify_keys - tag_value = options.delete("value") - name_and_id = options.dup - - if name_and_id["for"] - name_and_id["id"] = name_and_id["for"] - else - name_and_id.delete("id") - end - - add_default_name_and_id_for_value(tag_value, name_and_id) - options.delete("index") - options.delete("namespace") - options["for"] ||= name_and_id["id"] - - if block_given? - @template_object.label_tag(name_and_id["id"], options, &block) - else - content = if @content.blank? - @object_name.gsub!(/\[(.*)_attributes\]\[\d\]/, '.\1') - method_and_value = tag_value.present? ? "#{@method_name}.#{tag_value}" : @method_name - - if object.respond_to?(:to_model) - key = object.class.model_name.i18n_key - i18n_default = ["#{key}.#{method_and_value}".to_sym, ""] - end - - i18n_default ||= "" - I18n.t("#{@object_name}.#{method_and_value}", :default => i18n_default, :scope => "helpers.label").presence - else - @content.to_s - end - - content ||= if object && object.class.respond_to?(:human_attribute_name) - object.class.human_attribute_name(@method_name) - end - - content ||= @method_name.humanize - - label_tag(name_and_id["id"], content, options) - end - end - end - end - end -end diff --git a/actionpack/lib/action_view/helpers/tags/text_field.rb b/actionpack/lib/action_view/helpers/tags/text_field.rb new file mode 100644 index 0000000000..4e745061db --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/text_field.rb @@ -0,0 +1,18 @@ +module ActionView + module Helpers + module Tags + class TextField < Base #:nodoc: + def render + options = @options.stringify_keys + options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size") + options = DEFAULT_FIELD_OPTIONS.merge(options) + options["type"] ||= "text" + options["value"] = options.fetch("value"){ value_before_type_cast(object) } + options["value"] &&= ERB::Util.html_escape(options["value"]) + add_default_name_and_id(options) + tag("input", options) + end + end + end + end +end diff --git a/actionpack/lib/action_view/helpers/tags/text_field_tag.rb b/actionpack/lib/action_view/helpers/tags/text_field_tag.rb deleted file mode 100644 index 1dee28203f..0000000000 --- a/actionpack/lib/action_view/helpers/tags/text_field_tag.rb +++ /dev/null @@ -1,18 +0,0 @@ -module ActionView - module Helpers - module Tags - class TextFieldTag < BaseTag - def render - options = @options.stringify_keys - options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size") - options = DEFAULT_FIELD_OPTIONS.merge(options) - options["type"] ||= "text" - options["value"] = options.fetch("value"){ value_before_type_cast(object) } - options["value"] &&= ERB::Util.html_escape(options["value"]) - add_default_name_and_id(options) - tag("input", options) - end - end - end - end -end -- cgit v1.2.3 From 0ac47bd8192cb8b5b61b6a9ab2ce8b3b352870f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 19:07:52 -0300 Subject: Extract PasswordField --- actionpack/lib/action_view/helpers/tags/password_field.rb | 12 ++++++++++++ actionpack/lib/action_view/helpers/tags/text_field.rb | 8 +++++++- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 actionpack/lib/action_view/helpers/tags/password_field.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/password_field.rb b/actionpack/lib/action_view/helpers/tags/password_field.rb new file mode 100644 index 0000000000..6e7a4d3c36 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/password_field.rb @@ -0,0 +1,12 @@ +module ActionView + module Helpers + module Tags + class PasswordField < TextField #:nodoc: + def render + @options = {:value => nil}.merge!(@options) + super + end + end + end + end +end diff --git a/actionpack/lib/action_view/helpers/tags/text_field.rb b/actionpack/lib/action_view/helpers/tags/text_field.rb index 4e745061db..758fd636f2 100644 --- a/actionpack/lib/action_view/helpers/tags/text_field.rb +++ b/actionpack/lib/action_view/helpers/tags/text_field.rb @@ -6,12 +6,18 @@ module ActionView options = @options.stringify_keys options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size") options = DEFAULT_FIELD_OPTIONS.merge(options) - options["type"] ||= "text" + options["type"] ||= field_type options["value"] = options.fetch("value"){ value_before_type_cast(object) } options["value"] &&= ERB::Util.html_escape(options["value"]) add_default_name_and_id(options) tag("input", options) end + + private + + def field_type + self.class.name.split("::").last.sub("Field", "").downcase + end end end end -- cgit v1.2.3 From 6aeb799866ae08a0e8944cc23a48f1616b60d560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 20:02:39 -0300 Subject: Extract HiddenField --- actionpack/lib/action_view/helpers/tags/hidden_field.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/hidden_field.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/hidden_field.rb b/actionpack/lib/action_view/helpers/tags/hidden_field.rb new file mode 100644 index 0000000000..ea86596e0b --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/hidden_field.rb @@ -0,0 +1,12 @@ +module ActionView + module Helpers + module Tags + class HiddenField < TextField #:nodoc: + def render + @options.update(:size => nil) + super + end + end + end + end +end -- cgit v1.2.3 From 68b870f6be0eff3de6b3dfb6ba64b253419260ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 20:14:56 -0300 Subject: Extract FileField --- actionpack/lib/action_view/helpers/tags/file_field.rb | 12 ++++++++++++ actionpack/lib/action_view/helpers/tags/text_field.rb | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 actionpack/lib/action_view/helpers/tags/file_field.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/file_field.rb b/actionpack/lib/action_view/helpers/tags/file_field.rb new file mode 100644 index 0000000000..56442e1c14 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/file_field.rb @@ -0,0 +1,12 @@ +module ActionView + module Helpers + module Tags + class FileField < TextField #:nodoc: + def render + @options.update(:size => nil) + super + end + end + end + end +end diff --git a/actionpack/lib/action_view/helpers/tags/text_field.rb b/actionpack/lib/action_view/helpers/tags/text_field.rb index 758fd636f2..0f81726eb4 100644 --- a/actionpack/lib/action_view/helpers/tags/text_field.rb +++ b/actionpack/lib/action_view/helpers/tags/text_field.rb @@ -7,7 +7,7 @@ module ActionView options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size") options = DEFAULT_FIELD_OPTIONS.merge(options) options["type"] ||= field_type - options["value"] = options.fetch("value"){ value_before_type_cast(object) } + options["value"] = options.fetch("value"){ value_before_type_cast(object) } unless field_type == "file" options["value"] &&= ERB::Util.html_escape(options["value"]) add_default_name_and_id(options) tag("input", options) @@ -16,7 +16,7 @@ module ActionView private def field_type - self.class.name.split("::").last.sub("Field", "").downcase + @field_type ||= self.class.name.split("::").last.sub("Field", "").downcase end end end -- cgit v1.2.3 From 8e8c6c35832e6a9d9ee09b645e9f34b3594128c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 20:29:01 -0300 Subject: Extract TextArea --- actionpack/lib/action_view/helpers/tags/text_area.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/text_area.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/text_area.rb b/actionpack/lib/action_view/helpers/tags/text_area.rb new file mode 100644 index 0000000000..a7db8eb437 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/text_area.rb @@ -0,0 +1,20 @@ +module ActionView + module Helpers + module Tags + class TextArea < Base #:nodoc: + DEFAULT_TEXT_AREA_OPTIONS = { "cols" => 40, "rows" => 20 } + + def render + options = DEFAULT_TEXT_AREA_OPTIONS.merge(@options.stringify_keys) + add_default_name_and_id(options) + + if size = options.delete("size") + options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split) + end + + content_tag("textarea", ERB::Util.html_escape(options.delete('value') || value_before_type_cast(object)), options) + end + end + end + end +end -- cgit v1.2.3 From f42e1db35ab3a0300b8e02ac55e0ae82873b5986 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 20:48:32 -0300 Subject: Extract CheckBox --- .../lib/action_view/helpers/tags/check_box.rb | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/check_box.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/check_box.rb b/actionpack/lib/action_view/helpers/tags/check_box.rb new file mode 100644 index 0000000000..89aaad632c --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/check_box.rb @@ -0,0 +1,60 @@ +module ActionView + module Helpers + module Tags + class CheckBox < Base #:nodoc: + def initialize(object_name, method_name, template_object, checked_value, unchecked_value, options) + @checked_value = checked_value + @unchecked_value = unchecked_value + super(object_name, method_name, template_object, options) + end + + def render + options = @options.stringify_keys + options["type"] = "checkbox" + options["value"] = @checked_value + + if options.has_key?("checked") + cv = options.delete "checked" + checked = cv == true || cv == "checked" + else + checked = check_box_checked?(value(object)) + end + + options["checked"] = "checked" if checked + if options["multiple"] + add_default_name_and_id_for_value(@checked_value, options) + options.delete("multiple") + else + add_default_name_and_id(options) + end + hidden = @unchecked_value ? tag("input", "name" => options["name"], "type" => "hidden", "value" => @unchecked_value, "disabled" => options["disabled"]) : "" + checkbox = tag("input", options) + hidden + checkbox + end + + private + + def value(object) + object.send @method_name if object + end + + def check_box_checked?(value) + case value + when TrueClass, FalseClass + value + when NilClass + false + when Integer + value != 0 + when String + value == @checked_value + when Array + value.include?(@checked_value) + else + value.to_i != 0 + end + end + end + end + end +end -- cgit v1.2.3 From d6b9eb9b4417f1f8705284e520b796466a690ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 21:06:13 -0300 Subject: Extract RadioButton --- actionpack/lib/action_view/helpers/tags/base.rb | 4 +++ .../lib/action_view/helpers/tags/check_box.rb | 4 --- .../lib/action_view/helpers/tags/radio_button.rb | 35 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 actionpack/lib/action_view/helpers/tags/radio_button.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/base.rb b/actionpack/lib/action_view/helpers/tags/base.rb index 24f66f764e..c50c7716d4 100644 --- a/actionpack/lib/action_view/helpers/tags/base.rb +++ b/actionpack/lib/action_view/helpers/tags/base.rb @@ -24,6 +24,10 @@ module ActionView private + def value(object) + object.send @method_name if object + end + def value_before_type_cast(object) unless object.nil? object.respond_to?(@method_name + "_before_type_cast") ? diff --git a/actionpack/lib/action_view/helpers/tags/check_box.rb b/actionpack/lib/action_view/helpers/tags/check_box.rb index 89aaad632c..55574e6f06 100644 --- a/actionpack/lib/action_view/helpers/tags/check_box.rb +++ b/actionpack/lib/action_view/helpers/tags/check_box.rb @@ -34,10 +34,6 @@ module ActionView private - def value(object) - object.send @method_name if object - end - def check_box_checked?(value) case value when TrueClass, FalseClass diff --git a/actionpack/lib/action_view/helpers/tags/radio_button.rb b/actionpack/lib/action_view/helpers/tags/radio_button.rb new file mode 100644 index 0000000000..3f6a360928 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/radio_button.rb @@ -0,0 +1,35 @@ +module ActionView + module Helpers + module Tags + class RadioButton < Base #:nodoc: + def initialize(object_name, method_name, template_object, tag_value, options) + @tag_value = tag_value + super(object_name, method_name, template_object, options) + end + + def render + options = @options.stringify_keys + options["type"] = "radio" + options["value"] = @tag_value + + if options.has_key?("checked") + cv = options.delete "checked" + checked = cv == true || cv == "checked" + else + checked = radio_button_checked?(value(object)) + end + + options["checked"] = "checked" if checked + add_default_name_and_id_for_value(@tag_value, options) + tag("input", options) + end + + private + + def radio_button_checked?(value) + value.to_s == @tag_value.to_s + end + end + end + end +end -- cgit v1.2.3 From 675fb8defdbfdabdd57298b760e701b0b5c74e48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 21:17:33 -0300 Subject: Remove code duplication between CheckBox ans RadioButton --- actionpack/lib/action_view/helpers/tags/base.rb | 9 +++++++++ actionpack/lib/action_view/helpers/tags/check_box.rb | 12 +++--------- actionpack/lib/action_view/helpers/tags/radio_button.rb | 12 ++---------- 3 files changed, 14 insertions(+), 19 deletions(-) (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/base.rb b/actionpack/lib/action_view/helpers/tags/base.rb index c50c7716d4..9f026f69b1 100644 --- a/actionpack/lib/action_view/helpers/tags/base.rb +++ b/actionpack/lib/action_view/helpers/tags/base.rb @@ -47,6 +47,15 @@ module ActionView nil end + def input_checked?(object, options) + if options.has_key?("checked") + checked = options.delete "checked" + checked == true || checked == "checked" + else + checked?(value(object)) + end + end + def retrieve_autoindex(pre_match) object = self.object || @template_object.instance_variable_get("@#{pre_match}") if object && object.respond_to?(:to_param) diff --git a/actionpack/lib/action_view/helpers/tags/check_box.rb b/actionpack/lib/action_view/helpers/tags/check_box.rb index 55574e6f06..2cdc13524b 100644 --- a/actionpack/lib/action_view/helpers/tags/check_box.rb +++ b/actionpack/lib/action_view/helpers/tags/check_box.rb @@ -12,21 +12,15 @@ module ActionView options = @options.stringify_keys options["type"] = "checkbox" options["value"] = @checked_value + options["checked"] = "checked" if input_checked?(object, options) - if options.has_key?("checked") - cv = options.delete "checked" - checked = cv == true || cv == "checked" - else - checked = check_box_checked?(value(object)) - end - - options["checked"] = "checked" if checked if options["multiple"] add_default_name_and_id_for_value(@checked_value, options) options.delete("multiple") else add_default_name_and_id(options) end + hidden = @unchecked_value ? tag("input", "name" => options["name"], "type" => "hidden", "value" => @unchecked_value, "disabled" => options["disabled"]) : "" checkbox = tag("input", options) hidden + checkbox @@ -34,7 +28,7 @@ module ActionView private - def check_box_checked?(value) + def checked?(value) case value when TrueClass, FalseClass value diff --git a/actionpack/lib/action_view/helpers/tags/radio_button.rb b/actionpack/lib/action_view/helpers/tags/radio_button.rb index 3f6a360928..72edd21662 100644 --- a/actionpack/lib/action_view/helpers/tags/radio_button.rb +++ b/actionpack/lib/action_view/helpers/tags/radio_button.rb @@ -11,22 +11,14 @@ module ActionView options = @options.stringify_keys options["type"] = "radio" options["value"] = @tag_value - - if options.has_key?("checked") - cv = options.delete "checked" - checked = cv == true || cv == "checked" - else - checked = radio_button_checked?(value(object)) - end - - options["checked"] = "checked" if checked + options["checked"] = "checked" if input_checked?(object, options) add_default_name_and_id_for_value(@tag_value, options) tag("input", options) end private - def radio_button_checked?(value) + def checked?(value) value.to_s == @tag_value.to_s end end -- cgit v1.2.3 From 487471611b8eab1f1367fa2ef93aed77fb52dee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 21:25:32 -0300 Subject: Extract SearchField --- .../lib/action_view/helpers/tags/search_field.rb | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/search_field.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/search_field.rb b/actionpack/lib/action_view/helpers/tags/search_field.rb new file mode 100644 index 0000000000..818fd4b887 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/search_field.rb @@ -0,0 +1,24 @@ +module ActionView + module Helpers + module Tags + class SearchField < TextField #:nodoc: + def render + options = @options.stringify_keys + + if options["autosave"] + if options["autosave"] == true + options["autosave"] = request.host.split(".").reverse.join(".") + end + options["results"] ||= 10 + end + + if options["onsearch"] + options["incremental"] = true unless options.has_key?("incremental") + end + + super + end + end + end + end +end -- cgit v1.2.3 From 4cfdd238ed3bc7d4f54faab923dbceeeb7a4bced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 21:37:31 -0300 Subject: Extract TelField --- actionpack/lib/action_view/helpers/tags/tel_field.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/tel_field.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/tel_field.rb b/actionpack/lib/action_view/helpers/tags/tel_field.rb new file mode 100644 index 0000000000..87c1f6b6b6 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/tel_field.rb @@ -0,0 +1,8 @@ +module ActionView + module Helpers + module Tags + class TelField < TextField #:nodoc: + end + end + end +end -- cgit v1.2.3 From 864d7575a3908f14ffc2830a6b2ea78a98756079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 21:42:54 -0300 Subject: Extract UrlField --- actionpack/lib/action_view/helpers/tags/url_field.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/url_field.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/url_field.rb b/actionpack/lib/action_view/helpers/tags/url_field.rb new file mode 100644 index 0000000000..1ffdfe0b3c --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/url_field.rb @@ -0,0 +1,8 @@ +module ActionView + module Helpers + module Tags + class UrlField < TextField #:nodoc: + end + end + end +end -- cgit v1.2.3 From 647aff9e51dca62ea793c68c5ff830c8b364aa50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 21:43:06 -0300 Subject: Extract EmailField --- actionpack/lib/action_view/helpers/tags/email_field.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/email_field.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/email_field.rb b/actionpack/lib/action_view/helpers/tags/email_field.rb new file mode 100644 index 0000000000..45cde507d7 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/email_field.rb @@ -0,0 +1,8 @@ +module ActionView + module Helpers + module Tags + class EmailField < TextField #:nodoc: + end + end + end +end -- cgit v1.2.3 From 0f49caa6e0544521765fce2010337e3376e18c83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 21:49:55 -0300 Subject: Extract NumberField --- .../lib/action_view/helpers/tags/number_field.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/number_field.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/number_field.rb b/actionpack/lib/action_view/helpers/tags/number_field.rb new file mode 100644 index 0000000000..e89fdbec46 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/number_field.rb @@ -0,0 +1,19 @@ +module ActionView + module Helpers + module Tags + class NumberField < TextField #:nodoc: + def render + options = @options.stringify_keys + options['size'] ||= nil + + if range = options.delete("in") || options.delete("within") + options.update("min" => range.min, "max" => range.max) + end + + @options = options + super + end + end + end + end +end -- cgit v1.2.3 From f0041d4714f264bd72e9aaab055b03a7106c5990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 21:54:44 -0300 Subject: Extract RangeField --- actionpack/lib/action_view/helpers/tags/range_field.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/range_field.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/range_field.rb b/actionpack/lib/action_view/helpers/tags/range_field.rb new file mode 100644 index 0000000000..47db4680e7 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/range_field.rb @@ -0,0 +1,8 @@ +module ActionView + module Helpers + module Tags + class RangeField < NumberField #:nodoc: + end + end + end +end -- cgit v1.2.3 From 18f181db479630679bcc414b8c290b40d8b26781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 22:54:55 -0300 Subject: Extract Select --- actionpack/lib/action_view/helpers/tags/select.rb | 57 +++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/select.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/select.rb b/actionpack/lib/action_view/helpers/tags/select.rb new file mode 100644 index 0000000000..56d1dbfd38 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/select.rb @@ -0,0 +1,57 @@ +module ActionView + module Helpers + module Tags + class Select < Base #:nodoc: + include FormOptionsHelper + + def initialize(object_name, method_name, template_object, choices, options, html_options) + @choices = choices + @html_options = html_options + + super(object_name, method_name, template_object, options) + end + + def render + selected_value = @options.has_key?(:selected) ? @options[:selected] : value(@object) + + # Grouped choices look like this: + # + # [nil, []] + # { nil => [] } + # + if !@choices.empty? && @choices.first.respond_to?(:last) && Array === @choices.first.last + option_tags = grouped_options_for_select(@choices, :selected => selected_value, :disabled => @options[:disabled]) + else + option_tags = options_for_select(@choices, :selected => selected_value, :disabled => @options[:disabled]) + end + + select_content_tag(option_tags, @options, @html_options) + end + + private + + def select_content_tag(option_tags, options, html_options) + html_options = html_options.stringify_keys + add_default_name_and_id(html_options) + select = content_tag("select", add_options(option_tags, options, value(object)), html_options) + if html_options["multiple"] + tag("input", :disabled => html_options["disabled"], :name => html_options["name"], :type => "hidden", :value => "") + select + else + select + end + end + + def add_options(option_tags, options, value = nil) + if options[:include_blank] + option_tags = "\n" + option_tags + end + if value.blank? && options[:prompt] + prompt = options[:prompt].kind_of?(String) ? options[:prompt] : I18n.translate('helpers.select.prompt', :default => 'Please select') + option_tags = "\n" + option_tags + end + option_tags.html_safe + end + end + end + end +end -- cgit v1.2.3 From 9eb6cd6373cf8c1142e1f3cd9e3af55d47828bd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 23:06:25 -0300 Subject: Extract CollectionSelect --- actionpack/lib/action_view/helpers/tags/base.rb | 23 +++++++++++++++++++ .../action_view/helpers/tags/collection_select.rb | 23 +++++++++++++++++++ actionpack/lib/action_view/helpers/tags/select.rb | 26 ---------------------- 3 files changed, 46 insertions(+), 26 deletions(-) create mode 100644 actionpack/lib/action_view/helpers/tags/collection_select.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/base.rb b/actionpack/lib/action_view/helpers/tags/base.rb index 9f026f69b1..7580cbe20d 100644 --- a/actionpack/lib/action_view/helpers/tags/base.rb +++ b/actionpack/lib/action_view/helpers/tags/base.rb @@ -3,6 +3,7 @@ module ActionView module Tags class Base #:nodoc: include Helpers::ActiveModelInstanceTag, Helpers::TagHelper, Helpers::FormTagHelper + include FormOptionsHelper DEFAULT_FIELD_OPTIONS = { "size" => 30 } @@ -114,6 +115,28 @@ module ActionView def sanitized_method_name @sanitized_method_name ||= @method_name.sub(/\?$/,"") end + + def select_content_tag(option_tags, options, html_options) + html_options = html_options.stringify_keys + add_default_name_and_id(html_options) + select = content_tag("select", add_options(option_tags, options, value(object)), html_options) + if html_options["multiple"] + tag("input", :disabled => html_options["disabled"], :name => html_options["name"], :type => "hidden", :value => "") + select + else + select + end + end + + def add_options(option_tags, options, value = nil) + if options[:include_blank] + option_tags = "\n" + option_tags + end + if value.blank? && options[:prompt] + prompt = options[:prompt].kind_of?(String) ? options[:prompt] : I18n.translate('helpers.select.prompt', :default => 'Please select') + option_tags = "\n" + option_tags + end + option_tags.html_safe + end end end end diff --git a/actionpack/lib/action_view/helpers/tags/collection_select.rb b/actionpack/lib/action_view/helpers/tags/collection_select.rb new file mode 100644 index 0000000000..f84140d8d0 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/collection_select.rb @@ -0,0 +1,23 @@ +module ActionView + module Helpers + module Tags + class CollectionSelect < Base #:nodoc: + def initialize(object_name, method_name, template_object, collection, value_method, text_method, options, html_options) + @collection = collection + @value_method = value_method + @text_method = text_method + @html_options = html_options + + super(object_name, method_name, template_object, options) + end + + def render + selected_value = @options.has_key?(:selected) ? @options[:selected] : value(@object) + select_content_tag( + options_from_collection_for_select(@collection, @value_method, @text_method, :selected => selected_value, :disabled => @options[:disabled]), @options, @html_options + ) + end + end + end + end +end diff --git a/actionpack/lib/action_view/helpers/tags/select.rb b/actionpack/lib/action_view/helpers/tags/select.rb index 56d1dbfd38..71fd4d04b7 100644 --- a/actionpack/lib/action_view/helpers/tags/select.rb +++ b/actionpack/lib/action_view/helpers/tags/select.rb @@ -2,8 +2,6 @@ module ActionView module Helpers module Tags class Select < Base #:nodoc: - include FormOptionsHelper - def initialize(object_name, method_name, template_object, choices, options, html_options) @choices = choices @html_options = html_options @@ -27,30 +25,6 @@ module ActionView select_content_tag(option_tags, @options, @html_options) end - - private - - def select_content_tag(option_tags, options, html_options) - html_options = html_options.stringify_keys - add_default_name_and_id(html_options) - select = content_tag("select", add_options(option_tags, options, value(object)), html_options) - if html_options["multiple"] - tag("input", :disabled => html_options["disabled"], :name => html_options["name"], :type => "hidden", :value => "") + select - else - select - end - end - - def add_options(option_tags, options, value = nil) - if options[:include_blank] - option_tags = "\n" + option_tags - end - if value.blank? && options[:prompt] - prompt = options[:prompt].kind_of?(String) ? options[:prompt] : I18n.translate('helpers.select.prompt', :default => 'Please select') - option_tags = "\n" + option_tags - end - option_tags.html_safe - end end end end -- cgit v1.2.3 From 7d2846b9dbb728d304347a8e70dbe845d4efbb35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 23:15:07 -0300 Subject: Extract GroupedCollectionSelect --- .../helpers/tags/grouped_collection_select.rb | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/grouped_collection_select.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/grouped_collection_select.rb b/actionpack/lib/action_view/helpers/tags/grouped_collection_select.rb new file mode 100644 index 0000000000..507466a57a --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/grouped_collection_select.rb @@ -0,0 +1,24 @@ +module ActionView + module Helpers + module Tags + class GroupedCollectionSelect < Base #:nodoc: + def initialize(object_name, method_name, template_object, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options) + @collection = collection + @group_method = group_method + @group_label_method = group_label_method + @option_key_method = option_key_method + @option_value_method = option_value_method + @html_options = html_options + + super(object_name, method_name, template_object, options) + end + + def render + select_content_tag( + option_groups_from_collection_for_select(@collection, @group_method, @group_label_method, @option_key_method, @option_value_method, value(@object)), @options, @html_options + ) + end + end + end + end +end -- cgit v1.2.3 From 8461ad9744c3fea7db0c4c02af66afa30241ebb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 23:20:27 -0300 Subject: Extract TimeZoneSelect --- .../lib/action_view/helpers/tags/time_zone_select.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/time_zone_select.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/time_zone_select.rb b/actionpack/lib/action_view/helpers/tags/time_zone_select.rb new file mode 100644 index 0000000000..0a176157c3 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/time_zone_select.rb @@ -0,0 +1,20 @@ +module ActionView + module Helpers + module Tags + class TimeZoneSelect < Base #:nodoc: + def initialize(object_name, method_name, template_object, priority_zones, options, html_options) + @priority_zones = priority_zones + @html_options = html_options + + super(object_name, method_name, template_object, options) + end + + def render + select_content_tag( + time_zone_options_for_select(value(@object) || @options[:default], @priority_zones, @options[:model] || ActiveSupport::TimeZone), @options, @html_options + ) + end + end + end + end +end -- cgit v1.2.3 From e70f68fd1ebcb35a9f82b0b3c5a6ea14a4e253cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 23:47:49 -0300 Subject: Extract DateSelect --- .../lib/action_view/helpers/tags/date_select.rb | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/date_select.rb (limited to 'actionpack/lib/action_view/helpers/tags') 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..2f2681bf65 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/date_select.rb @@ -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 -- cgit v1.2.3 From b69e449c512d68b54af3d15cdf2d92181c2817d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 23:55:48 -0300 Subject: Extract TimeSelect --- actionpack/lib/action_view/helpers/tags/date_select.rb | 6 +++++- actionpack/lib/action_view/helpers/tags/time_select.rb | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 actionpack/lib/action_view/helpers/tags/time_select.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/date_select.rb b/actionpack/lib/action_view/helpers/tags/date_select.rb index 2f2681bf65..5912598ca1 100644 --- a/actionpack/lib/action_view/helpers/tags/date_select.rb +++ b/actionpack/lib/action_view/helpers/tags/date_select.rb @@ -9,11 +9,15 @@ module ActionView end def render - error_wrapping(datetime_selector(@options, @html_options).select_date.html_safe) + 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 diff --git a/actionpack/lib/action_view/helpers/tags/time_select.rb b/actionpack/lib/action_view/helpers/tags/time_select.rb new file mode 100644 index 0000000000..9e97deb706 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/time_select.rb @@ -0,0 +1,8 @@ +module ActionView + module Helpers + module Tags + class TimeSelect < DateSelect #:nodoc: + end + end + end +end -- cgit v1.2.3 From f6fc352927ca1db03e4c0541a49845ac9e51573a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 17 Jan 2012 00:00:42 -0300 Subject: Extract DatetimeSelect --- actionpack/lib/action_view/helpers/tags/datetime_select.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/tags/datetime_select.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/datetime_select.rb b/actionpack/lib/action_view/helpers/tags/datetime_select.rb new file mode 100644 index 0000000000..a32c840bce --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/datetime_select.rb @@ -0,0 +1,8 @@ +module ActionView + module Helpers + module Tags + class DatetimeSelect < DateSelect #:nodoc: + end + end + end +end -- cgit v1.2.3 From 6f1bf526d7f1869b47f6047c4285c673bb06d0ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 17 Jan 2012 18:52:48 -0300 Subject: Extract input_checked? to a module --- actionpack/lib/action_view/helpers/tags/base.rb | 9 --------- actionpack/lib/action_view/helpers/tags/check_box.rb | 4 ++++ actionpack/lib/action_view/helpers/tags/checkable.rb | 16 ++++++++++++++++ actionpack/lib/action_view/helpers/tags/radio_button.rb | 4 ++++ 4 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 actionpack/lib/action_view/helpers/tags/checkable.rb (limited to 'actionpack/lib/action_view/helpers/tags') diff --git a/actionpack/lib/action_view/helpers/tags/base.rb b/actionpack/lib/action_view/helpers/tags/base.rb index 7580cbe20d..24956beb9c 100644 --- a/actionpack/lib/action_view/helpers/tags/base.rb +++ b/actionpack/lib/action_view/helpers/tags/base.rb @@ -48,15 +48,6 @@ module ActionView nil end - def input_checked?(object, options) - if options.has_key?("checked") - checked = options.delete "checked" - checked == true || checked == "checked" - else - checked?(value(object)) - end - end - def retrieve_autoindex(pre_match) object = self.object || @template_object.instance_variable_get("@#{pre_match}") if object && object.respond_to?(:to_param) diff --git a/actionpack/lib/action_view/helpers/tags/check_box.rb b/actionpack/lib/action_view/helpers/tags/check_box.rb index 2cdc13524b..b3bd6eb2ad 100644 --- a/actionpack/lib/action_view/helpers/tags/check_box.rb +++ b/actionpack/lib/action_view/helpers/tags/check_box.rb @@ -1,7 +1,11 @@ +require 'action_view/helpers/tags/checkable' + module ActionView module Helpers module Tags class CheckBox < Base #:nodoc: + include Checkable + def initialize(object_name, method_name, template_object, checked_value, unchecked_value, options) @checked_value = checked_value @unchecked_value = unchecked_value diff --git a/actionpack/lib/action_view/helpers/tags/checkable.rb b/actionpack/lib/action_view/helpers/tags/checkable.rb new file mode 100644 index 0000000000..b97c0c68d7 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/checkable.rb @@ -0,0 +1,16 @@ +module ActionView + module Helpers + module Tags + module Checkable + def input_checked?(object, options) + if options.has_key?("checked") + checked = options.delete "checked" + checked == true || checked == "checked" + else + checked?(value(object)) + end + end + end + end + end +end diff --git a/actionpack/lib/action_view/helpers/tags/radio_button.rb b/actionpack/lib/action_view/helpers/tags/radio_button.rb index 72edd21662..8a0421f061 100644 --- a/actionpack/lib/action_view/helpers/tags/radio_button.rb +++ b/actionpack/lib/action_view/helpers/tags/radio_button.rb @@ -1,7 +1,11 @@ +require 'action_view/helpers/tags/checkable' + module ActionView module Helpers module Tags class RadioButton < Base #:nodoc: + include Checkable + def initialize(object_name, method_name, template_object, tag_value, options) @tag_value = tag_value super(object_name, method_name, template_object, options) -- cgit v1.2.3