aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG12
-rw-r--r--actionpack/lib/action_controller/integration.rb15
-rw-r--r--actionpack/lib/action_controller/middlewares.rb1
-rw-r--r--actionpack/lib/action_controller/rack_ext/parse_query.rb1
-rw-r--r--actionpack/lib/action_controller/resources.rb35
-rw-r--r--actionpack/lib/action_controller/test_process.rb23
-rw-r--r--actionpack/lib/action_controller/url_encoded_pair_parser.rb2
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb196
-rw-r--r--actionpack/test/abstract_unit.rb2
-rw-r--r--actionpack/test/controller/integration_test.rb14
-rw-r--r--actionpack/test/controller/request/url_encoded_params_parsing_test.rb36
-rw-r--r--actionpack/test/controller/resources_test.rb43
-rw-r--r--actionpack/test/controller/session/test_session_test.rb58
-rw-r--r--actionpack/test/controller/test_test.rb23
-rw-r--r--actionpack/test/template/form_helper_test.rb141
-rw-r--r--actionpack/test/template/render_test.rb2
16 files changed, 513 insertions, 91 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index e9e18a8f6b..0d3f04373f 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,17 @@
*2.3.0 [Edge]*
+* Make the form_for and fields_for helpers support the new Active Record nested update options. #1202 [Eloy Duran]
+
+ <% form_for @person do |person_form| %>
+ ...
+ <% person_form.fields_for :projects do |project_fields| %>
+ <% if project_fields.object.active? %>
+ Name: <%= project_fields.text_field :name %>
+ <% end %>
+ <% end %>
+ <% end %>
+
+
* Added grouped_options_for_select helper method for wrapping option tags in optgroups. #977 [Jon Crawford]
* Implement HTTP Digest authentication. #1230 [Gregg Kellogg, Pratik Naik] Example :
diff --git a/actionpack/lib/action_controller/integration.rb b/actionpack/lib/action_controller/integration.rb
index 163ba84a3e..a0e894108d 100644
--- a/actionpack/lib/action_controller/integration.rb
+++ b/actionpack/lib/action_controller/integration.rb
@@ -26,6 +26,9 @@ module ActionController
# The status message that accompanied the status code of the last request.
attr_reader :status_message
+ # The body of the last request.
+ attr_reader :body
+
# The URI of the last request.
attr_reader :path
@@ -308,7 +311,11 @@ module ActionController
ActionController::Base.clear_last_instantiation!
- app = Rack::Lint.new(@application)
+ app = @application
+ # Rack::Lint doesn't accept String headers or bodies in Ruby 1.9
+ unless RUBY_VERSION >= '1.9.0' && Rack.release <= '0.9.0'
+ app = Rack::Lint.new(app)
+ end
status, headers, body = app.call(env)
@request_count += 1
@@ -326,7 +333,11 @@ module ActionController
end
@body = ""
- body.each { |part| @body << part }
+ if body.is_a?(String)
+ @body << body
+ else
+ body.each { |part| @body << part }
+ end
if @controller = ActionController::Base.last_instantiation
@request = @controller.request
diff --git a/actionpack/lib/action_controller/middlewares.rb b/actionpack/lib/action_controller/middlewares.rb
index f9cfc2b18e..8ea1b5c7ce 100644
--- a/actionpack/lib/action_controller/middlewares.rb
+++ b/actionpack/lib/action_controller/middlewares.rb
@@ -19,3 +19,4 @@ end
use "ActionController::RewindableInput"
use "ActionController::ParamsParser"
use "Rack::MethodOverride"
+use "Rack::Head"
diff --git a/actionpack/lib/action_controller/rack_ext/parse_query.rb b/actionpack/lib/action_controller/rack_ext/parse_query.rb
index 2f21a57770..b1acef8e72 100644
--- a/actionpack/lib/action_controller/rack_ext/parse_query.rb
+++ b/actionpack/lib/action_controller/rack_ext/parse_query.rb
@@ -10,7 +10,6 @@ module Rack
def parse_query(qs, d = '&;')
qs = qs.dup
qs.chop! if qs[-1] == 0
- qs.gsub!(/&_=$/, '')
parse_query_without_ajax_body_cleanup(qs, d)
end
module_function :parse_query
diff --git a/actionpack/lib/action_controller/resources.rb b/actionpack/lib/action_controller/resources.rb
index e8988aa737..3af21967df 100644
--- a/actionpack/lib/action_controller/resources.rb
+++ b/actionpack/lib/action_controller/resources.rb
@@ -42,7 +42,7 @@ module ActionController
#
# Read more about REST at http://en.wikipedia.org/wiki/Representational_State_Transfer
module Resources
- INHERITABLE_OPTIONS = :namespace, :shallow, :actions
+ INHERITABLE_OPTIONS = :namespace, :shallow
class Resource #:nodoc:
DEFAULT_ACTIONS = :index, :create, :new, :edit, :show, :update, :destroy
@@ -119,7 +119,7 @@ module ActionController
end
def has_action?(action)
- !DEFAULT_ACTIONS.include?(action) || @options[:actions].nil? || @options[:actions].include?(action)
+ !DEFAULT_ACTIONS.include?(action) || action_allowed?(action)
end
protected
@@ -135,24 +135,29 @@ module ActionController
end
def set_allowed_actions
- only = @options.delete(:only)
- except = @options.delete(:except)
+ only, except = @options.values_at(:only, :except)
+ @allowed_actions ||= {}
- if only && except
- raise ArgumentError, 'Please supply either :only or :except, not both.'
- elsif only == :all || except == :none
- options[:actions] = DEFAULT_ACTIONS
+ if only == :all || except == :none
+ only = nil
+ except = []
elsif only == :none || except == :all
- options[:actions] = []
- elsif only
- options[:actions] = DEFAULT_ACTIONS & Array(only).map(&:to_sym)
+ only = []
+ except = nil
+ end
+
+ if only
+ @allowed_actions[:only] = Array(only).map(&:to_sym)
elsif except
- options[:actions] = DEFAULT_ACTIONS - Array(except).map(&:to_sym)
- else
- # leave options[:actions] alone
+ @allowed_actions[:except] = Array(except).map(&:to_sym)
end
end
+ def action_allowed?(action)
+ only, except = @allowed_actions.values_at(:only, :except)
+ (!only || only.include?(action)) && (!except || !except.include?(action))
+ end
+
def set_prefixes
@path_prefix = options.delete(:path_prefix)
@name_prefix = options.delete(:name_prefix)
@@ -403,8 +408,6 @@ module ActionController
# # --> POST /posts/1/comments (maps to the CommentsController#create action)
# # --> PUT /posts/1/comments/1 (fails)
#
- # The <tt>:only</tt> and <tt>:except</tt> options are inherited by any nested resource(s).
- #
# If <tt>map.resources</tt> is called with multiple resources, they all get the same options applied.
#
# Examples:
diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb
index ea17363c47..4b5fc3a3c1 100644
--- a/actionpack/lib/action_controller/test_process.rb
+++ b/actionpack/lib/action_controller/test_process.rb
@@ -15,7 +15,7 @@ module ActionController #:nodoc:
end
def reset_session
- @session = TestSession.new
+ @session.reset
end
# Wraps raw_post in a StringIO.
@@ -284,9 +284,13 @@ module ActionController #:nodoc:
attr_accessor :session_id
def initialize(attributes = nil)
- @session_id = ''
- attributes ||= {}
- replace(attributes.stringify_keys)
+ reset_session_id
+ replace_attributes(attributes)
+ end
+
+ def reset
+ reset_session_id
+ replace_attributes({ })
end
def data
@@ -322,6 +326,17 @@ module ActionController #:nodoc:
def close
ActiveSupport::Deprecation.warn('sessions should no longer be closed', caller)
end
+
+ private
+
+ def reset_session_id
+ @session_id = ''
+ end
+
+ def replace_attributes(attributes = nil)
+ attributes ||= {}
+ replace(attributes.stringify_keys)
+ end
end
# Essentially generates a modified Tempfile object similar to the object
diff --git a/actionpack/lib/action_controller/url_encoded_pair_parser.rb b/actionpack/lib/action_controller/url_encoded_pair_parser.rb
index 57594c4259..b17b8a31aa 100644
--- a/actionpack/lib/action_controller/url_encoded_pair_parser.rb
+++ b/actionpack/lib/action_controller/url_encoded_pair_parser.rb
@@ -46,7 +46,7 @@ module ActionController
when Array
value.map { |v| get_typed_value(v) }
when Hash
- if value.has_key?(:tempfile) && value[:filename].any?
+ if value.has_key?(:tempfile) && !value[:filename].blank?
upload = value[:tempfile]
upload.extend(UploadedFile)
upload.original_path = value[:filename]
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index a85751c657..2ac2427884 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -269,10 +269,12 @@ module ActionView
options[:url] ||= polymorphic_path(object_or_array)
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:
+ # 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.
+ #
+ # === Generic Examples
#
- # ==== Examples
# <% form_for @person, :url => { :action => "update" } do |person_form| %>
# First name: <%= person_form.text_field :first_name %>
# Last name : <%= person_form.text_field :last_name %>
@@ -282,20 +284,166 @@ module ActionView
# <% end %>
# <% end %>
#
- # ...or if you have an object that needs to be represented as a different parameter, like a Client that acts as a Person:
+ # ...or if you have an object that needs to be represented as a different
+ # parameter, like a Client that acts as a Person:
#
# <% fields_for :person, @client do |permission_fields| %>
# Admin?: <%= permission_fields.check_box :admin %>
# <% end %>
#
- # ...or if you don't have an object, just a name of the parameter
+ # ...or if you don't have an object, just a name of the parameter:
#
# <% fields_for :person do |permission_fields| %>
# Admin?: <%= permission_fields.check_box :admin %>
# <% end %>
#
- # Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base,
- # like FormOptionHelper#collection_select and DateHelper#datetime_select.
+ # Note: This also works for the methods in FormOptionHelper and
+ # DateHelper that are designed to work with an object as base, like
+ # FormOptionHelper#collection_select and DateHelper#datetime_select.
+ #
+ # === Nested Attributes Examples
+ #
+ # When the object belonging to the current scope has a nested attribute
+ # writer for a certain attribute, fields_for will yield a new scope
+ # for that attribute. This allows you to create forms that set or change
+ # the attributes of a parent object and its associations in one go.
+ #
+ # Nested attribute writers are normal setter methods named after an
+ # association. The most common way of defining these writers is either
+ # with +accepts_nested_attributes_for+ in a model definition or by
+ # defining a method with the proper name. For example: the attribute
+ # writer for the association <tt>:address</tt> is called
+ # <tt>address_attributes=</tt>.
+ #
+ # Whether a one-to-one or one-to-many style form builder will be yielded
+ # depends on whether the normal reader method returns a _single_ object
+ # or an _array_ of objects.
+ #
+ # ==== One-to-one
+ #
+ # Consider a Person class which returns a _single_ Address from the
+ # <tt>address</tt> reader method and responds to the
+ # <tt>address_attributes=</tt> writer method:
+ #
+ # class Person
+ # def address
+ # @address
+ # end
+ #
+ # def address_attributes=(attributes)
+ # # Process the attributes hash
+ # end
+ # end
+ #
+ # This model can now be used with a nested fields_for, like so:
+ #
+ # <% form_for @person, :url => { :action => "update" } do |person_form| %>
+ # ...
+ # <% person_form.fields_for :address do |address_fields| %>
+ # Street : <%= address_fields.text_field :street %>
+ # Zip code: <%= address_fields.text_field :zip_code %>
+ # <% end %>
+ # <% end %>
+ #
+ # When address is already an association on a Person you can use
+ # +accepts_nested_attributes_for+ to define the writer method for you:
+ #
+ # class Person < ActiveRecord::Base
+ # has_one :address
+ # accepts_nested_attributes_for :address
+ # end
+ #
+ # If you want to destroy the associated model through the form, you have
+ # to enable it first using the <tt>:allow_destroy</tt> option for
+ # +accepts_nested_attributes_for+:
+ #
+ # class Person < ActiveRecord::Base
+ # has_one :address
+ # accepts_nested_attributes_for :address, :allow_destroy => true
+ # end
+ #
+ # Now, when you use a form element with the <tt>_delete</tt> parameter,
+ # with a value that evaluates to +true+, you will destroy the associated
+ # model (eg. 1, '1', true, or 'true'):
+ #
+ # <% form_for @person, :url => { :action => "update" } do |person_form| %>
+ # ...
+ # <% person_form.fields_for :address do |address_fields| %>
+ # ...
+ # Delete: <%= address_fields.check_box :_delete %>
+ # <% end %>
+ # <% end %>
+ #
+ # ==== One-to-many
+ #
+ # Consider a Person class which returns an _array_ of Project instances
+ # from the <tt>projects</tt> reader method and responds to the
+ # <tt>projects_attributes=</tt> writer method:
+ #
+ # class Person
+ # def projects
+ # [@project1, @project2]
+ # end
+ #
+ # def projects_attributes=(attributes)
+ # # Process the attributes hash
+ # end
+ # end
+ #
+ # This model can now be used with a nested fields_for. The block given to
+ # the nested fields_for call will be repeated for each instance in the
+ # collection:
+ #
+ # <% form_for @person, :url => { :action => "update" } do |person_form| %>
+ # ...
+ # <% person_form.fields_for :projects do |project_fields| %>
+ # <% if project_fields.object.active? %>
+ # Name: <%= project_fields.text_field :name %>
+ # <% end %>
+ # <% end %>
+ # <% end %>
+ #
+ # It's also possible to specify the instance to be used:
+ #
+ # <% form_for @person, :url => { :action => "update" } do |person_form| %>
+ # ...
+ # <% @person.projects.each do |project| %>
+ # <% if project.active? %>
+ # <% person_form.fields_for :projects, project do |project_fields| %>
+ # Name: <%= project_fields.text_field :name %>
+ # <% end %>
+ # <% end %>
+ # <% end %>
+ # <% end %>
+ #
+ # When projects is already an association on Person you can use
+ # +accepts_nested_attributes_for+ to define the writer method for you:
+ #
+ # class Person < ActiveRecord::Base
+ # has_many :projects
+ # accepts_nested_attributes_for :projects
+ # end
+ #
+ # If you want to destroy any of the associated models through the
+ # form, you have to enable it first using the <tt>:allow_destroy</tt>
+ # option for +accepts_nested_attributes_for+:
+ #
+ # class Person < ActiveRecord::Base
+ # has_many :projects
+ # accepts_nested_attributes_for :projects, :allow_destroy => true
+ # end
+ #
+ # This will allow you to specify which models to destroy in the
+ # attributes hash by adding a form element for the <tt>_delete</tt>
+ # parameter with a value that evaluates to +true+
+ # (eg. 1, '1', true, or 'true'):
+ #
+ # <% form_for @person, :url => { :action => "update" } do |person_form| %>
+ # ...
+ # <% person_form.fields_for :projects do |project_fields| %>
+ # Delete: <%= project_fields.check_box :_delete %>
+ # <% end %>
+ # <% end %>
def fields_for(record_or_name_or_array, *args, &block)
raise ArgumentError, "Missing block" unless block_given?
options = args.extract_options!
@@ -760,7 +908,11 @@ module ActionView
case record_or_name_or_array
when String, Symbol
- name = "#{object_name}#{index}[#{record_or_name_or_array}]"
+ if nested_attributes_association?(record_or_name_or_array)
+ return fields_for_with_nested_attributes(record_or_name_or_array, args, block)
+ else
+ name = "#{object_name}#{index}[#{record_or_name_or_array}]"
+ end
when Array
object = record_or_name_or_array.last
name = "#{object_name}#{index}[#{ActionController::RecordIdentifier.singular_class_name(object)}]"
@@ -802,6 +954,32 @@ module ActionView
def objectify_options(options)
@default_options.merge(options.merge(:object => @object))
end
+
+ def nested_attributes_association?(association_name)
+ @object.respond_to?("#{association_name}_attributes=")
+ end
+
+ def fields_for_with_nested_attributes(association_name, args, block)
+ name = "#{object_name}[#{association_name}_attributes]"
+ association = @object.send(association_name)
+
+ if association.is_a?(Array)
+ children = args.first.respond_to?(:new_record?) ? [args.first] : association
+
+ children.map do |child|
+ child_name = "#{name}[#{ child.new_record? ? new_child_id : child.id }]"
+ @template.fields_for(child_name, child, *args, &block)
+ end.join
+ else
+ @template.fields_for(name, association, *args, &block)
+ end
+ end
+
+ def new_child_id
+ value = (@child_counter ||= 1)
+ @child_counter += 1
+ "new_#{value}"
+ end
end
end
@@ -809,4 +987,4 @@ module ActionView
cattr_accessor :default_form_builder
self.default_form_builder = ::ActionView::Helpers::FormBuilder
end
-end
+end \ No newline at end of file
diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb
index 4baebcb4d1..882d2cd6a8 100644
--- a/actionpack/test/abstract_unit.rb
+++ b/actionpack/test/abstract_unit.rb
@@ -34,7 +34,7 @@ ActionController::Base.session_store = nil
# Register danish language for testing
I18n.backend.store_translations 'da', {}
-ORIGINAL_LOCALES = I18n.available_locales
+ORIGINAL_LOCALES = I18n.available_locales.map(&:to_s).sort
FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
ActionController::Base.view_paths = FIXTURE_LOAD_PATH
diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb
index 4f07cbee47..dc2c6fae49 100644
--- a/actionpack/test/controller/integration_test.rb
+++ b/actionpack/test/controller/integration_test.rb
@@ -266,6 +266,7 @@ class IntegrationProcessTest < ActionController::IntegrationTest
assert_response :success
assert_response :ok
assert_equal({}, cookies)
+ assert_equal "OK", body
assert_equal "OK", response.body
assert_kind_of HTML::Document, html_document
assert_equal 1, request_count
@@ -281,6 +282,7 @@ class IntegrationProcessTest < ActionController::IntegrationTest
assert_response :success
assert_response :created
assert_equal({}, cookies)
+ assert_equal "Created", body
assert_equal "Created", response.body
assert_kind_of HTML::Document, html_document
assert_equal 1, request_count
@@ -360,6 +362,18 @@ class IntegrationProcessTest < ActionController::IntegrationTest
end
end
+ def test_head
+ with_test_route_set do
+ head '/get'
+ assert_equal 200, status
+ assert_equal "", body
+
+ head '/post'
+ assert_equal 201, status
+ assert_equal "", body
+ end
+ end
+
private
def with_test_route_set
with_routing do |set|
diff --git a/actionpack/test/controller/request/url_encoded_params_parsing_test.rb b/actionpack/test/controller/request/url_encoded_params_parsing_test.rb
index 89239687de..7e6099a041 100644
--- a/actionpack/test/controller/request/url_encoded_params_parsing_test.rb
+++ b/actionpack/test/controller/request/url_encoded_params_parsing_test.rb
@@ -80,36 +80,6 @@ class UrlEncodedParamsParsingTest < ActionController::IntegrationTest
assert_parses expected, query
end
- test "parses params with non alphanumeric name" do
- query = "a/b[c]=d"
- expected = { "a/b" => { "c" => "d" }}
- assert_parses expected, query
- end
-
- test "parses params with single brackets in the middle" do
- query = "a/b[c]d=e"
- expected = { "a/b" => {} }
- assert_parses expected, query
- end
-
- test "parses params with separated brackets" do
- query = "a/b@[c]d[e]=f"
- expected = { "a/b@" => { }}
- assert_parses expected, query
- end
-
- test "parses params with separated brackets and array" do
- query = "a/b@[c]d[e][]=f"
- expected = { "a/b@" => { }}
- assert_parses expected, query
- end
-
- test "parses params with unmatched brackets and array" do
- query = "a/b@[c][d[e][]=f"
- expected = { "a/b@" => { "c" => { }}}
- assert_parses expected, query
- end
-
test "parses params with nil key" do
query = "=&test2=value1"
expected = { "test2" => "value1" }
@@ -156,12 +126,6 @@ class UrlEncodedParamsParsingTest < ActionController::IntegrationTest
assert_parses expected, query
end
- test "parses params with Prototype's hack around Safari 2 trailing null character" do
- query = "selected[]=1&selected[]=2&selected[]=3&_="
- expected = { "selected" => [ "1", "2", "3" ] }
- assert_parses expected, query
- end
-
test "passes through rack middleware and parses params" do
with_muck_middleware do
assert_parses({ "a" => { "b" => "c" } }, "a[b]=c")
diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb
index 8dedeb23f6..ae2639d245 100644
--- a/actionpack/test/controller/resources_test.rb
+++ b/actionpack/test/controller/resources_test.rb
@@ -942,19 +942,6 @@ class ResourcesTest < ActionController::TestCase
end
end
- def test_nested_resource_inherits_only_show_action
- with_routing do |set|
- set.draw do |map|
- map.resources :products, :only => :show do |product|
- product.resources :images
- end
- end
-
- assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
- assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
- end
- end
-
def test_nested_resource_has_only_show_and_member_action
with_routing do |set|
set.draw do |map|
@@ -971,7 +958,7 @@ class ResourcesTest < ActionController::TestCase
end
end
- def test_nested_resource_ignores_only_option
+ def test_nested_resource_does_not_inherit_only_option
with_routing do |set|
set.draw do |map|
map.resources :products, :only => :show do |product|
@@ -984,7 +971,20 @@ class ResourcesTest < ActionController::TestCase
end
end
- def test_nested_resource_ignores_except_option
+ def test_nested_resource_does_not_inherit_only_option_by_default
+ with_routing do |set|
+ set.draw do |map|
+ map.resources :products, :only => :show do |product|
+ product.resources :images
+ end
+ end
+
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destory], [], 'products/1/images')
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
+ end
+ end
+
+ def test_nested_resource_does_not_inherit_except_option
with_routing do |set|
set.draw do |map|
map.resources :products, :except => :show do |product|
@@ -997,6 +997,19 @@ class ResourcesTest < ActionController::TestCase
end
end
+ def test_nested_resource_does_not_inherit_except_option_by_default
+ with_routing do |set|
+ set.draw do |map|
+ map.resources :products, :except => :show do |product|
+ product.resources :images
+ end
+ end
+
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
+ end
+ end
+
def test_default_singleton_restful_route_uses_get
with_routing do |set|
set.draw do |map|
diff --git a/actionpack/test/controller/session/test_session_test.rb b/actionpack/test/controller/session/test_session_test.rb
new file mode 100644
index 0000000000..83103be3ec
--- /dev/null
+++ b/actionpack/test/controller/session/test_session_test.rb
@@ -0,0 +1,58 @@
+require 'abstract_unit'
+require 'stringio'
+
+class ActionController::TestSessionTest < ActiveSupport::TestCase
+
+ def test_calling_delete_without_parameters_raises_deprecation_warning_and_calls_to_clear_test_session
+ assert_deprecated(/use clear instead/){ ActionController::TestSession.new.delete }
+ end
+
+ def test_calling_update_without_parameters_raises_deprecation_warning_and_calls_to_clear_test_session
+ assert_deprecated(/use replace instead/){ ActionController::TestSession.new.update }
+ end
+
+ def test_calling_close_raises_deprecation_warning
+ assert_deprecated(/sessions should no longer be closed/){ ActionController::TestSession.new.close }
+ end
+
+ def test_defaults
+ session = ActionController::TestSession.new
+ assert_equal({}, session.data)
+ assert_equal('', session.session_id)
+ end
+
+ def test_ctor_allows_setting
+ session = ActionController::TestSession.new({:one => 'one', :two => 'two'})
+ assert_equal('one', session[:one])
+ assert_equal('two', session[:two])
+ end
+
+ def test_setting_session_item_sets_item
+ session = ActionController::TestSession.new
+ session[:key] = 'value'
+ assert_equal('value', session[:key])
+ end
+
+ def test_calling_delete_removes item
+ session = ActionController::TestSession.new
+ session[:key] = 'value'
+ assert_equal('value', session[:key])
+ session.delete(:key)
+ assert_nil(session[:key])
+ end
+
+ def test_calling_update_with_params_passes_to_attributes
+ session = ActionController::TestSession.new()
+ session.update('key' => 'value')
+ assert_equal('value', session[:key])
+ end
+
+ def test_clear_emptys_session
+ params = {:one => 'one', :two => 'two'}
+ session = ActionController::TestSession.new({:one => 'one', :two => 'two'})
+ session.clear
+ assert_nil(session[:one])
+ assert_nil(session[:two])
+ end
+
+end \ No newline at end of file
diff --git a/actionpack/test/controller/test_test.rb b/actionpack/test/controller/test_test.rb
index ee7b8ade8c..65c894c2e7 100644
--- a/actionpack/test/controller/test_test.rb
+++ b/actionpack/test/controller/test_test.rb
@@ -23,6 +23,11 @@ class TestTest < ActionController::TestCase
render :text => 'Success'
end
+ def reset_the_session
+ reset_session
+ render :text => 'ignore me'
+ end
+
def render_raw_post
raise ActiveSupport::TestCase::Assertion, "#raw_post is blank" if request.raw_post.blank?
render :text => request.raw_post
@@ -171,6 +176,24 @@ XML
assert_equal 'value2', session[:symbol]
end
+ def test_session_is_cleared_from_controller_after_reset_session
+ process :set_session
+ process :reset_the_session
+ assert_equal Hash.new, @controller.session.to_hash
+ end
+
+ def test_session_is_cleared_from_response_after_reset_session
+ process :set_session
+ process :reset_the_session
+ assert_equal Hash.new, @response.session.to_hash
+ end
+
+ def test_session_is_cleared_from_request_after_reset_session
+ process :set_session
+ process :reset_the_session
+ assert_equal Hash.new, @request.session.to_hash
+ end
+
def test_process_with_request_uri_with_no_params
process :test_uri
assert_equal "/test_test/test/test_uri", @response.body
diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb
index 9454fd7e91..33a542af7e 100644
--- a/actionpack/test/template/form_helper_test.rb
+++ b/actionpack/test/template/form_helper_test.rb
@@ -15,21 +15,31 @@ silence_warnings do
def new_record?
@new_record
end
+
+ attr_accessor :author
+ def author_attributes=(attributes); end
+
+ attr_accessor :comments
+ def comments_attributes=(attributes); end
end
class Comment
attr_reader :id
attr_reader :post_id
+ def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end
def save; @id = 1; @post_id = 1 end
def new_record?; @id.nil? end
def to_param; @id; end
def name
- @id.nil? ? 'new comment' : "comment ##{@id}"
+ @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
end
end
-end
-class Comment::Nested < Comment; end
+ class Author < Comment
+ attr_accessor :post
+ def post_attributes=(attributes); end
+ end
+end
class FormHelperTest < ActionView::TestCase
tests ActionView::Helpers::FormHelper
@@ -479,7 +489,7 @@ class FormHelperTest < ActionView::TestCase
assert_dom_equal expected, output_buffer
end
- def test_nested_fields_for_with_index
+ def test_form_for_with_index_and_nested_fields_for
form_for(:post, @post, :index => 1) do |f|
f.fields_for(:comment, @post) do |c|
concat c.text_field(:title)
@@ -558,6 +568,127 @@ class FormHelperTest < ActionView::TestCase
assert_dom_equal expected, output_buffer
end
+ def test_nested_fields_for_with_a_new_record_on_a_nested_attributes_one_to_one_association
+ @post.author = Author.new
+
+ form_for(:post, @post) do |f|
+ concat f.text_field(:title)
+ f.fields_for(:author) do |af|
+ concat af.text_field(:name)
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
+ '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="new author" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_nested_fields_for_with_an_existing_record_on_a_nested_attributes_one_to_one_association
+ @post.author = Author.new(321)
+
+ form_for(:post, @post) do |f|
+ concat f.text_field(:title)
+ f.fields_for(:author) do |af|
+ concat af.text_field(:name)
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
+ '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collection_association
+ @post.comments = Array.new(2) { |id| Comment.new(id + 1) }
+
+ form_for(:post, @post) do |f|
+ concat f.text_field(:title)
+ @post.comments.each do |comment|
+ f.fields_for(:comments, comment) do |cf|
+ concat cf.text_field(:name)
+ end
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
+ '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #1" />' +
+ '<input id="post_comments_attributes_2_name" name="post[comments_attributes][2][name]" size="30" type="text" value="comment #2" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_nested_fields_for_with_new_records_on_a_nested_attributes_collection_association
+ @post.comments = [Comment.new, Comment.new]
+
+ form_for(:post, @post) do |f|
+ concat f.text_field(:title)
+ @post.comments.each do |comment|
+ f.fields_for(:comments, comment) do |cf|
+ concat cf.text_field(:name)
+ end
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
+ '<input id="post_comments_attributes_new_1_name" name="post[comments_attributes][new_1][name]" size="30" type="text" value="new comment" />' +
+ '<input id="post_comments_attributes_new_2_name" name="post[comments_attributes][new_2][name]" size="30" type="text" value="new comment" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_nested_fields_for_with_existing_and_new_records_on_a_nested_attributes_collection_association
+ @post.comments = [Comment.new(321), Comment.new]
+
+ form_for(:post, @post) do |f|
+ concat f.text_field(:title)
+ @post.comments.each do |comment|
+ f.fields_for(:comments, comment) do |cf|
+ concat cf.text_field(:name)
+ end
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
+ '<input id="post_comments_attributes_321_name" name="post[comments_attributes][321][name]" size="30" type="text" value="comment #321" />' +
+ '<input id="post_comments_attributes_new_1_name" name="post[comments_attributes][new_1][name]" size="30" type="text" value="new comment" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_nested_fields_for_on_a_nested_attributes_collection_association_yields_only_builder
+ @post.comments = [Comment.new(321), Comment.new]
+ yielded_comments = []
+
+ form_for(:post, @post) do |f|
+ concat f.text_field(:title)
+ f.fields_for(:comments) do |cf|
+ concat cf.text_field(:name)
+ yielded_comments << cf.object
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
+ '<input id="post_comments_attributes_321_name" name="post[comments_attributes][321][name]" size="30" type="text" value="comment #321" />' +
+ '<input id="post_comments_attributes_new_1_name" name="post[comments_attributes][new_1][name]" size="30" type="text" value="new comment" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ assert_equal yielded_comments, @post.comments
+ end
+
def test_fields_for
fields_for(:post, @post) do |f|
concat f.text_field(:title)
@@ -974,4 +1105,4 @@ class FormHelperTest < ActionView::TestCase
def protect_against_forgery?
false
end
-end
+end \ No newline at end of file
diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb
index c226e212b5..c7405d47de 100644
--- a/actionpack/test/template/render_test.rb
+++ b/actionpack/test/template/render_test.rb
@@ -11,7 +11,7 @@ module RenderTestCases
I18n.backend.store_translations 'da', {}
# Ensure original are still the same since we are reindexing view paths
- assert_equal ORIGINAL_LOCALES, I18n.available_locales
+ assert_equal ORIGINAL_LOCALES, I18n.available_locales.map(&:to_s).sort
end
def test_render_file