diff options
author | Nicholas Seckar <nseckar@gmail.com> | 2006-02-02 05:41:00 +0000 |
---|---|---|
committer | Nicholas Seckar <nseckar@gmail.com> | 2006-02-02 05:41:00 +0000 |
commit | 1bce58b31289362d62863ad0600b924858a11e34 (patch) | |
tree | a99ebc764c8e91e04b490bd6145a5201341e50c3 | |
parent | 4bb6f863b42814707c0df6d972c931c6925b3de8 (diff) | |
download | rails-1bce58b31289362d62863ad0600b924858a11e34.tar.gz rails-1bce58b31289362d62863ad0600b924858a11e34.tar.bz2 rails-1bce58b31289362d62863ad0600b924858a11e34.zip |
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
-rw-r--r-- | actionmailer/lib/action_mailer/base.rb | 7 | ||||
-rwxr-xr-x | actionpack/lib/action_controller/base.rb | 11 | ||||
-rw-r--r-- | actionpack/lib/action_controller/caching.rb | 6 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/observer.rb | 7 | ||||
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/reloadable.rb | 11 | ||||
-rw-r--r-- | activesupport/test/reloadable_test.rb | 37 |
8 files changed, 60 insertions, 26 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 97bd83395c..1aab9e23df 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -123,11 +123,8 @@ module ActionMailer # 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 diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 862d2e5efd..0b4a8ca3c5 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -224,14 +224,9 @@ module ActionController #:nodoc: # 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 diff --git a/actionpack/lib/action_controller/caching.rb b/actionpack/lib/action_controller/caching.rb index e62fe11a19..5e6fe3948b 100644 --- a/actionpack/lib/action_controller/caching.rb +++ b/actionpack/lib/action_controller/caching.rb @@ -523,10 +523,8 @@ module ActionController #: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) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index f134402c58..aa13dc6d68 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -242,9 +242,10 @@ module ActiveRecord #:nodoc: # 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 diff --git a/activerecord/lib/active_record/observer.rb b/activerecord/lib/active_record/observer.rb index d43909c3c5..ae0d7560e2 100644 --- a/activerecord/lib/active_record/observer.rb +++ b/activerecord/lib/active_record/observer.rb @@ -83,11 +83,8 @@ module ActiveRecord # 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 } diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 9fe54c2397..b2dfa0744c 100644 --- a/activesupport/CHANGELOG +++ b/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 diff --git a/activesupport/lib/active_support/reloadable.rb b/activesupport/lib/active_support/reloadable.rb index 49e6442a37..7749f0b165 100644 --- a/activesupport/lib/active_support/reloadable.rb +++ b/activesupport/lib/active_support/reloadable.rb @@ -14,4 +14,15 @@ module Reloadable 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
\ No newline at end of file diff --git a/activesupport/test/reloadable_test.rb b/activesupport/test/reloadable_test.rb index adac2fccec..fb1d61740d 100644 --- a/activesupport/test/reloadable_test.rb +++ b/activesupport/test/reloadable_test.rb @@ -27,6 +27,19 @@ module ReloadableTestSandbox 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 @@ class ReloadableTest < Test::Unit::TestCase 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| |