Skip to content
This repository
Browse code

Solve some pendencies.

  • Loading branch information...
commit 2fde9d774b322fc708990675671231c64c691a33 1 parent d3c4024
José Valim josevalim authored
2  actionpack/lib/action_dispatch/railtie.rb
@@ -6,7 +6,7 @@ class Railtie < Rails::Railtie
6 6 plugin_name :action_dispatch
7 7
8 8 # Initialize route files to an array
9   - config.action_dispatch.route_files = []
  9 + config.action_dispatch.route_paths = []
10 10
11 11 # Prepare dispatcher callbacks and run 'prepare' callbacks
12 12 initializer "action_dispatch.prepare_dispatcher" do |app|
5 railties/lib/rails/application.rb
... ... @@ -1,8 +1,7 @@
1 1 require 'fileutils'
2 2 require 'rails/railties_path'
3   -require 'rails/railtie'
4   -require 'rails/engine'
5 3 require 'rails/plugin'
  4 +require 'rails/engine'
6 5
7 6 module Rails
8 7 class Application < Engine
@@ -14,7 +13,6 @@ class Application < Engine
14 13
15 14 # TODO Check helpers works as expected
16 15 # TODO Check routes namespaces
17   - # TODO raise "You cannot have more than one Rails::Application" if Rails.application
18 16 # TODO Ensure production settings are read properly
19 17 class << self
20 18 private :new
@@ -25,6 +23,7 @@ def instance
25 23 end
26 24
27 25 def inherited(base)
  26 + raise "You cannot have more than one Rails::Application" if Rails.application
28 27 super
29 28 Rails.application = base.instance
30 29 base.require_environment!
2  railties/lib/rails/application/finisher.rb
@@ -17,7 +17,7 @@ module Finisher
17 17
18 18 initializer :add_builtin_route do |app|
19 19 if Rails.env.development?
20   - app.config.action_dispatch.route_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb')
  20 + app.config.action_dispatch.route_paths << File.join(RAILTIES_PATH, 'builtin', 'routes.rb')
21 21 end
22 22 end
23 23
2  railties/lib/rails/application/railties.rb
... ... @@ -1,7 +1,7 @@
1 1 module Rails
2 2 class Application
3 3 class Railties
4   - # TODO Write tests
  4 + # TODO Write tests for this behavior extracted from Application
5 5 def initialize(config)
6 6 @config = config
7 7 end
13 railties/lib/rails/application/routes_reloader.rb
... ... @@ -1,8 +1,7 @@
1 1 module Rails
2 2 class Application
3 3 class RoutesReloader
4   - # TODO Change config.action_dispatch.route_files to config.action_dispatch.routes_path
5   - # TODO Write tests
  4 + # TODO Write tests for this behavior extracted from Application
6 5 def initialize(config)
7 6 @config, @last_change_at = config, nil
8 7 end
@@ -10,8 +9,8 @@ def initialize(config)
10 9 def changed_at
11 10 routes_changed_at = nil
12 11
13   - files.each do |file|
14   - config_changed_at = File.stat(file).mtime
  12 + paths.each do |path|
  13 + config_changed_at = File.stat(path).mtime
15 14
16 15 if routes_changed_at.nil? || config_changed_at > routes_changed_at
17 16 routes_changed_at = config_changed_at
@@ -26,7 +25,7 @@ def reload!
26 25 routes.disable_clear_and_finalize = true
27 26
28 27 routes.clear!
29   - files.each { |file| load(file) }
  28 + paths.each { |path| load(path) }
30 29 routes.finalize!
31 30
32 31 nil
@@ -42,8 +41,8 @@ def reload_if_changed
42 41 end
43 42 end
44 43
45   - def files
46   - @config.action_dispatch.route_files
  44 + def paths
  45 + @config.action_dispatch.route_paths
47 46 end
48 47 end
49 48 end
4 railties/lib/rails/engine.rb
@@ -67,9 +67,9 @@ def load_tasks
67 67 config.load_once_paths.freeze
68 68 end
69 69
70   - initializer :add_routing_files do
  70 + initializer :add_routing_paths do
