mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix observed_class. References #11099.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8889 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
5457770a36
commit
23e58a0552
2 changed files with 6 additions and 7 deletions
|
@ -153,7 +153,7 @@ module ActiveRecord
|
|||
# The class observed by default is inferred from the observer's class name:
|
||||
# assert_equal Person, PersonObserver.observed_class
|
||||
def observed_class
|
||||
if observed_class_name = /(.*)Observer/.match(name)[1]
|
||||
if observed_class_name = name[/(.*)Observer/, 1]
|
||||
observed_class_name.constantize
|
||||
else
|
||||
nil
|
||||
|
|
|
@ -26,7 +26,7 @@ class TopicManualObserver
|
|||
end
|
||||
end
|
||||
|
||||
class TopicaObserver < ActiveRecord::Observer
|
||||
class TopicaAuditor < ActiveRecord::Observer
|
||||
observe :topic
|
||||
|
||||
attr_reader :topic
|
||||
|
@ -95,7 +95,9 @@ class LifecycleTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_auto_observer
|
||||
topic_observer = TopicaObserver.instance
|
||||
topic_observer = TopicaAuditor.instance
|
||||
assert_nil TopicaAuditor.observed_class
|
||||
assert_equal [Topic], TopicaAuditor.instance.observed_classes.to_a
|
||||
|
||||
topic = Topic.find(1)
|
||||
assert_equal topic.title, topic_observer.topic.title
|
||||
|
@ -103,6 +105,7 @@ class LifecycleTest < ActiveRecord::TestCase
|
|||
|
||||
def test_inferred_auto_observer
|
||||
topic_observer = TopicObserver.instance
|
||||
assert_equal Topic, TopicObserver.observed_class
|
||||
|
||||
topic = Topic.find(1)
|
||||
assert_equal topic.title, topic_observer.topic.title
|
||||
|
@ -134,8 +137,4 @@ class LifecycleTest < ActiveRecord::TestCase
|
|||
def test_invalid_observer
|
||||
assert_raise(ArgumentError) { Topic.observers = Object.new; Topic.instantiate_observers }
|
||||
end
|
||||
|
||||
def test_getting_observed_class_from_class_name
|
||||
assert_equal Topic, TopicObserver.observed_class
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue