aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/tags/label_tag.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-01-16 17:49:39 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-01-17 00:05:28 -0300
commit801dcb0a391a2145a38a7eb8a3879f59a654a825 (patch)
tree8fd75ed1653f7e01f12257915980bcba65addc81 /actionpack/lib/action_view/helpers/tags/label_tag.rb
parent520fe7defea2271cc540e8190a7b9d83d8dfb8d5 (diff)
downloadrails-801dcb0a391a2145a38a7eb8a3879f59a654a825.tar.gz
rails-801dcb0a391a2145a38a7eb8a3879f59a654a825.tar.bz2
rails-801dcb0a391a2145a38a7eb8a3879f59a654a825.zip
Extract duplicated code to a class
Diffstat (limited to 'actionpack/lib/action_view/helpers/tags/label_tag.rb')
-rw-r--r--actionpack/lib/action_view/helpers/tags/label_tag.rb94
1 files changed, 6 insertions, 88 deletions
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