From cf616e48765d1aa378d04618c044b493fd9583e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 16 Apr 2010 16:20:31 +0200 Subject: merge callbacks_observers_test.rb into lifecycle_test.rb where other observers test reside Benefits: test able to run independently, subclassing instead of changing the original Comment model Signed-off-by: Jeremy Kemper --- activerecord/test/cases/lifecycle_test.rb | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'activerecord/test/cases/lifecycle_test.rb') diff --git a/activerecord/test/cases/lifecycle_test.rb b/activerecord/test/cases/lifecycle_test.rb index fcad3e90d3..5e71ed0221 100644 --- a/activerecord/test/cases/lifecycle_test.rb +++ b/activerecord/test/cases/lifecycle_test.rb @@ -3,6 +3,7 @@ require 'models/topic' require 'models/developer' require 'models/reply' require 'models/minimalistic' +require 'models/comment' class SpecialDeveloper < Developer; end @@ -57,6 +58,28 @@ class MultiObserver < ActiveRecord::Observer end end +class ValidatedComment < Comment + attr_accessor :callers + + before_validation :record_callers + + after_validation do + record_callers + end + + def record_callers + callers << self.class if callers + end +end + +class ValidatedCommentObserver < ActiveRecord::Observer + attr_accessor :callers + + def after_validation(model) + callers << self.class if callers + end +end + class LifecycleTest < ActiveRecord::TestCase fixtures :topics, :developers, :minimalistics @@ -125,4 +148,15 @@ class LifecycleTest < ActiveRecord::TestCase def test_invalid_observer assert_raise(ArgumentError) { Topic.observers = Object.new; Topic.instantiate_observers } end + + test "model callbacks fire before observers are notified" do + callers = [] + + comment = ValidatedComment.new + comment.callers = ValidatedCommentObserver.instance.callers = callers + + comment.valid? + assert_equal [ValidatedComment, ValidatedComment, ValidatedCommentObserver], callers, + "model callbacks did not fire before observers were notified" + end end -- cgit v1.2.3 From c2ca73c9ee5fc3dadf69cf565bd5e2bb30c82c50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 16 Apr 2010 19:30:40 +0200 Subject: ActiveModel::Observing: stop using Observable Ruby module, re-implement `notify_observers` `Observable#notify_observers` from Ruby always returns false (which halts ActiveRecord callback chains) and has extra features (like `changed`) that were never used. Signed-off-by: Jeremy Kemper --- activerecord/test/cases/lifecycle_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'activerecord/test/cases/lifecycle_test.rb') diff --git a/activerecord/test/cases/lifecycle_test.rb b/activerecord/test/cases/lifecycle_test.rb index 5e71ed0221..233338498f 100644 --- a/activerecord/test/cases/lifecycle_test.rb +++ b/activerecord/test/cases/lifecycle_test.rb @@ -7,6 +7,14 @@ require 'models/comment' class SpecialDeveloper < Developer; end +class SalaryChecker < ActiveRecord::Observer + observe :special_developer + + def before_save(developer) + return developer.salary > 80000 + end +end + class TopicaAuditor < ActiveRecord::Observer observe :topic @@ -159,4 +167,16 @@ class LifecycleTest < ActiveRecord::TestCase assert_equal [ValidatedComment, ValidatedComment, ValidatedCommentObserver], callers, "model callbacks did not fire before observers were notified" end + + test "able to save developer" do + SalaryChecker.instance # activate + developer = SpecialDeveloper.new :name => 'Roger', :salary => 100000 + assert developer.save, "developer with normal salary failed to save" + end + + test "unable to save developer with low salary" do + SalaryChecker.instance # activate + developer = SpecialDeveloper.new :name => 'Rookie', :salary => 50000 + assert !developer.save, "allowed to save a developer with too low salary" + end end -- cgit v1.2.3