Skip to content

Commit

Permalink
Merge pull request #18845 from bogdan/remove-code-dups-in-action-view
Browse files Browse the repository at this point in the history
Remove some code duplication in ActionView tags code
  • Loading branch information
rafaelfranca committed Feb 11, 2015
2 parents d9cd1e9 + 7543717 commit c3513a1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 33 deletions.
49 changes: 24 additions & 25 deletions actionview/lib/action_view/helpers/tags/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(object_name, method_name, template_object, options = {})
@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
@auto_index = Regexp.last_match ? retrieve_autoindex(Regexp.last_match.pre_match) : nil
end

# This is what child classes implement.
Expand Down Expand Up @@ -79,35 +79,30 @@ def add_default_name_and_id_for_value(tag_value, options)
end

def add_default_name_and_id(options)
if options.has_key?("index")
options["name"] ||= options.fetch("name"){ tag_name_with_index(options["index"], options["multiple"]) }
options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) }
options.delete("index")
elsif defined?(@auto_index)
options["name"] ||= options.fetch("name"){ tag_name_with_index(@auto_index, options["multiple"]) }
options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) }
else
options["name"] ||= options.fetch("name"){ tag_name(options["multiple"]) }
options["id"] = options.fetch("id"){ tag_id }
index = name_and_id_index(options)
options["name"] = options.fetch("name"){ tag_name(options["multiple"], index) }
options["id"] = options.fetch("id"){ tag_id(index) }
if namespace = options.delete("namespace")
options['id'] = options['id'] ? "#{namespace}_#{options['id']}" : namespace
end

options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence
end

def tag_name(multiple = false)
"#{@object_name}[#{sanitized_method_name}]#{"[]" if multiple}"
end

def tag_name_with_index(index, multiple = false)
"#{@object_name}[#{index}][#{sanitized_method_name}]#{"[]" if multiple}"
end

def tag_id
"#{sanitized_object_name}_#{sanitized_method_name}"
def tag_name(multiple = false, index = nil)
# a little duplication to construct less strings
if index
"#{@object_name}[#{index}][#{sanitized_method_name}]#{"[]" if multiple}"
else
"#{@object_name}[#{sanitized_method_name}]#{"[]" if multiple}"
end
end

def tag_id_with_index(index)
"#{sanitized_object_name}_#{index}_#{sanitized_method_name}"
def tag_id(index = nil)
# a little duplication to construct less strings
if index
"#{sanitized_object_name}_#{index}_#{sanitized_method_name}"
else
"#{sanitized_object_name}_#{sanitized_method_name}"
end
end

def sanitized_object_name
Expand Down Expand Up @@ -149,6 +144,10 @@ def add_options(option_tags, options, value = nil)
end
option_tags
end

def name_and_id_index(options)
options.key?("index") ? options.delete("index") || "" : @auto_index
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@ def render_component(builder)
end

def hidden_field
hidden_name = @html_options[:name]

hidden_name ||= if @options.has_key?(:index)
"#{tag_name_with_index(@options[:index])}[]"
else
"#{tag_name}[]"
end

hidden_name = @html_options[:name] || "#{tag_name(false, @options[:index])}[]"
@template_object.hidden_field_tag(hidden_name, "", id: nil)
end
end
Expand Down

0 comments on commit c3513a1

Please sign in to comment.