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:
Greg Sterndale 2011-02-01 14:32:38 -05:00
parent 83b70ec080
commit 4682e4ac5d
5 changed files with 28 additions and 24 deletions

View file

@ -12,8 +12,7 @@ module CouchRest #:nodoc:
:create, :create,
:destroy, :destroy,
:save, :save,
:update, :update
:validate
end end

View file

@ -15,6 +15,7 @@ module CouchRest
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
include ActiveModel::Validations include ActiveModel::Validations
include ActiveModel::Validations::Callbacks
end end

View file

@ -29,14 +29,16 @@ end
class WithCastedCallBackModel < Hash class WithCastedCallBackModel < Hash
include CouchRest::Model::CastedModel include CouchRest::Model::CastedModel
property :name property :name
property :run_before_validate property :run_before_validation
property :run_after_validate property :run_after_validation
before_validate do |object| validates_presence_of :run_before_validation
object.run_before_validate = true
before_validation do |object|
object.run_before_validation = true
end end
after_validate do |object| after_validation do |object|
object.run_after_validate = true object.run_after_validation = true
end end
end end
@ -423,15 +425,15 @@ describe CouchRest::Model::CastedModel do
end end
describe "validate" do describe "validate" do
it "should run before_validate before validating" do it "should run before_validation before validating" do
@model.run_before_validate.should be_nil @model.run_before_validation.should be_nil
@model.should be_valid @model.should be_valid
@model.run_before_validate.should be_true @model.run_before_validation.should be_true
end end
it "should run after_validate after validating" do it "should run after_validation after validating" do
@model.run_after_validate.should be_nil @model.run_after_validation.should be_nil
@model.should be_valid @model.should be_valid
@model.run_after_validate.should be_true @model.run_after_validation.should be_true
end end
end end
end end

View file

@ -329,14 +329,14 @@ describe "Model Persistence" do
describe "validation" do describe "validation" do
it "should run before_validation before validating" 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.should be_valid
@doc.run_before_validate.should be_true @doc.run_before_validation.should be_true
end end
it "should run after_validation after validating" do 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.should be_valid
@doc.run_after_validate.should be_true @doc.run_after_validation.should be_true
end end
end end

14
spec/fixtures/base.rb vendored
View file

@ -20,20 +20,22 @@ end
class WithCallBacks < CouchRest::Model::Base class WithCallBacks < CouchRest::Model::Base
use_database TEST_SERVER.default_database use_database TEST_SERVER.default_database
property :name property :name
property :run_before_validate property :run_before_validation
property :run_after_validate property :run_after_validation
property :run_before_save property :run_before_save
property :run_after_save property :run_after_save
property :run_before_create property :run_before_create
property :run_after_create property :run_after_create
property :run_before_update property :run_before_update
property :run_after_update property :run_after_update
validates_presence_of :run_before_validation
before_validate do |object| before_validation do |object|
object.run_before_validate = true object.run_before_validation = true
end end
after_validate do |object| after_validation do |object|
object.run_after_validate = true object.run_after_validation = true
end end
before_save do |object| before_save do |object|
object.run_before_save = true object.run_before_save = true