Callbacks now take multiple callback methods
This commit is contained in:
parent
ab362cb32c
commit
db7829e996
3 changed files with 61 additions and 4 deletions
|
@ -86,8 +86,8 @@ CouchRest uses a mixin you can find in lib/mixins/callbacks which is extracted f
|
|||
|
||||
Or the new shorter version:
|
||||
|
||||
before_save :before_method
|
||||
after_save :after_method, :if => :condition
|
||||
before_save :before_method, :another_method
|
||||
after_save :after_method, :another_method, :if => :condition
|
||||
around_save {|r| stuff; yield; stuff }
|
||||
|
||||
Check the mixin or the ExtendedDocument class to see how to implement your own callbacks.
|
||||
|
|
|
@ -533,13 +533,23 @@ module CouchRest
|
|||
# set_callback(:save, :before) becomes before_save
|
||||
[:before, :after, :around].each do |filter|
|
||||
self.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
||||
def self.#{filter}_#{symbol}(meth=nil, &blk)
|
||||
set_callback(:#{symbol}, :#{filter}, meth||blk)
|
||||
def self.#{filter}_#{symbol}(*symbols, &blk)
|
||||
_alias_callbacks(symbols, blk) do |callback, options|
|
||||
set_callback(:#{symbol}, :#{filter}, callback, options)
|
||||
end
|
||||
end
|
||||
RUBY_EVAL
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def _alias_callbacks(callbacks, block)
|
||||
options = callbacks.last.is_a?(Hash) ? callbacks.pop : {}
|
||||
callbacks.push(block) if block
|
||||
callbacks.each do |callback|
|
||||
yield callback, options
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -53,6 +53,32 @@ describe "ExtendedDocument" do
|
|||
after_update do |object|
|
||||
object.run_after_update = true
|
||||
end
|
||||
|
||||
property :run_one
|
||||
property :run_two
|
||||
property :run_three
|
||||
|
||||
before_save :run_one_method, :run_two_method do |object|
|
||||
object.run_three = true
|
||||
end
|
||||
def run_one_method
|
||||
self.run_one = true
|
||||
end
|
||||
def run_two_method
|
||||
self.run_two = true
|
||||
end
|
||||
|
||||
attr_accessor :run_it
|
||||
property :conditional_one
|
||||
property :conditional_two
|
||||
|
||||
before_save :conditional_one_method, :conditional_two_method, :if => proc { self.run_it }
|
||||
def conditional_one_method
|
||||
self.conditional_one = true
|
||||
end
|
||||
def conditional_two_method
|
||||
self.conditional_two = true
|
||||
end
|
||||
end
|
||||
|
||||
class WithTemplateAndUniqueID < CouchRest::ExtendedDocument
|
||||
|
@ -532,6 +558,27 @@ describe "ExtendedDocument" do
|
|||
@doc.save.should be_true
|
||||
@doc.run_after_save.should be_true
|
||||
end
|
||||
it "should run the grouped callbacks before saving" do
|
||||
@doc.run_one.should be_nil
|
||||
@doc.run_two.should be_nil
|
||||
@doc.run_three.should be_nil
|
||||
@doc.save.should be_true
|
||||
@doc.run_one.should be_true
|
||||
@doc.run_two.should be_true
|
||||
@doc.run_three.should be_true
|
||||
end
|
||||
it "should not run conditional callbacks" do
|
||||
@doc.run_it = false
|
||||
@doc.save.should be_true
|
||||
@doc.conditional_one.should be_nil
|
||||
@doc.conditional_two.should be_nil
|
||||
end
|
||||
it "should run conditional callbacks" do
|
||||
@doc.run_it = true
|
||||
@doc.save.should be_true
|
||||
@doc.conditional_one.should be_true
|
||||
@doc.conditional_two.should be_true
|
||||
end
|
||||
end
|
||||
describe "create" do
|
||||
it "should run the before save filter when creating" do
|
||||
|
|
Loading…
Reference in a new issue