Skip to content

Commit

Permalink
remove form_for case statement
Browse files Browse the repository at this point in the history
  • Loading branch information
skmetz committed Jun 17, 2009
1 parent d6b9f84 commit cffbc43
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 42 deletions.
2 changes: 2 additions & 0 deletions 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'
Expand Down
43 changes: 18 additions & 25 deletions actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -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('</form>')
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('</form>')

[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?
Expand Down Expand Up @@ -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)
Expand Down
59 changes: 59 additions & 0 deletions 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
19 changes: 2 additions & 17 deletions actionpack/lib/action_view/helpers/prototype_helper.rb
Expand Up @@ -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('</form>')
Expand Down

1 comment on commit cffbc43

@zoras
Copy link

@zoras zoras commented on cffbc43 Mar 15, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.