Skip to content

Commit

Permalink
Merge remote branch 'rails/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed May 9, 2010
2 parents 1ff3d95 + df508bd commit e1a0d86
Show file tree
Hide file tree
Showing 66 changed files with 1,065 additions and 1,013 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/middleware/params_parser.rb
Expand Up @@ -38,7 +38,7 @@ def parse_formatted_parameters(env)
when Proc
strategy.call(request.raw_post)
when :xml_simple, :xml_node
data = Hash.from_xml(request.body) || {}
data = Hash.from_xml(request.body.read) || {}
request.body.rewind if request.body.respond_to?(:rewind)
data.with_indifferent_access
when :yaml
Expand Down
13 changes: 13 additions & 0 deletions actionpack/test/dispatch/request/xml_params_parsing_test.rb
Expand Up @@ -16,6 +16,19 @@ def teardown
TestController.last_request_parameters = nil
end

test "parses a strict rack.input" do
class Linted
def call(env)
bar = env['action_dispatch.request.request_parameters']['foo']
result = "<ok>#{bar}</ok>"
[200, {"Content-Type" => "application/xml", "Content-Length" => result.length.to_s}, result]
end
end
req = Rack::MockRequest.new(ActionDispatch::ParamsParser.new(Linted.new))
resp = req.post('/', "CONTENT_TYPE" => "application/xml", :input => "<foo>bar</foo>", :lint => true)
assert_equal "<ok>bar</ok>", resp.body
end

test "parses hash params" do
with_test_routing do
xml = "<person><name>David</name></person>"
Expand Down
5 changes: 5 additions & 0 deletions activemodel/CHANGELOG
@@ -1,3 +1,8 @@
*Rails 3.0.0 [beta 4/release candidate] (unreleased)*

* JSON supports a custom root option: to_json(:root => 'custom') #4515 [Jatinder Singh]


*Rails 3.0.0 [beta 3] (April 13th, 2010)*

* No changes
Expand Down
6 changes: 5 additions & 1 deletion activemodel/lib/active_model/serializers/json.rb
Expand Up @@ -79,7 +79,11 @@ module JSON
# "title": "So I was thinking"}]}
def encode_json(encoder)
hash = serializable_hash(encoder.options)
hash = { self.class.model_name.element => hash } if include_root_in_json
if include_root_in_json
custom_root = encoder.options && encoder.options[:root]
hash = { custom_root || self.class.model_name.element => hash }
end

ActiveSupport::JSON.encode(hash)
end

Expand Down
17 changes: 12 additions & 5 deletions activemodel/lib/active_model/validations.rb
Expand Up @@ -29,7 +29,7 @@ module ActiveModel
# person.invalid?
# #=> false
# person.first_name = 'zoolander'
# person.valid?
# person.valid?
# #=> false
# person.invalid?
# #=> true
Expand All @@ -48,6 +48,8 @@ module Validations
extend ActiveModel::Translation
define_callbacks :validate, :scope => :name

attr_accessor :validation_context

class_attribute :_validators
self._validators = Hash.new { |h,k| h[k] = [] }
end
Expand Down Expand Up @@ -117,7 +119,7 @@ def validate(*args, &block)
options = args.last
if options.is_a?(Hash) && options.key?(:on)
options[:if] = Array.wrap(options[:if])
options[:if] << "@_on_validate == :#{options[:on]}"
options[:if] << "validation_context == :#{options[:on]}"
end
set_callback(:validate, *args, &block)
end
Expand Down Expand Up @@ -150,15 +152,20 @@ def errors
end

# Runs all the specified validations and returns true if no errors were added otherwise false.
def valid?
# Context can optionally be supplied to define which callbacks to test against (the context is
# defined on the validations using :on).
def valid?(context = nil)
current_context, self.validation_context = validation_context, context
errors.clear
_run_validate_callbacks
errors.empty?
ensure
self.validation_context = current_context
end

# Performs the opposite of <tt>valid?</tt>. Returns true if errors were added, false otherwise.
def invalid?
!valid?
def invalid?(context = nil)
!valid?(context)
end

# Hook method defining how an attribute value should be retieved. By default this is assumed
Expand Down
1 change: 1 addition & 0 deletions activemodel/test/cases/helper.rb
Expand Up @@ -5,6 +5,7 @@

require 'config'
require 'active_model'
require 'active_support/core_ext/string/access'

# Show backtraces for deprecated behavior for quicker cleanup.
ActiveSupport::Deprecation.debug = true
Expand Down
16 changes: 16 additions & 0 deletions activemodel/test/cases/serializeration/json_serialization_test.rb
Expand Up @@ -37,6 +37,22 @@ def setup
end
end

test "should include custom root in json" do
begin
Contact.include_root_in_json = true
json = @contact.to_json(:root => 'json_contact')

