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/form_helper.rb | 4 +- actionpack/lib/action_view/helpers/tags.rb | 6 +- 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 ---- 8 files changed, 195 insertions(+), 195 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') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index e1244346d4..d3d0e2e2f8 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -655,7 +655,7 @@ module ActionView # 'Accept Terms.'.html_safe # end def label(object_name, method, content_or_options = nil, options = nil, &block) - ActionView::Helpers::Tags::LabelTag.new(object_name, method, self, content_or_options, options).render(&block) + ActionView::Helpers::Tags::Label.new(object_name, method, self, content_or_options, options).render(&block) end # Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object @@ -677,7 +677,7 @@ module ActionView # # => # def text_field(object_name, method, options = {}) - ActionView::Helpers::Tags::TextFieldTag.new(object_name, method, self, options).render + ActionView::Helpers::Tags::TextField.new(object_name, method, self, options).render end # Returns an input tag of the "password" type tailored for accessing a specified attribute (identified by +method+) on an object diff --git a/actionpack/lib/action_view/helpers/tags.rb b/actionpack/lib/action_view/helpers/tags.rb index cff5b0220c..bc2fb34deb 100644 --- a/actionpack/lib/action_view/helpers/tags.rb +++ b/actionpack/lib/action_view/helpers/tags.rb @@ -1,9 +1,9 @@ module ActionView module Helpers module Tags - autoload :BaseTag, 'action_view/helpers/tags/base_tag' - autoload :LabelTag, 'action_view/helpers/tags/label_tag' - autoload :TextFieldTag, 'action_view/helpers/tags/text_field_tag' + autoload :Base, 'action_view/helpers/tags/base' + autoload :Label, 'action_view/helpers/tags/label' + autoload :TextField, 'action_view/helpers/tags/text_field' end end end 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