Added validation callbacks to extended documents and casted models

This commit is contained in:
Peter Gumeson 2009-06-04 19:49:10 -07:00
parent 91cd1d9c7b
commit b4e2250668
5 changed files with 77 additions and 4 deletions

View file

@ -68,10 +68,13 @@ CouchRest::Model has been deprecated and replaced by CouchRest::ExtendedDocument
### Callbacks
`CouchRest::ExtendedDocuments` instances have 2 callbacks already defined for you:
`create_callback`, `save_callback`, `update_callback` and `destroy_callback`
`CouchRest::ExtendedDocuments` instances have 4 callbacks already defined for you:
`validate_callback`, `create_callback`, `save_callback`, `update_callback` and `destroy_callback`
In your document inherits from `CouchRest::ExtendedDocument`, define your callback as follows:
`CouchRest::CastedModel` instances have 1 callback already defined for you:
`validate_callback`
Define your callback as follows:
save_callback :before, :generate_slug_from_name

View file

@ -51,6 +51,9 @@ module CouchRest
def self.included(base)
base.extlib_inheritable_accessor(:auto_validation)
base.class_eval <<-EOS, __FILE__, __LINE__
# Callbacks
define_callbacks :validate
# Turn off auto validation by default
self.auto_validation ||= false
@ -72,6 +75,7 @@ module CouchRest
base.extend(ClassMethods)
base.class_eval <<-EOS, __FILE__, __LINE__
define_callbacks :validate
if method_defined?(:_run_save_callbacks)
save_callback :before, :check_validations
end
@ -147,8 +151,10 @@ module CouchRest
valid = recursive_valid?(prop, context, valid) && valid
end
end
target._run_validate_callbacks do
target.class.validators.execute(context, target) && valid
end
end
def validation_property_value(name)

View file

@ -4,6 +4,7 @@ module CouchRest
module CastedModel
def self.included(base)
base.send(:include, CouchRest::Callbacks)
base.send(:include, CouchRest::Mixins::Properties)
base.send(:attr_accessor, :casted_by)
base.send(:attr_accessor, :document_saved)

View file

@ -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

View file

@ -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