More convenient callback alias methods
This commit is contained in:
parent
806a79a72a
commit
ab362cb32c
5 changed files with 27 additions and 11 deletions
|
@ -84,6 +84,12 @@ CouchRest uses a mixin you can find in lib/mixins/callbacks which is extracted f
|
|||
set_callback :save, :after, :after_method, :if => :condition
|
||||
set_callback :save, :around {|r| stuff; yield; stuff }
|
||||
|
||||
Or the new shorter version:
|
||||
|
||||
before_save :before_method
|
||||
after_save :after_method, :if => :condition
|
||||
around_save {|r| stuff; yield; stuff }
|
||||
|
||||
Check the mixin or the ExtendedDocument class to see how to implement your own callbacks.
|
||||
|
||||
### Casting
|
||||
|
|
|
@ -528,6 +528,16 @@ module CouchRest
|
|||
|
||||
self.set_callback(:#{symbol}, :before)
|
||||
RUBY_EVAL
|
||||
|
||||
# Define more convenient callback methods
|
||||
# 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)
|
||||
end
|
||||
RUBY_EVAL
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -35,10 +35,10 @@ class WithCastedCallBackModel < Hash
|
|||
property :run_before_validate
|
||||
property :run_after_validate
|
||||
|
||||
set_callback :validate, :before do |object|
|
||||
before_validate do |object|
|
||||
object.run_before_validate = true
|
||||
end
|
||||
set_callback :validate, :after do |object|
|
||||
after_validate do |object|
|
||||
object.run_after_validate = true
|
||||
end
|
||||
end
|
||||
|
|
|
@ -29,28 +29,28 @@ describe "ExtendedDocument" do
|
|||
property :run_before_update
|
||||
property :run_after_update
|
||||
|
||||
set_callback :validate, :before do |object|
|
||||
before_validate do |object|
|
||||
object.run_before_validate = true
|
||||
end
|
||||
set_callback :validate, :after do |object|
|
||||
after_validate do |object|
|
||||
object.run_after_validate = true
|
||||
end
|
||||
set_callback :save, :before do |object|
|
||||
before_save do |object|
|
||||
object.run_before_save = true
|
||||
end
|
||||
set_callback :save, :after do |object|
|
||||
after_save do |object|
|
||||
object.run_after_save = true
|
||||
end
|
||||
set_callback :create, :before do |object|
|
||||
before_create do |object|
|
||||
object.run_before_create = true
|
||||
end
|
||||
set_callback :create, :after do |object|
|
||||
after_create do |object|
|
||||
object.run_after_create = true
|
||||
end
|
||||
set_callback :update, :before do |object|
|
||||
before_update do |object|
|
||||
object.run_before_update = true
|
||||
end
|
||||
set_callback :update, :after do |object|
|
||||
after_update do |object|
|
||||
object.run_after_update = true
|
||||
end
|
||||
end
|
||||
|
|
2
spec/fixtures/more/article.rb
vendored
2
spec/fixtures/more/article.rb
vendored
|
@ -26,7 +26,7 @@ class Article < CouchRest::ExtendedDocument
|
|||
|
||||
timestamps!
|
||||
|
||||
set_callback :save, :before, :generate_slug_from_title
|
||||
before_save :generate_slug_from_title
|
||||
|
||||
def generate_slug_from_title
|
||||
self['slug'] = title.downcase.gsub(/[^a-z0-9]/,'-').squeeze('-').gsub(/^\-|\-$/,'') if new?
|
||||
|
|
Loading…
Reference in a new issue