Fix regression with context-dependent validations in ActiveResource

As reported in #348, model#valid? is called with one argument while
ActiveResource::Validations only defines #valid? without an optional
argument.

The interface of ActiveResouce::Validations#valid? is the same for
A'resource in Rails 3.2 and the one extracted into a gem.

Rails 3.2 version:
https://github.com/rails/rails/blob/3-2-stable/activeresource/lib/active_resource/validations.rb#L123

Gem version:
https://github.com/rails/activeresource/blob/master/lib/active_resource/validations.rb#L160
This commit is contained in:
Carsten Zimmermann 2013-08-28 12:08:06 +02:00 committed by Elliot Winkler
parent 15ef228856
commit bcd8173383
7 changed files with 55 additions and 1 deletions

View File

@ -26,6 +26,7 @@ appraise '4.0' do
gem 'bcrypt-ruby', '~> 3.0.0' #FIXME: This should be ~> 3.1.0 for Rails 4.0
gem 'jquery-rails'
gem 'sass-rails', '~> 4.0.0'
gem 'activeresource', require: 'active_resource'
# Test suite makes heavy use of attr_accessible
gem 'protected_attributes'

View File

@ -1,5 +1,7 @@
# HEAD
* Fix a regression with context-dependent validations in ActiveResource
* shoulda-matchers is now fully compatible with Rails 4.
* When not using RSpec, shoulda-matchers is now auto-included into

View File

@ -13,6 +13,7 @@ gem "rails", "4.0.0"
gem "bcrypt-ruby", "~> 3.0.0"
gem "jquery-rails"
gem "sass-rails", "~> 4.0.0"
gem "activeresource", :require=>"active_resource"
gem "protected_attributes"
gemspec :path=>"../"

View File

@ -25,6 +25,10 @@ GEM
activesupport (= 4.0.0)
arel (~> 4.0.0)
activerecord-deprecated_finders (1.0.3)
activeresource (4.0.0)
activemodel (~> 4.0)
activesupport (~> 4.0)
rails-observers (~> 0.1.1)
activesupport (4.0.0)
i18n (~> 0.6, >= 0.6.4)
minitest (~> 4.2)
@ -86,6 +90,8 @@ GEM
bundler (>= 1.3.0, < 2.0)
railties (= 4.0.0)
sprockets-rails (~> 2.0.0)
rails-observers (0.1.2)
activemodel (~> 4.0)
railties (4.0.0)
actionpack (= 4.0.0)
activesupport (= 4.0.0)
@ -134,6 +140,7 @@ PLATFORMS
DEPENDENCIES
activerecord-jdbc-adapter
activerecord-jdbcsqlite3-adapter
activeresource
appraisal (~> 0.4)
aruba
bcrypt-ruby (~> 3.0.0)

View File

@ -59,7 +59,7 @@ module Shoulda
end
def validate_instance
@instance.valid?(@context)
@instance.valid?(*@context)
@instance
end
end

View File

@ -134,6 +134,16 @@ describe Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher do
end
end
context 'an active_resource model' do
context 'with the validation context' do
it 'does not raise an exception' do
expect {
active_resource_model.should validate_presence_of(:attr)
}.to_not raise_exception
end
end
end
def matcher
validate_presence_of(:attr)
end
@ -161,4 +171,10 @@ describe Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher do
end
end.new
end
def active_resource_model
define_active_resource_class :foo, :attr => :string do
validates_presence_of :attr
end.new
end
end

View File

@ -0,0 +1,27 @@
module ActiveResourceBuilder
def self.included(example_group)
example_group.class_eval do
after do
ActiveSupport::Dependencies.clear
end
end
end
def define_active_resource_class(class_name, attributes = {}, &block)
define_class(class_name, ActiveResource::Base) do
schema do
attributes.each do |attr, type|
attribute attr, type
end
end
if block_given?
class_eval(&block)
end
end
end
end
RSpec.configure do |config|
config.include ActiveResourceBuilder
end