From cffbc4399288be2fb7e15f67680089dbb36a7152 Mon Sep 17 00:00:00 2001 From: Sandi Metz Date: Wed, 17 Jun 2009 09:20:55 -0400 Subject: [PATCH] remove form_for case statement --- actionpack/lib/action_view/helpers.rb | 2 + .../lib/action_view/helpers/form_helper.rb | 43 ++++++-------- .../helpers/form_helper_core_extensions.rb | 59 +++++++++++++++++++ .../action_view/helpers/prototype_helper.rb | 19 +----- 4 files changed, 81 insertions(+), 42 deletions(-) create mode 100644 actionpack/lib/action_view/helpers/form_helper_core_extensions.rb diff --git a/actionpack/lib/action_view/helpers.rb b/actionpack/lib/action_view/helpers.rb index 693ab7c2e01de..35259c5dcf84b 100644 --- a/actionpack/lib/action_view/helpers.rb +++ b/actionpack/lib/action_view/helpers.rb @@ -1,3 +1,5 @@ +require 'action_view/helpers/form_helper_core_extensions' + module ActionView #:nodoc: module Helpers #:nodoc: autoload :ActiveRecordHelper, 'action_view/helpers/active_record_helper' diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index a85751c6574cd..620dd22ca5a60 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -232,30 +232,29 @@ module FormHelper def form_for(record_or_name_or_array, *args, &proc) raise ArgumentError, "Missing block" unless block_given? + object, object_name, options = standardize_form_invocation!(record_or_name_or_array, args) + + concat(form_tag(options.delete(:url) || {}, options.delete(:html) || {})) + fields_for(object_name, *(args << options), &proc) + concat('') + end + + def standardize_form_invocation!(record_or_name_or_array, args) options = args.extract_options! - case record_or_name_or_array - when String, Symbol - object_name = record_or_name_or_array - when Array - object = record_or_name_or_array.last - object_name = ActionController::RecordIdentifier.singular_class_name(object) - apply_form_for_options!(record_or_name_or_array, options) - args.unshift object - else - object = record_or_name_or_array - object_name = ActionController::RecordIdentifier.singular_class_name(object) - apply_form_for_options!([object], options) + object = record_or_name_or_array.as_form_object + object_name = object.form_object_name + + if object.acts_like_model? + apply_form_for_options!(record_or_name_or_array.as_array, options) args.unshift object end - - concat(form_tag(options.delete(:url) || {}, options.delete(:html) || {})) - fields_for(object_name, *(args << options), &proc) - concat('') + + [object, object_name, options] end 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 = object_or_array.as_form_object html_options = if object.respond_to?(:new_record?) && object.new_record? @@ -300,14 +299,8 @@ def fields_for(record_or_name_or_array, *args, &block) raise ArgumentError, "Missing block" unless block_given? options = args.extract_options! - case record_or_name_or_array - when String, Symbol - object_name = record_or_name_or_array - object = args.first - else - object = record_or_name_or_array - object_name = ActionController::RecordIdentifier.singular_class_name(object) - end + object = record_or_name_or_array.as_fields_for_form_object(args) + object_name = record_or_name_or_array.form_object_name builder = options[:builder] || ActionView::Base.default_form_builder yield builder.new(object_name, object, self, options, block) diff --git a/actionpack/lib/action_view/helpers/form_helper_core_extensions.rb b/actionpack/lib/action_view/helpers/form_helper_core_extensions.rb new file mode 100644 index 0000000000000..8d6e9242f6961 --- /dev/null +++ b/actionpack/lib/action_view/helpers/form_helper_core_extensions.rb @@ -0,0 +1,59 @@ +class String + def form_object_name + self + end + + def as_fields_for_form_object(args) + args.first + end + + def acts_like_model? + false + end +end + +class Symbol + def form_object_name + self + end + + def as_fields_for_form_object(args) + args.first + end + + def acts_like_model? + false + end +end + +class Array + def as_form_object + last + end + + def as_array + self + end +end + +class Object + def as_form_object + self + end + + def as_fields_for_form_object(args) + as_form_object + end + + def form_object_name + ActionController::RecordIdentifier.singular_class_name(self) + end + + def as_array + [self] + end + + def acts_like_model? + true + end +end \ No newline at end of file diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 99676a9c271df..edebf49d9093d 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -374,23 +374,8 @@ def form_remote_tag(options = {}, &block) # # See FormHelper#form_for for additional semantics. def remote_form_for(record_or_name_or_array, *args, &proc) - options = args.extract_options! - - case record_or_name_or_array - when String, Symbol - object_name = record_or_name_or_array - when Array - object = record_or_name_or_array.last - object_name = ActionController::RecordIdentifier.singular_class_name(object) - apply_form_for_options!(record_or_name_or_array, options) - args.unshift object - else - object = record_or_name_or_array - object_name = ActionController::RecordIdentifier.singular_class_name(record_or_name_or_array) - apply_form_for_options!(object, options) - args.unshift object - end - + object, object_name, options = standardize_form_invocation!(record_or_name_or_array, args) + concat(form_remote_tag(options)) fields_for(object_name, *(args << options), &proc) concat('')