Added ActiveRecord like after_initialize callback to ExtendedDocument
This commit is contained in:
parent
d992f44365
commit
ca23e186f4
2 changed files with 20 additions and 0 deletions
|
@ -58,6 +58,7 @@ module CouchRest
|
|||
unless self['_id'] && self['_rev']
|
||||
self['couchrest-type'] = self.class.to_s
|
||||
end
|
||||
after_initialize if respond_to?(:after_initialize)
|
||||
end
|
||||
|
||||
# Defines an instance and save it directly to the database
|
||||
|
|
|
@ -104,6 +104,18 @@ describe "ExtendedDocument" do
|
|||
self.other_arg = "foo-#{value}"
|
||||
end
|
||||
end
|
||||
|
||||
class WithAfterInitializeMethod < CouchRest::ExtendedDocument
|
||||
use_database TEST_SERVER.default_database
|
||||
|
||||
property :some_value
|
||||
|
||||
def after_initialize
|
||||
self.some_value ||= "value"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
before(:each) do
|
||||
@obj = WithDefaultValues.new
|
||||
|
@ -698,6 +710,13 @@ describe "ExtendedDocument" do
|
|||
@doc.other_arg.should == "foo-foo"
|
||||
end
|
||||
end
|
||||
|
||||
describe "initialization" do
|
||||
it "should call after_initialize method if available" do
|
||||
@doc = WithAfterInitializeMethod.new
|
||||
@doc['some_value'].should eql('value')
|
||||
end
|
||||
end
|
||||
|
||||
describe "recursive validation on an extended document" do
|
||||
before :each do
|
||||
|
|
Loading…
Reference in a new issue