Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added that gems can now be plugins if they include rails/init.rb (clo…

…ses #11444) [jbarnette]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9101 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 8301ce4d0b9de4423b74abf842faa6cf9e5aae1b 1 parent 6080943
David Heinemeier Hansson dhh authored
2  railties/CHANGELOG
View
@@ -1,5 +1,7 @@
*SVN*
+* Added that gems can now be plugins if they include rails/init.rb #11444 [jbarnette]
+
* Added Plugin#about method to programmatically access the about.yml in a plugin #10979 [lazyatom]
plugin = Rails::Plugin.new(path_to_my_plugin)
1  railties/environments/boot.rb
View
@@ -43,6 +43,7 @@ def run
class VendorBoot < Boot
def load_initializer
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
+ Rails::Initializer.run(:install_gem_spec_stubs)
end
end
25 railties/lib/initializer.rb
View
@@ -72,6 +72,7 @@ def process
Rails.configuration = configuration
check_ruby_version
+ install_gem_spec_stubs
set_load_path
require_frameworks
@@ -118,6 +119,26 @@ def check_ruby_version
require 'ruby_version_check'
end
+ # If Rails is vendored and RubyGems is available, install stub GemSpecs
+ # for Rails, ActiveSupport, ActiveRecord, ActionPack, ActionMailer, and
+ # ActiveResource. This allows Gem plugins to depend on Rails even when
+ # the Gem version of Rails shouldn't be loaded.
+ def install_gem_spec_stubs
+ if Rails.vendor_rails?
+ begin; require "rubygems"; rescue LoadError; return; end
+
+ stubs = %w(rails activesupport activerecord actionpack actionmailer activeresource)
+ stubs.reject! { |s| Gem.loaded_specs.key?(s) }
+
+ stubs.each do |stub|
+ Gem.loaded_specs[stub] = Gem::Specification.new do |s|
+ s.name = stub
+ s.version = Rails::VERSION::STRING
+ end
+ end
+ end
+ end
+
# Set the <tt>$LOAD_PATH</tt> based on the value of
# Configuration#load_paths. Duplicates are removed.
def set_load_path
@@ -666,7 +687,9 @@ def default_plugin_paths
end
def default_plugin_locators
- [Plugin::FileSystemLocator]
+ locators = []
+ locators << Plugin::GemLocator if defined? Gem
+ locators << Plugin::FileSystemLocator
end
def default_plugin_loader
20 railties/lib/rails/plugin.rb
View
@@ -97,4 +97,24 @@ def evaluate_init_rb(initializer)
end
end
end
+
+ # This Plugin subclass represents a Gem plugin. It behaves exactly like a
+ # "traditional" Rails plugin, but doesn't expose any additional load paths,
+ # since RubyGems has already taken care of things.
+ class GemPlugin < Plugin
+
+ # Initialize this plugin from a Gem::Specification.
+ def initialize(spec)
+ super(File.join(spec.full_gem_path, "rails"))
+ @name = spec.name
+ end
+
+ def valid?
+ true
+ end
+
+ def load_paths
+ []
+ end
+ end
end
21 railties/lib/rails/plugin/locator.rb
View
@@ -72,7 +72,26 @@ def locate_plugins_under(base_path)
plugins
end
end
-
+ end
+
+ # The GemLocator scans all the loaded RubyGems, looking for gems with
+ # a <tt>rails/init.rb</tt> file.
+ class GemLocator < Locator
+ def plugins
+ specs = Gem.loaded_specs.values.select do |spec|
+ spec.loaded_from && # prune stubs
+ File.exist?(File.join(spec.full_gem_path, "rails", "init.rb"))
+ end
+
+ require "rubygems/dependency_list"
+
+ deps = Gem::DependencyList.new
+ deps.add(*specs)
+
+ deps.dependency_order.collect do |spec|
+ Rails::GemPlugin.new(spec)
+ end
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.