Use :validation callbacks, not the :validate callbacks reserved by ActiveModel, ensuring callbacks are actually run when they're supposed to be.
This commit is contained in:
parent
83b70ec080
commit
4682e4ac5d
5 changed files with 28 additions and 24 deletions
|
@ -12,8 +12,7 @@ module CouchRest #:nodoc:
|
|||
:create,
|
||||
:destroy,
|
||||
:save,
|
||||
:update,
|
||||
:validate
|
||||
:update
|
||||
|
||||
end
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ module CouchRest
|
|||
extend ActiveSupport::Concern
|
||||
included do
|
||||
include ActiveModel::Validations
|
||||
include ActiveModel::Validations::Callbacks
|
||||
end
|
||||
|
||||
|
||||
|
|
|
@ -29,14 +29,16 @@ end
|
|||
class WithCastedCallBackModel < Hash
|
||||
include CouchRest::Model::CastedModel
|
||||
property :name
|
||||
property :run_before_validate
|
||||
property :run_after_validate
|
||||
property :run_before_validation
|
||||
property :run_after_validation
|
||||
|
||||
before_validate do |object|
|
||||
object.run_before_validate = true
|
||||
validates_presence_of :run_before_validation
|
||||
|
||||
before_validation do |object|
|
||||
object.run_before_validation = true
|
||||
end
|
||||
after_validate do |object|
|
||||
object.run_after_validate = true
|
||||
after_validation do |object|
|
||||
object.run_after_validation = true
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -423,15 +425,15 @@ describe CouchRest::Model::CastedModel do
|
|||
end
|
||||
|
||||
describe "validate" do
|
||||
it "should run before_validate before validating" do
|
||||
@model.run_before_validate.should be_nil
|
||||
it "should run before_validation before validating" do
|
||||
@model.run_before_validation.should be_nil
|
||||
@model.should be_valid
|
||||
@model.run_before_validate.should be_true
|
||||
@model.run_before_validation.should be_true
|
||||
end
|
||||
it "should run after_validate after validating" do
|
||||
@model.run_after_validate.should be_nil
|
||||
it "should run after_validation after validating" do
|
||||
@model.run_after_validation.should be_nil
|
||||
@model.should be_valid
|
||||
@model.run_after_validate.should be_true
|
||||
@model.run_after_validation.should be_true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -329,14 +329,14 @@ describe "Model Persistence" do
|
|||
|
||||
describe "validation" do
|
||||
it "should run before_validation before validating" do
|
||||
@doc.run_before_validate.should be_nil
|
||||
@doc.run_before_validation.should be_nil
|
||||
@doc.should be_valid
|
||||
@doc.run_before_validate.should be_true
|
||||
@doc.run_before_validation.should be_true
|
||||
end
|
||||
it "should run after_validation after validating" do
|
||||
@doc.run_after_validate.should be_nil
|
||||
@doc.run_after_validation.should be_nil
|
||||
@doc.should be_valid
|
||||
@doc.run_after_validate.should be_true
|
||||
@doc.run_after_validation.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
|
|
14
spec/fixtures/base.rb
vendored
14
spec/fixtures/base.rb
vendored
|
@ -20,20 +20,22 @@ end
|
|||
class WithCallBacks < CouchRest::Model::Base
|
||||
use_database TEST_SERVER.default_database
|
||||
property :name
|
||||
property :run_before_validate
|
||||
property :run_after_validate
|
||||
property :run_before_validation
|
||||
property :run_after_validation
|
||||
property :run_before_save
|
||||
property :run_after_save
|
||||
property :run_before_create
|
||||
property :run_after_create
|
||||
property :run_before_update
|
||||
property :run_after_update
|
||||
|
||||
validates_presence_of :run_before_validation
|
||||
|
||||
before_validate do |object|
|
||||
object.run_before_validate = true
|
||||
before_validation do |object|
|
||||
object.run_before_validation = true
|
||||
end
|
||||
after_validate do |object|
|
||||
object.run_after_validate = true
|
||||
after_validation do |object|
|
||||
object.run_after_validation = true
|
||||
end
|
||||
before_save do |object|
|
||||
object.run_before_save = true
|
||||
|
|
Loading…
Reference in a new issue