aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
Diffstat (limited to 'railties')
-rw-r--r--railties/CHANGELOG2
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb4
-rw-r--r--railties/test/generators/rails_scaffold_generator_test.rb58
3 files changed, 34 insertions, 30 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index 693da47da8..527290de41 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Scaffolded validation errors set the appropriate HTTP status for XML responses. #8622 [mmmultiworks]
+
* Sexy migrations for the session_migration generator. #8561 [Vladislav]
* Console reload! runs to_prepare callbacks also. #8393 [f.svehla]
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb b/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb
index bf47e8eba8..cbfd88f3bd 100644
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb
+++ b/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb
@@ -49,7 +49,7 @@ class <%= controller_class_name %>Controller < ApplicationController
format.xml { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> }
else
format.html { render :action => "new" }
- format.xml { render :xml => @<%= file_name %>.errors }
+ format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
end
end
end
@@ -66,7 +66,7 @@ class <%= controller_class_name %>Controller < ApplicationController
format.xml { head :ok }
else
format.html { render :action => "edit" }
- format.xml { render :xml => @<%= file_name %>.errors }
+ format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
end
end
end
diff --git a/railties/test/generators/rails_scaffold_generator_test.rb b/railties/test/generators/rails_scaffold_generator_test.rb
index cbf116996c..f129efbd81 100644
--- a/railties/test/generators/rails_scaffold_generator_test.rb
+++ b/railties/test/generators/rails_scaffold_generator_test.rb
@@ -14,7 +14,7 @@ module ActiveRecord
end
self.pluralize_table_names = true
end
-
+
module ConnectionAdapters
class Column
attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
@@ -24,7 +24,7 @@ module ActiveRecord
@default=default
@type=@sql_type=sql_type
end
-
+
def human_name
@name.humanize
end
@@ -54,9 +54,9 @@ require 'rails_generator'
require "#{File.dirname(__FILE__)}/generator_test_helper"
class RailsScaffoldGeneratorTest < Test::Unit::TestCase
-
+
include GeneratorTestHelper
-
+
def setup
ActiveRecord::Base.pluralize_table_names = true
Dir.mkdir("#{RAILS_ROOT}/app") unless File.exists?("#{RAILS_ROOT}/app")
@@ -70,9 +70,9 @@ class RailsScaffoldGeneratorTest < Test::Unit::TestCase
Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exists?("#{RAILS_ROOT}/public/stylesheets")
File.open("#{RAILS_ROOT}/config/routes.rb",'w') do |f|
f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n"
- end
+ end
end
-
+
def teardown
FileUtils.rm_rf "#{RAILS_ROOT}/app"
FileUtils.rm_rf "#{RAILS_ROOT}/test"
@@ -80,7 +80,7 @@ class RailsScaffoldGeneratorTest < Test::Unit::TestCase
FileUtils.rm_rf "#{RAILS_ROOT}/db"
FileUtils.rm_rf "#{RAILS_ROOT}/public"
end
-
+
def test_scaffolded_names
g = Rails::Generator::Base.instance('scaffold', %w(ProductLine))
assert_equal "ProductLines", g.controller_name
@@ -90,31 +90,32 @@ class RailsScaffoldGeneratorTest < Test::Unit::TestCase
assert_equal "product_lines", g.controller_file_name
assert_equal "product_lines", g.controller_table_name
end
-
+
def test_scaffold_generates_resources
-
+
run_generator('scaffold', %w(Product))
-
+
assert_generated_controller_for :products do |f|
-
+
assert_has_method f,:index do |name,m|
- assert m=~/@products = Product\.find\(:all\)/,"#{name} should query products table"
+ assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table"
end
-
+
assert_has_method f,:show,:edit,:update,:destroy do |name,m|
- assert m=~/@product = Product\.find\(params\[:id\]\)/,"#{name.to_s} should query products table"
+ assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table"
end
assert_has_method f,:new do |name,m|
- assert m=~/@product = Product\.new/,"#{name.to_s} should instantiate a product"
+ assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product"
end
assert_has_method f,:create do |name,m|
- assert m=~/@product = Product\.new\(params\[:product\]\)/,"#{name.to_s} should instantiate a product"
+ assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product"
+ assert_match /format.xml \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml"
end
-
+
end
-
+
assert_generated_model_for :product
assert_generated_functional_test_for :products
assert_generated_unit_test_for :product
@@ -128,27 +129,28 @@ class RailsScaffoldGeneratorTest < Test::Unit::TestCase
def test_scaffold_generates_resources_with_attributes
run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp))
-
+
assert_generated_controller_for :products do |f|
-
+
assert_has_method f,:index do |name,m|
- assert m=~/@products = Product\.find\(:all\)/,"#{name} should query products table"
+ assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table"
end
-
+
assert_has_method f,:show,:edit,:update,:destroy do |name,m|
- assert m=~/@product = Product\.find\(params\[:id\]\)/,"#{name.to_s} should query products table"
+ assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table"
end
assert_has_method f,:new do |name,m|
- assert m=~/@product = Product\.new/,"#{name.to_s} should instantiate a product"
+ assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product"
end
assert_has_method f,:create do |name,m|
- assert m=~/@product = Product\.new\(params\[:product\]\)/,"#{name.to_s} should instantiate a product"
+ assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product"
+ assert_match /format.xml \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml"
end
-
+
end
-
+
assert_generated_model_for :product
assert_generated_functional_test_for :products
assert_generated_unit_test_for :product
@@ -161,7 +163,7 @@ class RailsScaffoldGeneratorTest < Test::Unit::TestCase
assert_generated_column t, :supplier_id, :integer
assert_generated_column t, :created_at,:timestamp
end
-
+
assert_added_route_for :products
end
end