Skip to content

Commit

Permalink
Validation context should default to proper context instead of none:
Browse files Browse the repository at this point in the history
- When no context provided to Document#valid? use the following
  defaults: :create if new, :update if persisted.

- Slight refactoring of the include annoyance in the validations module.

- Mongoid now includes ActiveModel::Validations::Callbacks

- Initialize callback runs only :after.

- Fixes #556.
  • Loading branch information
durran committed Jan 17, 2011
1 parent 8fe0df0 commit f76a2ca
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 53 deletions.
20 changes: 3 additions & 17 deletions lib/mongoid/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,10 @@ module Callbacks
extend ActiveSupport::Concern
included do
extend ActiveModel::Callbacks
include ActiveModel::Validations::Callbacks

define_model_callbacks \
:create,
:destroy,
:initialize,
:save,
:update,
:validation
end

# Determine if the document is valid.
#
# @example Is the document valid?
# person.valid?
#
# @return [ true, false ] True if valid, false if not.
def valid?(*)
_run_validation_callbacks { super }
define_model_callbacks :initialize, :only => :after
define_model_callbacks :create, :destroy, :save, :update
end
end
end
83 changes: 47 additions & 36 deletions lib/mongoid/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,57 @@ module Mongoid #:nodoc:
# provide: validates_associated and validates_uniqueness_of.
module Validations
extend ActiveSupport::Concern
include ActiveModel::Validations

included do
include ActiveModel::Validations
attr_accessor :validated

attr_accessor :validated

# Overrides the default ActiveModel behaviour since we need to handle
# validations of relations slightly different than just calling the
# getter.
#
# @todo Durran: Why does moving the ActiveModel::Validations include
# statement outside of the block bomb the test suite. This feels dirty.
#
# @example Read the value.
# person.read_attribute_for_validation(:addresses)
#
# @param [ Symbol ] attr The name of the field or relation.
#
# @return [ Object ] The value of the field or the relation.
#
# @since 2.0.0.rc.1
def read_attribute_for_validation(attr)
if relations[attr.to_s]
send(attr, false, :eager => true)
else
send(attr)
end
# Overrides the default ActiveModel behaviour since we need to handle
# validations of relations slightly different than just calling the
# getter.
#
# @example Read the value.
# person.read_attribute_for_validation(:addresses)
#
# @param [ Symbol ] attr The name of the field or relation.
#
# @return [ Object ] The value of the field or the relation.
#
# @since 2.0.0.rc.1
def read_attribute_for_validation(attr)
if relations[attr.to_s]
send(attr, false, :eager => true)
else
send(attr)
end
end

# Used to prevent infinite loops in associated validations.
#
# @example Is the document validated?
# document.validated?
#
# @return [ true, false ] Has the document already been validated?
#
# @since 2.0.0.rc.2
def validated?
!!@validated
end
# Determine if the document is valid.
#
# @example Is the document valid?
# person.valid?
#
# @example Is the document valid in a context?
# person.valid?(:create)
#
# @param [ Symbol ] context The optional validation context.
#
# @return [ true, false ] True if valid, false if not.
#
# @since 2.0.0.rc.6
def valid?(context = nil)
super context ? context : (new? ? :create : :update)
end

# Used to prevent infinite loops in associated validations.
#
# @example Is the document validated?
# document.validated?
#
# @return [ true, false ] Has the document already been validated?
#
# @since 2.0.0.rc.2
def validated?
!!@validated
end

module ClassMethods #:nodoc:
Expand Down
41 changes: 41 additions & 0 deletions spec/integration/mongoid/validations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "spec_helper"

describe Mongoid::Validations do

let(:account) do
Account.new(:name => "Testing a really long name.")
end

describe "#valid?" do

context "when provided a context" do

it "uses the provided context" do
account.should be_valid(:update)
end
end

context "when not provided a context" do

context "when the document is new" do

it "defaults the context to :create" do
account.should_not be_valid
end
end

context "when the document is persisted" do

before do
account.name = "Testing"
account.save
account.name = "Testing a really long name."
end

it "defaults the context to :update" do
account.should be_valid
end
end
end
end
end
1 change: 1 addition & 0 deletions spec/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ class Account
attr_accessible :nickname, :name

validates_presence_of :name
validates_length_of :name, :maximum => 10, :on => :create
end

0 comments on commit f76a2ca

Please sign in to comment.