Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rails::Engine.find(path) - method to find engine by path

  • Loading branch information...
commit 051127d4d38329238bb0b3587ef128f131174786 1 parent 5df72a2
Piotr Sarnacki drogus authored
8 railties/lib/rails/application/railties.rb
View
@@ -8,14 +8,6 @@ def all(&block)
@all.each(&block) if block
@all
end
-
- def railties
- @railties ||= ::Rails::Railtie.subclasses.map(&:instance)
- end
-
- def engines
- @engines ||= ::Rails::Engine.subclasses.map(&:instance)
- end
end
end
end
5 railties/lib/rails/engine.rb
View
@@ -371,6 +371,11 @@ def isolate_namespace(mod)
end
end
end
+
+ # Finds engine with given path
+ def find(path)
+ Rails::Engine::Railties.engines.find { |r| File.expand_path(r.root.to_s) == File.expand_path(path.to_s) }
+ end
end
delegate :middleware, :root, :paths, :to => :config
10 railties/lib/rails/engine/railties.rb
View
@@ -18,6 +18,16 @@ def plugins
Plugin.all(plugin_names, @config.paths["vendor/plugins"].existent)
end
end
+
+ def self.railties
+ @railties ||= ::Rails::Railtie.subclasses.map(&:instance)
+ end
+
+ def self.engines
+ @engines ||= ::Rails::Engine.subclasses.map(&:instance)
+ end
+
+ delegate :railties, :engines, :to => "self.class"
end
end
end
19 railties/test/railties/engine_test.rb
View
@@ -702,5 +702,24 @@ class Engine < ::Rails::Engine
assert_equal "foo", Bukkits.table_name_prefix
end
+
+ test "fetching engine by path" do
+ @plugin.write "lib/bukkits.rb", <<-RUBY
+ module Bukkits
+ class Engine < ::Rails::Engine
+ end
+ end
+ RUBY
+
+ boot_rails
+ require "#{rails_root}/config/environment"
+
+ assert_equal Bukkits::Engine.instance, Rails::Engine.find(@plugin.path)
+
+ # check expanding paths
+ engine_dir = @plugin.path.chomp("/").split("/").last
+ engine_path = File.join(@plugin.path, '..', engine_dir)
+ assert_equal Bukkits::Engine.instance, Rails::Engine.find(engine_path)
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.