Browse files

Refactor form_for helper

  • Loading branch information...
1 parent 8a16485 commit eb8a24a375a25d43b10459af2fef7641d0746738 @carlosantoniodasilva carlosantoniodasilva committed with josevalim Sep 25, 2010
Showing with 11 additions and 21 deletions.
  1. +11 −21 actionpack/lib/action_view/helpers/form_helper.rb
View
32 actionpack/lib/action_view/helpers/form_helper.rb
@@ -298,29 +298,24 @@ def form_for(record, record_object = nil, options = nil, &proc)
options, record_object = record_object, nil if record_object.is_a?(Hash)
options ||= {}
+ options[:html] ||= {}
case record
when String, Symbol
ActiveSupport::Deprecation.warn("Using form_for(:name, @resource) is deprecated. Please use form_for(@resource, :as => :name) instead.", caller) if record_object
object_name = record
object = record_object
- when Array
- object = record.last
- object_name = options[:as] || ActiveModel::Naming.param_key(object)
- apply_form_for_options!(record, options)
else
- object = record
+ object = record.is_a?(Array) ? record.last : record
object_name = options[:as] || ActiveModel::Naming.param_key(object)
- apply_form_for_options!([object], options)
+ apply_form_for_options!(record, options)
end
- options[:html] ||= {}
options[:html][:remote] = options.delete(:remote)
-
builder = options[:parent_builder] = instantiate_builder(object_name, object, options, &proc)
fields_for = fields_for(object_name, object, options, &proc)
default_options = builder.multipart? ? { :multipart => true } : {}
- output = form_tag(options.delete(:url) || {}, default_options.merge!(options.delete(:html) || {}))
+ output = form_tag(options.delete(:url) || {}, default_options.merge!(options.delete(:html)))
output << fields_for
output.safe_concat('</form>')
end
@@ -329,23 +324,18 @@ def apply_form_for_options!(object_or_array, options) #:nodoc:
object = object_or_array.is_a?(Array) ? object_or_array.last : object_or_array
object = convert_to_model(object)
- html_options =
- if object.respond_to?(:persisted?) && object.persisted?
- { :class => options[:as] ? "#{options[:as]}_edit" : dom_class(object, :edit),
- :id => options[:as] ? "#{options[:as]}_edit" : dom_id(object, :edit),
- :method => :put }
- else
- { :class => options[:as] ? "#{options[:as]}_new" : dom_class(object, :new),
- :id => options[:as] ? "#{options[:as]}_new" : dom_id(object),
- :method => :post }
- end
-
- options[:html] ||= {}
+ as = options[:as]
+ action, method = object.respond_to?(:persisted?) && object.persisted? ? [:edit, :put] : [:new, :post]
+ html_options = { :class => as ? "#{as}_#{action}" : dom_class(object, action),
+ :id => as ? "#{as}_#{action}" : dom_id(object, action),
+ :method => method }
options[:html].reverse_merge!(html_options)
+
options[:url] ||= options[:format] ?
polymorphic_path(object_or_array, :format => options.delete(:format)) :
polymorphic_path(object_or_array)
end
+ private :apply_form_for_options!
# Creates a scope around a specific model object like form_for, but
# doesn't create the form tags themselves. This makes fields_for suitable

0 comments on commit eb8a24a

Please sign in to comment.