Skip to content

Commit

Permalink
Simplify form_for by removing *args and extract_options! (at asakusa.rb)
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Sep 10, 2010
1 parent e0e3adf commit 74b49e8
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -293,31 +293,32 @@ module FormHelper
# #
# If you don't need to attach a form to a model instance, then check out # If you don't need to attach a form to a model instance, then check out
# FormTagHelper#form_tag. # FormTagHelper#form_tag.
def form_for(record_or_name_or_array, *args, &proc) def form_for(record, record_object = nil, options = nil, &proc)
raise ArgumentError, "Missing block" unless block_given? raise ArgumentError, "Missing block" unless block_given?


options = args.extract_options! options, record_object = record_object, nil if record_object.is_a?(Hash)
options ||= {}


case record_or_name_or_array case record
when String, Symbol when String, Symbol
ActiveSupport::Deprecation.warn("Using form_for(:name, @resource) is deprecated. Please use form_for(@resource, :as => :name) instead.", caller) unless args.empty? 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_or_name_or_array object_name = record
object = record_object
when Array when Array
object = record_or_name_or_array.last object = record.last
object_name = options[:as] || ActiveModel::Naming.param_key(object) object_name = options[:as] || ActiveModel::Naming.singular(object)
apply_form_for_options!(record_or_name_or_array, options) apply_form_for_options!(record, options)
args.unshift object
else else
object = record_or_name_or_array object = record
object_name = options[:as] || ActiveModel::Naming.param_key(object) object_name = options[:as] || ActiveModel::Naming.singular(object)
apply_form_for_options!([object], options) apply_form_for_options!([object], options)
args.unshift object
end end


(options[:html] ||= {})[:remote] = true if options.delete(:remote) options[:html] ||= {}
options[:html][:remote] = options.delete(:remote)


output = form_tag(options.delete(:url) || {}, options.delete(:html) || {}) output = form_tag(options.delete(:url) || {}, options.delete(:html) || {})
output << fields_for(object_name, *(args << options), &proc) output << fields_for(object_name, object, options, &proc)
output.safe_concat('</form>') output.safe_concat('</form>')
end end


Expand Down

0 comments on commit 74b49e8

Please sign in to comment.