assert_match %r{^\{"json_contact":\{}, json
assert_match %r{"name":"Konata Izumi"}, json
assert_match %r{"age":16}, json
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
assert_match %r{"awesome":true}, json
assert_match %r{"preferences":\{"shows":"anime"\}}, json
ensure
Contact.include_root_in_json = false
end
end

test "should encode all encodable attributes" do
json = @contact.to_json

Expand Down
35 changes: 0 additions & 35 deletions activemodel/test/cases/tests_database.rb

This file was deleted.

33 changes: 15 additions & 18 deletions activemodel/test/cases/validations/acceptance_validation_test.rb
@@ -1,57 +1,54 @@
# encoding: utf-8
require 'cases/helper'
require 'cases/tests_database'

require 'models/topic'
require 'models/reply'
require 'models/developer'
require 'models/person'

class AcceptanceValidationTest < ActiveModel::TestCase
include ActiveModel::TestsDatabase

def teardown
Topic.reset_callbacks(:validate)
end

def test_terms_of_service_agreement_no_acceptance
Topic.validates_acceptance_of(:terms_of_service, :on => :create)
Topic.validates_acceptance_of(:terms_of_service)

t = Topic.create("title" => "We should not be confirmed")
assert t.save
t = Topic.new("title" => "We should not be confirmed")
assert t.valid?
end

def test_terms_of_service_agreement
Topic.validates_acceptance_of(:terms_of_service, :on => :create)
Topic.validates_acceptance_of(:terms_of_service)

t = Topic.create("title" => "We should be confirmed","terms_of_service" => "")
assert !t.save
t = Topic.new("title" => "We should be confirmed","terms_of_service" => "")
assert t.invalid?
assert_equal ["must be accepted"], t.errors[:terms_of_service]

t.terms_of_service = "1"
assert t.save
assert t.valid?
end

def test_eula
Topic.validates_acceptance_of(:eula, :message => "must be abided", :on => :create)
Topic.validates_acceptance_of(:eula, :message => "must be abided")

t = Topic.create("title" => "We should be confirmed","eula" => "")
assert !t.save
t = Topic.new("title" => "We should be confirmed","eula" => "")
assert t.invalid?
assert_equal ["must be abided"], t.errors[:eula]

t.eula = "1"
assert t.save
assert t.valid?
end

def test_terms_of_service_agreement_with_accept_value
Topic.validates_acceptance_of(:terms_of_service, :on => :create, :accept => "I agree.")
Topic.validates_acceptance_of(:terms_of_service, :accept => "I agree.")

t = Topic.create("title" => "We should be confirmed", "terms_of_service" => "")
assert !t.save
t = Topic.new("title" => "We should be confirmed", "terms_of_service" => "")
assert t.invalid?
assert_equal ["must be accepted"], t.errors[:terms_of_service]

t.terms_of_service = "I agree."
assert t.save
assert t.valid?
end

def test_validates_acceptance_of_for_ruby_class
Expand Down
44 changes: 21 additions & 23 deletions activemodel/test/cases/validations/conditional_validation_test.rb
@@ -1,80 +1,78 @@
# encoding: utf-8
require 'cases/helper'
require 'cases/tests_database'

require 'models/topic'

class ConditionalValidationTest < ActiveModel::TestCase
include ActiveModel::TestsDatabase

def teardown
Topic.reset_callbacks(:validate)
end

def test_if_validation_using_method_true
# When the method returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => :condition_is_true )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.invalid?
assert t.errors[:title].any?
assert_equal ["hoo 5"], t.errors["title"]
end

def test_unless_validation_using_method_true
# When the method returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => :condition_is_true )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert !t.errors[:title].any?
assert t.errors[:title].empty?
end

def test_if_validation_using_method_false
# When the method returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => :condition_is_true_but_its_not )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert t.errors[:title].empty?
end

def test_unless_validation_using_method_false
# When the method returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => :condition_is_true_but_its_not )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.invalid?
assert t.errors[:title].any?
assert_equal ["hoo 5"], t.errors["title"]
end

def test_if_validation_using_string_true
# When the evaluated string returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => "a = 1; a == 1" )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.invalid?
assert t.errors[:title].any?
assert_equal ["hoo 5"], t.errors["title"]
end

def test_unless_validation_using_string_true
# When the evaluated string returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => "a = 1; a == 1" )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert t.errors[:title].empty?
end

def test_if_validation_using_string_false
# When the evaluated string returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => "false")
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert t.errors[:title].empty?
end

def test_unless_validation_using_string_false
# When the evaluated string returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => "false")
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.invalid?
assert t.errors[:title].any?
assert_equal ["hoo 5"], t.errors["title"]
end
Expand All @@ -83,8 +81,8 @@ def test_if_validation_using_block_true
# When the block returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}",
:if => Proc.new { |r| r.content.size > 4 } )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.invalid?
assert t.errors[:title].any?
assert_equal ["hoo 5"], t.errors["title"]
end
Expand All @@ -93,7 +91,7 @@ def test_unless_validation_using_block_true
# When the block returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}",
:unless => Proc.new { |r| r.content.size > 4 } )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert t.errors[:title].empty?
end
Expand All @@ -102,7 +100,7 @@ def test_if_validation_using_block_false
# When the block returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}",
:if => Proc.new { |r| r.title != "uhohuhoh"} )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert t.errors[:title].empty?
end
Expand All @@ -111,8 +109,8 @@ def test_unless_validation_using_block_false
# When the block returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}",
:unless => Proc.new { |r| r.title != "uhohuhoh"} )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
assert t.invalid?
assert t.errors[:title].any?
assert_equal ["hoo 5"], t.errors["title"]
end
Expand All @@ -132,7 +130,7 @@ def test_validation_with_if_as_string
assert t.valid?, "A topic with a basic title should be valid"

t.title = "A very important title"
assert !t.valid?, "A topic with an important title, but without an author, should not be valid"
assert t.invalid?, "A topic with an important title, but without an author, should not be valid"
assert t.errors[:author_name].any?, "A topic with an 'important' title should require an author"

t.author_name = "Hubert J. Farnsworth"
Expand Down

0 comments on commit e1a0d86

Please sign in to comment.