forked from langalex/couch_potato
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Callback are implemented with ActiveModel framework
- Loading branch information
kazjote
committed
Aug 18, 2010
1 parent
2491e5a
commit 7ae9a51
Showing
6 changed files
with
53 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,29 @@ | ||
require 'active_support/concern' | ||
require 'active_model/callbacks' | ||
|
||
module CouchPotato | ||
module Persistence | ||
module Callbacks | ||
def self.included(base) #:nodoc: | ||
base.extend ClassMethods | ||
base.extend ActiveModel::Callbacks | ||
|
||
base.class_eval do | ||
attr_accessor :skip_callbacks | ||
def self.callbacks #:nodoc: | ||
@callbacks ||= {:before_validation => [], :before_validation_on_create => [], | ||
:before_validation_on_update => [], :before_validation_on_save => [], :before_create => [], | ||
:after_create => [], :before_update => [], :after_update => [], | ||
:before_save => [], :after_save => [], | ||
:before_destroy => [], :after_destroy => []} | ||
end | ||
|
||
define_model_callbacks :create, :save, :update, :destroy | ||
define_model_callbacks *[:save, :create, :update].map {|c| :"validation_on_#{c}"} | ||
define_model_callbacks :validation unless Config.validation_framework == :active_model | ||
end | ||
end | ||
|
||
# Runs all callbacks on a model with the given name, e.g. :after_create. | ||
# | ||
# This method is called by the CouchPotato::Database object when saving/destroying an object | ||
def run_callbacks(name) | ||
def run_callbacks(name, &block) | ||
return if skip_callbacks | ||
|
||
callbacks = self.class.ancestors.map do |clazz| | ||
clazz.callbacks[name] if clazz.respond_to?(:callbacks) | ||
end.flatten.compact.uniq | ||
|
||
callbacks.each do |callback| | ||
if [Symbol, String].include?(callback.class) | ||
send callback | ||
elsif callback.is_a?(Proc) | ||
callback.call self | ||
else | ||
raise "Don't know how to handle callback of type #{callback.class.name}" | ||
end | ||
end | ||
end | ||
|
||
module ClassMethods | ||
[ | ||
:before_validation, | ||
:before_validation_on_create, | ||
:before_validation_on_update, | ||
:before_validation_on_save, | ||
:before_create, | ||
:before_save, | ||
:before_update, | ||
:before_destroy, | ||
:after_update, | ||
:after_save, | ||
:after_create, | ||
:after_destroy | ||
].each do |callback| | ||
define_method callback do |*names| | ||
callbacks[callback].push *names | ||
end | ||
end | ||
send(:"_run_#{name}_callbacks", &block) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,4 +69,4 @@ def watered? | |
end | ||
end | ||
|
||
end | ||
end |
7ae9a51
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great work. do we really need the activesupport dependency though?
7ae9a51
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks :)
If it is for
require 'active_support/concern'
- I think it might be the bug in ActiveModel and it should be required by ActiveModel itself.Anyway ActiveModel depends on ActiveRecord so using ActiveModel we depend on ActiveSupport.