71 71 config.paths.config.routes.to_a.each do |route|
72   - config.action_dispatch.route_files.unshift(route) if File.exists?(route)
  72 + config.action_dispatch.route_paths.unshift(route) if File.exists?(route)
73 73 end
74 74 end
75 75
23 railties/lib/rails/paths.rb
@@ -31,22 +31,21 @@ def initialize(path)
31 31 @all_paths = []
32 32 end
33 33
34   - def load_once
35   - all_paths.map { |path| path.paths if path.load_once? }.compact.flatten.uniq
  34 + def all_paths
  35 + @all_paths.uniq!
  36 + @all_paths
36 37 end
37 38
38   - def eager_load
39   - all_paths.map { |path| path.paths if path.eager_load? }.compact.flatten.uniq
  39 + def load_once
  40 + filter { |path| path.paths if path.load_once? }
40 41 end
41 42
42   - # TODO Discover why do we need to call uniq! here
43   - def all_paths
44   - @all_paths.uniq!
45   - @all_paths
  43 + def eager_load
  44 + filter { |path| path.paths if path.eager_load? }
46 45 end
47 46
48 47 def load_paths
49   - all_paths.map { |path| path.paths if path.load_path? }.compact.flatten.uniq
  48 + filter { |path| path.paths if path.load_path? }
50 49 end
51 50
52 51 def push(*)
@@ -56,6 +55,12 @@ def push(*)
56 55 alias unshift push
57 56 alias << push
58 57 alias concat push
  58 +
  59 + protected
  60 +
  61 + def filter(&block)
  62 + all_paths.map(&block).compact.flatten.uniq.select { |p| File.exists?(p) }
  63 + end
59 64 end
60 65
61 66 class Path
9 railties/lib/rails/plugin.rb
@@ -6,8 +6,6 @@ def self.inherited(base)
6 6 raise "You cannot inherit from Rails::Plugin"
7 7 end
8 8
9   - # TODO Right now, if a plugin has an Engine or a Railtie inside it,
10   - # the initializers for this plugin will be executed twice.
11 9 def self.all(list, paths)
12 10 plugins = []
13 11 paths.each do |path|
@@ -39,5 +37,12 @@ def config
39 37 config = app.config
40 38 eval(File.read(file), binding, file) if file && File.file?(file)
41 39 end
  40 +
  41 + # TODO Write tests for this sanity check
  42 + initializer :sanity_check_railties_collision do
  43 + if Engine.subclasses.map { |k| k.root.to_s }.include?(root.to_s)
  44 + raise "The plugin #{name.inspect} is a Railtie or an Engine and cannot be installed as Plugin"
  45 + end
  46 + end
42 47 end
43 48 end
1  railties/test/paths_test.rb
@@ -3,6 +3,7 @@
3 3
4 4 class PathsTest < ActiveSupport::TestCase
5 5 def setup
  6 + File.stubs(:exists?).returns(true)
6 7 @root = Rails::Paths::Root.new("/foo/bar")
7 8 end
8 9
6 railties/test/plugins/configuration_test.rb
@@ -26,11 +26,11 @@ class Foo < Rails::Railtie ; config.foo.greetings = "hello" ; end
26 26
27 27 test "plugin config merges are deep" do
28 28 class Foo < Rails::Railtie ; config.foo.greetings = 'hello' ; end
29   - class MyApp < Rails::Application
  29 + class Bar < Rails::Railtie
30 30 config.foo.bar = "bar"
31 31 end
32   - assert_equal "hello", MyApp.config.foo.greetings
33   - assert_equal "bar", MyApp.config.foo.bar
  32 + assert_equal "hello", Bar.config.foo.greetings
  33 + assert_equal "bar", Bar.config.foo.bar
34 34 end
35 35
36 36 test "plugin can add subscribers" do

0 comments on commit 2fde9d7

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