diff --git a/couch_potato.gemspec b/couch_potato.gemspec index 4d6a7a75..7861258a 100644 --- a/couch_potato.gemspec +++ b/couch_potato.gemspec @@ -144,13 +144,16 @@ Gem::Specification.new do |s| if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then s.add_runtime_dependency(%q, [">= 0"]) s.add_runtime_dependency(%q, [">= 0.24"]) + s.add_runtime_dependency(%q, [">= 0"]) else s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0.24"]) + s.add_dependency(%q, [">= 0"]) end else s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0.24"]) + s.add_dependency(%q, [">= 0"]) end end diff --git a/lib/couch_potato/database.rb b/lib/couch_potato/database.rb index dafad272..0dfe0b3a 100644 --- a/lib/couch_potato/database.rb +++ b/lib/couch_potato/database.rb @@ -79,10 +79,10 @@ def save_document!(document) alias_method :save!, :save_document! def destroy_document(document) - document.run_callbacks :before_destroy - document._deleted = true - database.delete_doc document.to_hash - document.run_callbacks :after_destroy + document.run_callbacks :destroy do + document._deleted = true + database.delete_doc document.to_hash + end document._id = nil document._rev = nil end @@ -112,35 +112,39 @@ def create_document(document, validate) if validate document.errors.clear - document.run_callbacks :before_validation_on_save - document.run_callbacks :before_validation_on_create - return false unless valid_document?(document) + document.run_callbacks :validation_on_save do + document.run_callbacks :validation_on_create do + return false unless valid_document?(document) + end + end end - document.run_callbacks :before_save - document.run_callbacks :before_create - res = database.save_doc document.to_hash - document._rev = res['rev'] - document._id = res['id'] - document.run_callbacks :after_save - document.run_callbacks :after_create + document.run_callbacks :save do + document.run_callbacks :create do + res = database.save_doc document.to_hash + document._rev = res['rev'] + document._id = res['id'] + end + end true end def update_document(document, validate) if validate document.errors.clear - document.run_callbacks :before_validation_on_save - document.run_callbacks :before_validation_on_update - return false unless valid_document?(document) + document.run_callbacks :validation_on_save do + document.run_callbacks :validation_on_update do + return false unless valid_document?(document) + end + end + end + + document.run_callbacks :save do + document.run_callbacks :update do + res = database.save_doc document.to_hash + document._rev = res['rev'] + end end - - document.run_callbacks :before_save - document.run_callbacks :before_update - res = database.save_doc document.to_hash - document._rev = res['rev'] - document.run_callbacks :after_save - document.run_callbacks :after_update true end @@ -158,4 +162,4 @@ def database end end -end \ No newline at end of file +end diff --git a/lib/couch_potato/persistence/callbacks.rb b/lib/couch_potato/persistence/callbacks.rb index a392ac4a..31c936d3 100644 --- a/lib/couch_potato/persistence/callbacks.rb +++ b/lib/couch_potato/persistence/callbacks.rb @@ -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 \ No newline at end of file +end diff --git a/lib/couch_potato/validation/with_validatable.rb b/lib/couch_potato/validation/with_validatable.rb index 62a448ce..b6167d01 100644 --- a/lib/couch_potato/validation/with_validatable.rb +++ b/lib/couch_potato/validation/with_validatable.rb @@ -13,11 +13,12 @@ def self.included(base) # Override the validate method to first run before_validation callback def valid? errors.clear - run_callbacks :before_validation - before_validation_errors = errors.errors.dup - super - before_validation_errors.each do |k, v| - v.each {|message| errors.add(k, message)} + run_callbacks :validation do + before_validation_errors = errors.errors.dup + super + before_validation_errors.each do |k, v| + v.each {|message| errors.add(k, message)} + end end errors.empty? end diff --git a/spec/callbacks_spec.rb b/spec/callbacks_spec.rb index eeafc2ea..b6c38d9d 100644 --- a/spec/callbacks_spec.rb +++ b/spec/callbacks_spec.rb @@ -58,7 +58,7 @@ def eat_apple it "should run all callback methods given to the callback method call" do monkey = Monkey.new - monkey.run_callbacks :before_create + monkey.run_callbacks :create monkey.eaten_banana.should be_true monkey.eaten_apple.should be_true end @@ -266,7 +266,7 @@ def eat_apple describe "lambda callbacks" do it "should run the lambda" do recorder = CallbackRecorder.new - recorder.run_callbacks :before_create + recorder.run_callbacks :create recorder.lambda_works.should be_true end end @@ -305,4 +305,4 @@ def check_name user.valid?.should == true user.errors.on(:name).should == nil end -end \ No newline at end of file +end diff --git a/spec/unit/callbacks_spec.rb b/spec/unit/callbacks_spec.rb index 2ac1d465..4698aa1e 100644 --- a/spec/unit/callbacks_spec.rb +++ b/spec/unit/callbacks_spec.rb @@ -69,4 +69,4 @@ def watered? end end -end \ No newline at end of file +end