Browse files

Add Reloadable::OnlySubclasses which handles the common case where a …

…base class should not be reloaded, but its subclasses should be.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3521 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 4bb6f86 commit 1bce58b31289362d62863ad0600b924858a11e34 @seckar seckar committed Feb 2, 2006
View
7 actionmailer/lib/action_mailer/base.rb
@@ -123,11 +123,8 @@ class Base
# Action Mailer subclasses should be reloaded by the dispatcher in Rails
# when Dependencies.mechanism = :load.
- def self.inherited(child) #:nodoc:
- child.send :include, Reloadable
- super
- end
-
+ include Reloadable::OnlySubclasses
+
private_class_method :new #:nodoc:
cattr_accessor :template_root
View
11 actionpack/lib/action_controller/base.rb
@@ -224,14 +224,9 @@ def initialize(message = nil)
# FCGI.each_cgi{ |cgi| WeblogController.process_cgi(cgi) }
class Base
DEFAULT_RENDER_STATUS_CODE = "200 OK"
-
- # Action Controller subclasses should be reloaded by the dispatcher in Rails
- # when Dependencies.mechanism = :load.
- def self.inherited(child) #:nodoc:
- child.send :include, Reloadable
- super
- end
-
+
+ include Reloadable::OnlySubclasses
+
# Determines whether the view has access to controller internals @request, @response, @session, and @template.
# By default, it does.
@@view_controller_internals = true
View
6 actionpack/lib/action_controller/caching.rb
@@ -523,10 +523,8 @@ class Sweeper < ActiveRecord::Observer #:nodoc:
# ActiveRecord::Observer will mark this class as reloadable even though it should not be.
# However, subclasses of ActionController::Caching::Sweeper should be Reloadable
- def self.reloadable? #:nodoc:
- self != Sweeper
- end
-
+ include Reloadable::OnlySubclasses
+
def before(controller)
self.controller = controller
callback(:before)
View
5 activerecord/lib/active_record/base.rb
@@ -242,9 +242,10 @@ class Base
# Accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then passed
# on to any new database connections made and which can be retrieved on both a class and instance level by calling +logger+.
cattr_accessor :logger
-
+
+ include Reloadable::OnlySubclasses
+
def self.inherited(child) #:nodoc:
- child.send :include, Reloadable
@@subclasses[self] ||= []
@@subclasses[self] << child
super
View
7 activerecord/lib/active_record/observer.rb
@@ -83,11 +83,8 @@ class Observer
# Observer subclasses should be reloaded by the dispatcher in Rails
# when Dependencies.mechanism = :load.
- def self.inherited(child) #:nodoc:
- child.send :include, Reloadable
- super
- end
-
+ include Reloadable::OnlySubclasses
+
# Attaches the observer to the supplied model classes.
def self.observe(*models)
define_method(:observed_class) { models }
View
2 activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add Reloadable::OnlySubclasses which handles the common case where a base class should not be reloaded, but its subclasses should be. [Nicholas Seckar]
+
* Further improvements to reloading code [Nicholas Seckar, Trevor Squires]
- All classes/modules which include Reloadable can define reloadable? for fine grained control of reloading
View
11 activesupport/lib/active_support/reloadable.rb
@@ -14,4 +14,15 @@ def reloadable_classes
included_in_classes.select { |klass| klass.reloadable? }
end
end
+
+ module OnlySubclasses
+ class << self
+ def included(base) #nodoc:
+ base.send :include, Reloadable
+ (class << base; self; end;).class_eval do
+ define_method(:reloadable?) { self != base }
+ end
+ end
+ end
+ end
end
View
37 activesupport/test/reloadable_test.rb
@@ -27,6 +27,19 @@ def self.reloadable?
end
include Reloadable
end
+
+ class OnlySubclassesReloadable
+ include Reloadable::OnlySubclasses
+ end
+ class ASubclassOfOnlySubclassesReloadable < OnlySubclassesReloadable
+ end
+
+ class AnOnlySubclassReloadableClassSubclassingAReloadableClass
+ include Reloadable::OnlySubclasses
+ end
+
+ class ASubclassofAOnlySubclassReloadableClassWhichWasSubclassingAReloadableClass < AnOnlySubclassReloadableClassSubclassingAReloadableClass
+ end
end
class ReloadableTest < Test::Unit::TestCase
@@ -43,9 +56,29 @@ def test_reloadable_is_not_overwritten_if_present
assert_equal 10, ReloadableTestSandbox::AClassWhichDefinesItsOwnReloadable.reloadable?
end
+ def test_only_subclass_reloadable
+ assert ! ReloadableTestSandbox::OnlySubclassesReloadable.reloadable?
+ assert ReloadableTestSandbox::ASubclassOfOnlySubclassesReloadable.reloadable?
+ end
+
+ def test_inside_hierarchy_only_subclass_reloadable
+ assert ! ReloadableTestSandbox::AnOnlySubclassReloadableClassSubclassingAReloadableClass.reloadable?
+ assert ReloadableTestSandbox::ASubclassofAOnlySubclassReloadableClassWhichWasSubclassingAReloadableClass.reloadable?
+ end
+
def test_removable_classes
- reloadables = %w(AReloadableClass AReloadableClassWithSubclasses AReloadableSubclass AClassWhichDefinesItsOwnReloadable)
- non_reloadables = %w(ANonReloadableSubclass AModuleIncludingReloadable)
+ reloadables = %w(
+ AReloadableClass
+ AReloadableClassWithSubclasses
+ AReloadableSubclass
+ AClassWhichDefinesItsOwnReloadable
+ ASubclassOfOnlySubclassesReloadable
+ )
+ non_reloadables = %w(
+ ANonReloadableSubclass
+ AModuleIncludingReloadable
+ OnlySubclassesReloadable
+ )
results = Reloadable.reloadable_classes
reloadables.each do |name|

0 comments on commit 1bce58b

Please sign in to comment.