Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
[dm-validations] Added specs for Resource#update w/validation context
Browse files Browse the repository at this point in the history
* Added Resource#update that handled the validation context
* Removed unecessary Model#create! method
* Updated the default_validation_context to check the context stack
  before returning the default label to use, in case we are nested
  inside another context.

[#882 state:resolved]
  • Loading branch information
dkubb committed Aug 18, 2009
1 parent 987fa7e commit e3bb9f1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 18 deletions.
2 changes: 2 additions & 0 deletions dm-validations/Manifest.txt
Expand Up @@ -37,6 +37,7 @@ spec/fixtures/city.rb
spec/fixtures/company.rb
spec/fixtures/corporate_world.rb
spec/fixtures/country.rb
spec/fixtures/currency.rb
spec/fixtures/ethernet_frame.rb
spec/fixtures/event.rb
spec/fixtures/g3_concert.rb
Expand Down Expand Up @@ -124,6 +125,7 @@ spec/integration/uniqueness_validator/spec_helper.rb
spec/integration/uniqueness_validator/uniqueness_validator_spec.rb
spec/integration/within_validator/spec_helper.rb
spec/integration/within_validator/within_validator_spec.rb
spec/public/resource_spec.rb
spec/spec.opts
spec/spec_helper.rb
spec/unit/contextual_validators/emptiness_spec.rb
Expand Down
35 changes: 18 additions & 17 deletions dm-validations/lib/dm-validations.rb
Expand Up @@ -26,42 +26,37 @@

module DataMapper
module Validate
Model.append_inclusions self

extend Chainable

def self.included(model)
model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.create(attributes = {}, context = :default)
resource = new(attributes)
resource.save(context)
resource
end
def self.create!(attributes = {})
def self.create(attributes = {}, *args)
resource = new(attributes)
resource.save!
resource.save(*args)
resource
end
RUBY

# models that are non DM resources must get .validators
# and other methods, too
model.extend Validate::ClassMethods
model.extend ClassMethods
end

# Ensures the object is valid for the context provided, and otherwise
# throws :halt and returns false.
#
chainable do

def save(context = default_validation_context)
validation_context(context) { super() }
end
end

def save_self(*)
return false unless validation_context_stack.empty? || valid?(current_validation_context)
super
chainable do
def update(attributes = {}, context = default_validation_context)
validation_context(context) { super(attributes) }
end

end

# Return the ValidationErrors
Expand Down Expand Up @@ -142,6 +137,15 @@ def validation_association_keys(name)
nil
end

private

chainable do
def save_self(*)
return false unless validation_context_stack.empty? || valid?(current_validation_context)
super
end
end

module ClassMethods
include DataMapper::Validate::ValidatesPresent
include DataMapper::Validate::ValidatesAbsent
Expand Down Expand Up @@ -231,7 +235,4 @@ def add_validator_to_context(opts, fields, klazz)
end
end # module ClassMethods
end # module Validate

Model.append_inclusions Validate
Model.append_extensions Validate::ClassMethods
end # module DataMapper
2 changes: 1 addition & 1 deletion dm-validations/lib/dm-validations/support/context.rb
Expand Up @@ -13,7 +13,7 @@ module Context
# TODO: document
# @api private
def default_validation_context
:default
current_validation_context || :default
end

protected
Expand Down
68 changes: 68 additions & 0 deletions dm-validations/spec/public/resource_spec.rb
@@ -0,0 +1,68 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))

describe DataMapper::Resource do
before :all do
DataMapper::Validate::Fixtures::Barcode.auto_migrate!

@resource = DataMapper::Validate::Fixtures::Barcode.new
end

describe '#update' do
describe 'when provided valid attributes' do
before :all do
@response = @resource.update(:code => 'a' * 10)
end

it 'should return true' do
@response.should be_true
end
end

describe 'when provided invalid attributes' do
before :all do
@response = @resource.update(:code => 'a' * 11)
end

it 'should return false' do
@response.should be_false
end

it 'should set errors' do
@resource.errors.to_a.should == [ [ 'Code must be at most 10 characters long' ] ]
end
end

describe 'when provided invalid attributes and a context' do
before :all do
# remove data from previous spec runs
::DataMapper::Validate::Fixtures::Organisation.all.destroy!
::DataMapper::Validate::Fixtures::Department.all.destroy!
::DataMapper::Validate::Fixtures::User.all.destroy!

organization = DataMapper::Validate::Fixtures::Organisation.create(:name => 'Org 101', :domain => '101')
dept = DataMapper::Validate::Fixtures::Department.create(:name => 'accounting')

attributes = {
:organisation => organization,
:user_name => 'guy',
:department => dept,
}

# create a record that will be a dupe when User#update is executed below
DataMapper::Validate::Fixtures::User.create(attributes).should be_saved

@resource = DataMapper::Validate::Fixtures::User.new

@response = @resource.update(attributes, :signing_up_for_department_account)
end

it 'should return false' do
@response.should be_false
end

it 'should set errors' do
@resource.errors.to_a.should == [ [ 'User name is already taken' ] ]
end
end
end
end

0 comments on commit e3bb9f1

Please sign in to comment.