Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added the :all option to config.plugins thatll include the rest of th…
…e plugins not already explicitly named (closes #9613) [fcheung]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7531 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Sep 21, 2007
1 parent 83c17f2 commit 6dd10d8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 9 deletions.
7 changes: 7 additions & 0 deletions railties/CHANGELOG
@@ -1,5 +1,12 @@
*SVN*

* Added the :all option to config.plugins that'll include the rest of the plugins not already explicitly named #9613 [fcheung]. Example:

# Loads :classic_pagination before all the other plugins
config.plugins = [ :classic_pagination, :all ]

* Removed deprecated task names, like clear_logs, in favor of the new namespaced style [DHH]

* Support multiple config.after_initialize blocks so plugins and apps can more easily cooperate. #9582 [zdennis]

* Added db:drop:all to drop all databases declared in config/database.yml [DHH]
Expand Down
5 changes: 3 additions & 2 deletions railties/environments/environment.rb
Expand Up @@ -16,8 +16,9 @@
# Skip frameworks you're not going to use (only works if using vendor/rails)
# config.frameworks -= [ :active_resource, :action_mailer ]

# Only load the plugins named here, by default all plugins in vendor/plugins are loaded
# config.plugins = %W( exception_notification ssl_requirement )
# Only load the plugins named here, in the order given. By default all plugins in vendor/plugins are loaded, in alphabetical order
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]

# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{RAILS_ROOT}/extras )
Expand Down
7 changes: 5 additions & 2 deletions railties/lib/initializer.rb
Expand Up @@ -177,6 +177,9 @@ def add_support_load_paths
# If an array of plugin names is specified in config.plugins, only those plugins will be loaded
# and they plugins will be loaded in that order. Otherwise, plugins are loaded in alphabetical
# order.
#
# if config.plugins ends contains :all then the named plugins will be loaded in the given order and all other
# plugins will be loaded in alphabetical order
def load_plugins
configuration.plugin_locators.each do |locator|
locator.new(self).each do |plugin|
Expand Down Expand Up @@ -339,8 +342,8 @@ def load_application_initializers
private
def ensure_all_registered_plugins_are_loaded!
unless configuration.plugins.nil?
unless loaded_plugins == configuration.plugins
missing_plugins = configuration.plugins - loaded_plugins
if configuration.plugins.detect {|plugin| plugin != :all && !loaded_plugins.include?( plugin)}
missing_plugins = configuration.plugins - (loaded_plugins + [:all])
raise LoadError, "Could not locate the following plugins: #{missing_plugins.to_sentence}"
end
end
Expand Down
33 changes: 28 additions & 5 deletions railties/lib/rails/plugin/loader.rb
Expand Up @@ -36,8 +36,16 @@ def plugin_path?
def enabled?
!explicit_plugin_loading_order? || registered?
end


def explicitly_enabled?
!explicit_plugin_loading_order? || explicitly_registered?
end

def registered?
explicit_plugin_loading_order? && registered_plugins_names_plugin?(name)
end

def explicitly_registered?
explicit_plugin_loading_order? && registered_plugins.include?(name)
end

Expand All @@ -54,6 +62,10 @@ def registered_plugins
config.plugins
end

def registered_plugins_names_plugin?(plugin_name)
registered_plugins.include?(plugin_name) || registered_plugins.include?(:all)
end

def explicit_plugin_loading_order?
!registered_plugins.nil?
end
Expand Down Expand Up @@ -104,20 +116,31 @@ def register_plugin_as_loaded

# Evaluate in init.rb
def evaluate
silence_warnings { eval(IO.read(init_path), binding, init_path)} if has_init_file?
silence_warnings { eval(IO.read(init_path), binding, init_path) } if has_init_file?
end

def <=>(other_plugin_loader)
if explicit_plugin_loading_order?
if non_existent_plugin = [self, other_plugin_loader].detect {|plugin| !registered_plugins.include?(plugin.name)}
if non_existent_plugin = [self, other_plugin_loader].detect { |plugin| !registered_plugins_names_plugin?(plugin.name) }
plugin_does_not_exist!(non_existent_plugin.name)
end

registered_plugins.index(name) <=> registered_plugins.index(other_plugin_loader.name)
if !explicitly_enabled? && !other_plugin_loader.explicitly_enabled?
name <=> other_plugin_loader.name
elsif registered_plugins.include?(:all) && (!explicitly_enabled? || !other_plugin_loader.explicitly_enabled?)
effective_index = explicitly_enabled? ? registered_plugins.index(name) : registered_plugins.index(:all)
other_effective_index = other_plugin_loader.explicitly_enabled? ?
registered_plugins.index(other_plugin_loader.name) : registered_plugins.index(:all)

effective_index <=> other_effective_index
else
registered_plugins.index(name) <=> registered_plugins.index(other_plugin_loader.name)
end

else
name <=> other_plugin_loader.name
end
end
end
end
end
end
12 changes: 12 additions & 0 deletions railties/test/plugin_loader_test.rb
Expand Up @@ -14,6 +14,18 @@ def test_determining_if_the_plugin_order_has_been_explicitly_set
assert loader.send(:explicit_plugin_loading_order?)
end

def test_enabled_if_not_named_explicitly
stubby_loader = loader_for(@valid_plugin_path)
acts_as_loader = loader_for('acts_as/acts_as_chunky_bacon')

only_load_the_following_plugins! ['stubby', :all]
assert stubby_loader.send(:enabled?)
assert acts_as_loader.send(:enabled?)

assert stubby_loader.send(:explicitly_enabled?)
assert !acts_as_loader.send(:explicitly_enabled?)
end

def test_determining_whether_a_given_plugin_is_loaded
plugin_loader = loader_for(@valid_plugin_path)
assert !plugin_loader.loaded?
Expand Down
14 changes: 14 additions & 0 deletions railties/test/plugin_locator_test.rb
Expand Up @@ -26,6 +26,20 @@ def test_all_plugins_are_loaded_when_registered_plugin_list_is_untouched
assert_equal %w(a acts_as_chunky_bacon plugin_with_no_lib_dir stubby), @locator.plugin_names, failure_tip
end

def test_all_plugins_loaded_when_all_is_used
plugin_names = ['stubby', 'acts_as_chunky_bacon', :all]
only_load_the_following_plugins! plugin_names
failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
assert_equal %w(stubby acts_as_chunky_bacon a plugin_with_no_lib_dir), @locator.plugin_names, failure_tip
end

def test_all_plugins_loaded_after_all
plugin_names = ['stubby', :all, 'acts_as_chunky_bacon']
only_load_the_following_plugins! plugin_names
failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
assert_equal %w(stubby a plugin_with_no_lib_dir acts_as_chunky_bacon ), @locator.plugin_names, failure_tip
end


def test_registering_a_plugin_name_that_does_not_exist_raises_a_load_error
only_load_the_following_plugins! %w(stubby acts_as_a_non_existant_plugin)
Expand Down

0 comments on commit 6dd10d8

Please sign in to comment.