diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2010-09-25 23:49:40 +0800 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-09-26 02:13:46 +0800 |
commit | eb8a24a375a25d43b10459af2fef7641d0746738 (patch) | |
tree | 8603d4decdbdb5fcc01980c4e0fe3a7d3e4e468e /actionpack | |
parent | 8a16485a9667f9278b7d03f4556edbdeaf27ec5a (diff) | |
download | rails-eb8a24a375a25d43b10459af2fef7641d0746738.tar.gz rails-eb8a24a375a25d43b10459af2fef7641d0746738.tar.bz2 rails-eb8a24a375a25d43b10459af2fef7641d0746738.zip |
Refactor form_for helper
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 32 |
1 files changed, 11 insertions, 21 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 0937075edf..85ba6feb31 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -298,29 +298,24 @@ module ActionView 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 @@ module ActionView 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 |