Refactor wrapping collections

This avoids calculating wrapper class every time when there is no
wrapper tag involved, and also improves readability.
This commit is contained in:
Carlos Antonio da Silva 2011-11-07 17:19:30 -02:00
parent 2f09d1279b
commit 40f81c2da1
1 changed files with 13 additions and 4 deletions

View File

@ -138,9 +138,7 @@ module SimpleForm
end
def render_collection(attribute, collection, value_method, text_method, options={}, html_options={}) #:nodoc:
collection_wrapper_class = [SimpleForm.collection_wrapper_class, options[:collection_wrapper_class]].compact.presence
collection_wrapper_tag = options.key?(:collection_wrapper_tag) ? options[:collection_wrapper_tag] : SimpleForm.collection_wrapper_tag
item_wrapper_tag = options.key?(:item_wrapper_tag) ? options[:item_wrapper_tag] : SimpleForm.item_wrapper_tag
item_wrapper_tag = options.key?(:item_wrapper_tag) ? options[:item_wrapper_tag] : SimpleForm.item_wrapper_tag
rendered_collection = collection.map do |item|
value = value_for_collection(item, value_method)
@ -152,12 +150,23 @@ module SimpleForm
item_wrapper_tag ? @template.content_tag(item_wrapper_tag, rendered_item) : rendered_item
end.join.html_safe
collection_wrapper_tag ? @template.content_tag(collection_wrapper_tag, rendered_collection, :class => collection_wrapper_class) : rendered_collection
wrap_rendered_collection(rendered_collection, options)
end
def value_for_collection(item, value) #:nodoc:
value.respond_to?(:call) ? value.call(item) : item.send(value)
end
def wrap_rendered_collection(collection, options)
wrapper_tag = options.key?(:collection_wrapper_tag) ? options[:collection_wrapper_tag] : SimpleForm.collection_wrapper_tag
if wrapper_tag
wrapper_class = [SimpleForm.collection_wrapper_class, options[:collection_wrapper_class]].compact.presence
@template.content_tag(wrapper_tag, collection, :class => wrapper_class)
else
collection
end
end
end
end
end