Added validation callbacks to extended documents and casted models
This commit is contained in:
parent
91cd1d9c7b
commit
b4e2250668
5 changed files with 77 additions and 4 deletions
|
@ -23,6 +23,26 @@ class DummyModel < CouchRest::ExtendedDocument
|
|||
property :keywords, :cast_as => ["String"]
|
||||
end
|
||||
|
||||
class CastedCallbackDoc < CouchRest::ExtendedDocument
|
||||
use_database TEST_SERVER.default_database
|
||||
raise "Default DB not set" if TEST_SERVER.default_database.nil?
|
||||
property :callback_model, :cast_as => 'WithCastedCallBackModel'
|
||||
end
|
||||
class WithCastedCallBackModel < Hash
|
||||
include CouchRest::CastedModel
|
||||
include CouchRest::Validation
|
||||
property :name
|
||||
property :run_before_validate
|
||||
property :run_after_validate
|
||||
|
||||
validate_callback :before do |object|
|
||||
object.run_before_validate = true
|
||||
end
|
||||
validate_callback :after do |object|
|
||||
object.run_after_validate = true
|
||||
end
|
||||
end
|
||||
|
||||
describe CouchRest::CastedModel do
|
||||
|
||||
describe "A non hash class including CastedModel" do
|
||||
|
@ -354,4 +374,25 @@ describe CouchRest::CastedModel do
|
|||
lambda{@toy.base_doc.save!}.should_not raise_error
|
||||
end
|
||||
end
|
||||
|
||||
describe "callbacks" do
|
||||
before(:each) do
|
||||
@doc = CastedCallbackDoc.new
|
||||
@model = WithCastedCallBackModel.new
|
||||
@doc.callback_model = @model
|
||||
end
|
||||
|
||||
describe "validate" do
|
||||
it "should run before_validate before validating" do
|
||||
@model.run_before_validate.should be_nil
|
||||
@model.should be_valid
|
||||
@model.run_before_validate.should be_true
|
||||
end
|
||||
it "should run after_validate after validating" do
|
||||
@model.run_after_validate.should be_nil
|
||||
@model.should be_valid
|
||||
@model.run_after_validate.should be_true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -17,8 +17,11 @@ describe "ExtendedDocument" do
|
|||
end
|
||||
|
||||
class WithCallBacks < CouchRest::ExtendedDocument
|
||||
include ::CouchRest::Validation
|
||||
use_database TEST_SERVER.default_database
|
||||
property :name
|
||||
property :run_before_validate
|
||||
property :run_after_validate
|
||||
property :run_before_save
|
||||
property :run_after_save
|
||||
property :run_before_create
|
||||
|
@ -26,6 +29,12 @@ describe "ExtendedDocument" do
|
|||
property :run_before_update
|
||||
property :run_after_update
|
||||
|
||||
validate_callback :before do |object|
|
||||
object.run_before_validate = true
|
||||
end
|
||||
validate_callback :after do |object|
|
||||
object.run_after_validate = true
|
||||
end
|
||||
save_callback :before do |object|
|
||||
object.run_before_save = true
|
||||
end
|
||||
|
@ -504,6 +513,19 @@ describe "ExtendedDocument" do
|
|||
@doc = WithCallBacks.new
|
||||
end
|
||||
|
||||
|
||||
describe "validate" do
|
||||
it "should run before_validate before validating" do
|
||||
@doc.run_before_validate.should be_nil
|
||||
@doc.should be_valid
|
||||
@doc.run_before_validate.should be_true
|
||||
end
|
||||
it "should run after_validate after validating" do
|
||||
@doc.run_after_validate.should be_nil
|
||||
@doc.should be_valid
|
||||
@doc.run_after_validate.should be_true
|
||||
end
|
||||
end
|
||||
describe "save" do
|
||||
it "should run the after filter after saving" do
|
||||
@doc.run_after_save.should be_nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue