Skip to content
This repository
Browse code

Add config.file_watcher so developers can provide their own watchers …

…(for instance, hooking on fsevents).
  • Loading branch information...
commit cd3033eb627f408b71a7adf98c350e2c01115c33 1 parent 148d15d
José Valim authored December 13, 2011
4  railties/guides/source/configuring.textile
Source Rendered
@@ -82,6 +82,8 @@ NOTE. The +config.asset_path+ configuration is ignored if the asset pipeline is
82 82
 
83 83
 * +config.encoding+ sets up the application-wide encoding. Defaults to UTF-8.
84 84
 
  85
+* +config.file_watcher+ the class used to detect file updates in the filesystem when +config.reload_classes_only_on_change+ is true. Must conform to +ActiveSupport::FileUpdateChecker+ API.
  86
+
85 87
 * +config.filter_parameters+ used for filtering out the parameters that you don't want shown in the logs, such as passwords or credit card numbers.
86 88
 
87 89
 * +config.force_ssl+ forces all requests to be under HTTPS protocol by using +Rack::SSL+ middleware.
@@ -98,7 +100,7 @@ NOTE. The +config.asset_path+ configuration is ignored if the asset pipeline is
98 100
 
99 101
 * +config.preload_frameworks+ enables or disables preloading all frameworks at startup. Enabled by +config.threadsafe!+. Defaults to +nil+, so is disabled.
100 102
 
101  
-* +config.reload_classes_only_on_change+ enables or disables reloading of classes only when tracked files change. By default tracks everything on autoload paths and is set to true.
  103
+* +config.reload_classes_only_on_change+ enables or disables reloading of classes only when tracked files change. By default tracks everything on autoload paths and is set to true. If +config.cache_classes+ is true, this option is ignored.
102 104
 
103 105
 * +config.reload_plugins+ enables or disables plugin reloading. Defaults to false.
104 106
 
3  railties/lib/rails/application.rb
... ...
@@ -1,5 +1,4 @@
1 1
 require 'active_support/core_ext/hash/reverse_merge'
2  
-require 'active_support/file_update_checker'
3 2
 require 'fileutils'
4 3
 require 'rails/plugin'
5 4
 require 'rails/engine'
@@ -135,7 +134,7 @@ def set_dependencies_hook
135 134
       end
136 135
 
137 136
       if config.reload_classes_only_on_change
138  
-        reloader = ActiveSupport::FileUpdateChecker.new(watchable_args, true, &callback)
  137
+        reloader = config.file_watcher.new(watchable_args, true, &callback)
139 138
         self.reloaders << reloader
140 139
         # We need to set a to_prepare callback regardless of the reloader result, i.e.
141 140
         # models should be reloaded if any of the reloaders (i18n, routes) were updated.
6  railties/lib/rails/application/configuration.rb
... ...
@@ -1,5 +1,6 @@
1 1
 require 'active_support/core_ext/string/encoding'
2 2
 require 'active_support/core_ext/kernel/reporting'
  3
+require 'active_support/file_update_checker'
3 4
 require 'rails/engine/configuration'
4 5
 
5 6
 module Rails
@@ -7,8 +8,8 @@ class Application
7 8
     class Configuration < ::Rails::Engine::Configuration
8 9
       attr_accessor :allow_concurrency, :asset_host, :asset_path, :assets,
9 10
                     :cache_classes, :cache_store, :consider_all_requests_local,
10  
-                    :dependency_loading, :filter_parameters, :force_ssl, :helpers_paths,
11  
-                    :logger, :log_tags, :preload_frameworks,
  11
+                    :dependency_loading, :file_watcher, :filter_parameters,
  12
+                    :force_ssl, :helpers_paths, :logger, :log_tags, :preload_frameworks,
12 13
                     :railties_order, :relative_url_root, :reload_plugins, :secret_token,
13 14
                     :serve_static_assets, :ssl_options, :static_cache_control, :session_options,
14 15
                     :time_zone, :reload_classes_only_on_change, :whiny_nils
@@ -38,6 +39,7 @@ def initialize(*)
38 39
         @railties_order                = [:all]
39 40
         @relative_url_root             = ENV["RAILS_RELATIVE_URL_ROOT"]
40 41
         @reload_classes_only_on_change = true
  42
+        @file_watcher                  = ActiveSupport::FileUpdateChecker
41 43
 
42 44
         @assets = ActiveSupport::OrderedOptions.new
43 45
         @assets.enabled                  = false
82  railties/test/application/loading_test.rb
@@ -16,7 +16,7 @@ def app
16 16
     @app ||= Rails.application
17 17
   end
18 18
 
19  
-  def test_constants_in_app_are_autoloaded
  19
+  test "constants in app are autoloaded" do
20 20
     app_file "app/models/post.rb", <<-MODEL
21 21
       class Post < ActiveRecord::Base
22 22
         validates_acceptance_of :title, :accept => "omg"
@@ -33,7 +33,7 @@ class Post < ActiveRecord::Base
33 33
     assert_equal 'omg', p.title
34 34
   end
35 35
 
36  
-  def test_models_without_table_do_not_panic_on_scope_definitions_when_loaded
  36
+  test "models without table do not panic on scope definitions when loaded" do
37 37
     app_file "app/models/user.rb", <<-MODEL
38 38
       class User < ActiveRecord::Base
39 39
         default_scope where(:published => true)
@@ -63,7 +63,7 @@ class User < ActiveRecord::Base
63 63
     assert ::AppTemplate::Application.config.loaded
64 64
   end
65 65
 
66  
-  def test_descendants_are_cleaned_on_each_request_without_cache_classes
  66
+  test "descendants are cleaned on each request without cache classes" do
67 67
     add_to_config <<-RUBY
68 68
       config.cache_classes = false
69 69
       config.reload_classes_only_on_change = false
@@ -99,6 +99,82 @@ class Post < ActiveRecord::Base
99 99
     assert_raise(RuntimeError) { ::AppTemplate::Application.initialize! }
100 100
   end
101 101
 
  102
+  test "reload constants on development" do
  103
+    add_to_config <<-RUBY
  104
+      config.cache_classes = false
  105
+    RUBY
  106
+
  107
+    app_file 'config/routes.rb', <<-RUBY
  108
+      AppTemplate::Application.routes.draw do
  109
+        match '/c', :to => lambda { |env| [200, {"Content-Type" => "text/plain"}, [User.counter.to_s]] }
  110
+      end
  111
+    RUBY
  112
+
  113
+    app_file "app/models/user.rb", <<-MODEL
  114
+      class User
  115
+        def self.counter; 1; end
  116
+      end
  117
+    MODEL
  118
+
  119
+    require 'rack/test'
  120
+    extend Rack::Test::Methods
  121
+
  122
+    require "#{rails_root}/config/environment"
  123
+    sleep(1)
  124
+
  125
+    get "/c"
  126
+    assert_equal "1", last_response.body
  127
+
  128
+    app_file "app/models/user.rb", <<-MODEL
  129
+      class User
  130
+        def self.counter; 2; end
  131
+      end
  132
+    MODEL
  133
+
  134
+    get "/c"
  135
+    assert_equal "2", last_response.body
  136
+  end
  137
+
  138
+  test "does not reload constants on development if custom file watcher always returns false" do
  139
+    add_to_config <<-RUBY
  140
+      config.cache_classes = false
  141
+      config.file_watcher = Class.new do
  142
+        def initialize(*); end
  143
+        def updated?; false; end
  144
+      end
  145
+    RUBY
  146
+
  147
+    app_file 'config/routes.rb', <<-RUBY
  148
+      AppTemplate::Application.routes.draw do
  149
+        match '/c', :to => lambda { |env| [200, {"Content-Type" => "text/plain"}, [User.counter.to_s]] }
  150
+      end
  151
+    RUBY
  152
+
  153
+    app_file "app/models/user.rb", <<-MODEL
  154
+      class User
  155
+        def self.counter; 1; end
  156
+      end
  157
+    MODEL
  158
+
  159
+    require 'rack/test'
  160
+    extend Rack::Test::Methods
  161
+
  162
+    require "#{rails_root}/config/environment"
  163
+    sleep(1)
  164
+
  165
+    get "/c"
  166
+    assert_equal "1", last_response.body
  167
+
  168
+    app_file "app/models/user.rb", <<-MODEL
  169
+      class User
  170
+        def self.counter; 2; end
  171
+      end
  172
+    MODEL
  173
+
  174
+    get "/c"
  175
+    assert_equal "1", last_response.body
  176
+  end
  177
+
102 178
   protected
103 179
 
104 180
   def setup_ar!

0 notes on commit cd3033e

Please sign in to comment.
Something went wrong with that request. Please try again.