From 6e7b593992f8f6f8e783b87e7ab9d1d5a97063f7 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 14 May 2007 17:30:35 +0000 Subject: Added record identifications to FormHelper#form_for and PrototypeHelper#remote_form_for [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6731 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/base.rb | 2 + .../lib/action_controller/polymorphic_routes.rb | 8 +- .../lib/action_controller/record_identifier.rb | 2 +- actionpack/lib/action_view/helpers/form_helper.rb | 61 +++++++++++- .../lib/action_view/helpers/prototype_helper.rb | 12 ++- .../helpers/record_identification_helper.rb | 28 +++--- .../lib/action_view/helpers/record_tag_helper.rb | 108 +++++++++++---------- 7 files changed, 152 insertions(+), 69 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index a9d502e6f1..c922dc7284 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -999,6 +999,7 @@ module ActionController #:nodoc: # Redirects the browser to the target specified in +options+. This parameter can take one of three forms: # # * Hash: The URL will be generated by calling url_for with the +options+. + # * Record: The URL will be generated by calling url_for with the +options+, which will reference a named URL for that record. # * String starting with protocol:// (like http://): Is passed straight through as the target for redirection. # * String not containing a protocol: The current protocol and host is prepended to the string. # * :back: Back to the page that issued the request. Useful for forms that are triggered from multiple places. @@ -1006,6 +1007,7 @@ module ActionController #:nodoc: # # Examples: # redirect_to :action => "show", :id => 5 + # redirect_to post # redirect_to "http://www.rubyonrails.org" # redirect_to "/images/screenshot.jpg" # redirect_to :back diff --git a/actionpack/lib/action_controller/polymorphic_routes.rb b/actionpack/lib/action_controller/polymorphic_routes.rb index eeddc28d60..88bd7ab192 100644 --- a/actionpack/lib/action_controller/polymorphic_routes.rb +++ b/actionpack/lib/action_controller/polymorphic_routes.rb @@ -7,10 +7,14 @@ module ActionController case when options[:action] == "new" - url_writer.send(action_prefix(options) + RecordIdentifier.singular_class_name(record) + routing_type(options)) + url_writer.send( + action_prefix(options) + RecordIdentifier.singular_class_name(record) + routing_type(options) + ) when record.respond_to?(:new_record?) && record.new_record? - url_writer.send(RecordIdentifier.plural_class_name(record) + routing_type(options)) + url_writer.send( + action_prefix(options) + RecordIdentifier.plural_class_name(record) + routing_type(options) + ) else url_writer.send( diff --git a/actionpack/lib/action_controller/record_identifier.rb b/actionpack/lib/action_controller/record_identifier.rb index 1247665a8b..1a60becff3 100644 --- a/actionpack/lib/action_controller/record_identifier.rb +++ b/actionpack/lib/action_controller/record_identifier.rb @@ -63,7 +63,7 @@ module ActionController # # dom_class(Post.new(:id => 45), :edit) # => "edit_post_45" def dom_id(record, prefix = nil) - prefix ||= 'new' unless record.id + prefix ||= 'new' unless record.id [ prefix, singular_class_name(record), record.id ].compact * '_' end diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 3af7236b30..d307c194d2 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -103,6 +103,41 @@ module ActionView # The above form will then have the id attribute with the value person_form, which you can then # style with CSS or manipulate with JavaScript. # + # === Relying on record identification + # + # In addition to manually configuring the form_for call, you can also rely on record identification, which will use + # the conventions and named routes of that approach. Examples: + # + # <% form_for(@post) do |f| %> + # ... + # <% end %> + # + # This will expand to be the same as: + # + # <% form_for :post, @post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %> + # ... + # <% end %> + # + # And for new records: + # + # <% form_for(Post.new) do |f| %> + # ... + # <% end %> + # + # This will expand to be the same as: + # + # <% form_for :post, @post, :url => posts_path, :html => { :class => "new_post", :id => "new_post" } do |f| %> + # ... + # <% end %> + # + # You can also overwrite the individual conventions, like this: + # + # <% form_for(@post, :url => super_post_path(@post)) do |f| %> + # ... + # <% end %> + # + # === Customized form builders + # # You can also build forms using a customized FormBuilder class. Subclass FormBuilder and override or define some more helpers, # then use your custom builder. For example, let's say you made a helper to automatically add labels to form inputs. # @@ -120,13 +155,37 @@ module ActionView # end # # If you don't need to attach a form to a model instance, then check out FormTagHelper#form_tag. - def form_for(object_name, *args, &proc) + def form_for(record_or_name, *args, &proc) raise ArgumentError, "Missing block" unless block_given? + options = args.last.is_a?(Hash) ? args.pop : {} + + case record_or_name + when String, Symbol + object_name = record_or_name + else + object = record_or_name + object_name = ActionController::RecordIdentifier.singular_class_name(record_or_name) + apply_form_for_options!(object, options) + end + concat(form_tag(options.delete(:url) || {}, options.delete(:html) || {}), proc.binding) fields_for(object_name, *(args << options), &proc) concat('', proc.binding) end + + def apply_form_for_options!(object, options) #:nodoc: + html_options = if object.respond_to?(:new_record?) && object.new_record? + { :class => dom_class(object, :new), :id => dom_id(object), :method => :post } + else + { :class => dom_class(object, :edit), :id => dom_id(object, :edit), :method => :put } + end + + options[:html] ||= {} + options[:html].reverse_merge!(html_options) + + options[:url] ||= polymorphic_path(object, self) + end # Creates a scope around a specific model object like form_for, but doesn't create the form tags themselves. This makes # fields_for suitable for specifying additional model objects in the same form: diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 9fa27d6386..9bf9a71df2 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -181,8 +181,18 @@ module ActionView end # Works like form_remote_tag, but uses form_for semantics. - def remote_form_for(object_name, *args, &proc) + def remote_form_for(record_or_name, *args, &proc) options = args.last.is_a?(Hash) ? args.pop : {} + + case record_or_name + when String, Symbol + object_name = record_or_name + else + object = record_or_name + object_name = ActionController::RecordIdentifier.singular_class_name(record_or_name) + apply_form_for_options!(object, options) + end + concat(form_remote_tag(options), proc.binding) fields_for(object_name, *(args << options), &proc) concat('', proc.binding) diff --git a/actionpack/lib/action_view/helpers/record_identification_helper.rb b/actionpack/lib/action_view/helpers/record_identification_helper.rb index 2e5ff865cc..6c235bff3d 100644 --- a/actionpack/lib/action_view/helpers/record_identification_helper.rb +++ b/actionpack/lib/action_view/helpers/record_identification_helper.rb @@ -1,16 +1,20 @@ -module RecordIdentificationHelper - # See ActionController::RecordIdentifier.partial_path -- this is just a delegate to that for convenient access in the view. - def partial_path(*args, &block) - ActionController::RecordIdentifier.partial_path(*args, &block) - end +module ActionView + module Helpers + module RecordIdentificationHelper + # See ActionController::RecordIdentifier.partial_path -- this is just a delegate to that for convenient access in the view. + def partial_path(*args, &block) + ActionController::RecordIdentifier.partial_path(*args, &block) + end - # See ActionController::RecordIdentifier.dom_class -- this is just a delegate to that for convenient access in the view. - def dom_class(*args, &block) - ActionController::RecordIdentifier.dom_class(*args, &block) - end + # See ActionController::RecordIdentifier.dom_class -- this is just a delegate to that for convenient access in the view. + def dom_class(*args, &block) + ActionController::RecordIdentifier.dom_class(*args, &block) + end - # See ActionController::RecordIdentifier.dom_id -- this is just a delegate to that for convenient access in the view. - def dom_id(*args, &block) - ActionController::RecordIdentifier.dom_id(*args, &block) + # See ActionController::RecordIdentifier.dom_id -- this is just a delegate to that for convenient access in the view. + def dom_id(*args, &block) + ActionController::RecordIdentifier.dom_id(*args, &block) + end + end end end \ No newline at end of file diff --git a/actionpack/lib/action_view/helpers/record_tag_helper.rb b/actionpack/lib/action_view/helpers/record_tag_helper.rb index 29845ba147..61e51f50af 100644 --- a/actionpack/lib/action_view/helpers/record_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/record_tag_helper.rb @@ -1,55 +1,59 @@ -module RecordTagHelper - # Produces a wrapper DIV element with id and class parameters that - # relate to the specified ActiveRecord object. Usage example: - # - # <% div_for(@person, :class => "foo") do %> - # <%=h @person.name %> - # <% end %> - # - # produces: - # - #
Joe Bloggs
- # - def div_for(record, *args, &block) - content_tag_for(:div, record, *args, &block) - end +module ActionView + module Helpers + module RecordTagHelper + # Produces a wrapper DIV element with id and class parameters that + # relate to the specified ActiveRecord object. Usage example: + # + # <% div_for(@person, :class => "foo") do %> + # <%=h @person.name %> + # <% end %> + # + # produces: + # + #
Joe Bloggs
+ # + def div_for(record, *args, &block) + content_tag_for(:div, record, *args, &block) + end - # content_tag_for creates an HTML element with id and class parameters - # that relate to the specified ActiveRecord object. For example: - # - # <% content_tag_for(:tr, @person) do %> - # <%=h @person.first_name %> - # <%=h @person.last_name %> - # <% end %> - # - # would produce hthe following HTML (assuming @person is an instance of - # a Person object, with an id value of 123): - # - # .... - # - # If you require the HTML id attribute to have a prefix, you can specify it: - # - # <% content_tag_for(:tr, @person, :foo) do %> ... - # - # produces: - # - # ... - # - # content_tag_for also accepts a hash of options, which will be converted to - # additional HTML attributes. If you specify a +:class+ value, it will be combined - # with the default class name for your object. For example: - # - # <% content_tag_for(:li, @person, :class => "bar") %>... - # - # produces: - # - #
  • ... - # - def content_tag_for(tag_name, record, *args, &block) - prefix = args.first.is_a?(Hash) ? nil : args.shift - options = args.first.is_a?(Hash) ? args.shift : {} - concat content_tag(tag_name, capture(&block), - options.merge({ :class => "#{dom_class(record)} #{options[:class]}".strip, :id => dom_id(record, prefix) })), - block.binding + # content_tag_for creates an HTML element with id and class parameters + # that relate to the specified ActiveRecord object. For example: + # + # <% content_tag_for(:tr, @person) do %> + # <%=h @person.first_name %> + # <%=h @person.last_name %> + # <% end %> + # + # would produce hthe following HTML (assuming @person is an instance of + # a Person object, with an id value of 123): + # + # .... + # + # If you require the HTML id attribute to have a prefix, you can specify it: + # + # <% content_tag_for(:tr, @person, :foo) do %> ... + # + # produces: + # + # ... + # + # content_tag_for also accepts a hash of options, which will be converted to + # additional HTML attributes. If you specify a +:class+ value, it will be combined + # with the default class name for your object. For example: + # + # <% content_tag_for(:li, @person, :class => "bar") %>... + # + # produces: + # + #
  • ... + # + def content_tag_for(tag_name, record, *args, &block) + prefix = args.first.is_a?(Hash) ? nil : args.shift + options = args.first.is_a?(Hash) ? args.shift : {} + concat content_tag(tag_name, capture(&block), + options.merge({ :class => "#{dom_class(record)} #{options[:class]}".strip, :id => dom_id(record, prefix) })), + block.binding + end + end end end \ No newline at end of file -- cgit v1.2.3