Commit afdae5a97e1696823ba24b76826dada70bc45274

Authored by Braulio Bhavamitra
1 parent ef453b43

plugins: reload extensions before request when needed

Use ActionDispatch::Reloader.to_prepare instead of
Rails.configuration.to_prepare. The latter runs the block only once and
is not adequate for code that are reloaded upon files changes on each
request. As extensions may touch on helpers and controllers, the former
suits well.

Besides, filters must be loaded after all plugins' extensions, as controllers
and helpers needs the "final" code to execute properly.

The production behaviour don't change as the code is not reloaded.
lib/noosfero/plugin.rb
@@ -24,11 +24,17 @@ class Noosfero::Plugin @@ -24,11 +24,17 @@ class Noosfero::Plugin
24 24
25 def initialize! 25 def initialize!
26 return if !should_load 26 return if !should_load
27 - available_plugins.each do |plugin_dir| 27 +
  28 + klasses = available_plugins.map do |plugin_dir|
28 plugin_name = File.basename(plugin_dir) 29 plugin_name = File.basename(plugin_dir)
29 - plugin = load_plugin(plugin_name)  
30 - load_plugin_extensions(plugin_dir)  
31 - load_plugin_filters(plugin) 30 + load_plugin plugin_name
  31 + end
  32 + available_plugins.each do |plugin_dir|
  33 + load_plugin_extensions plugin_dir
  34 + end
  35 + # filters must be loaded after all extensions
  36 + klasses.each do |plugin|
  37 + load_plugin_filters plugin
32 end 38 end
33 end 39 end
34 40
@@ -88,7 +94,7 @@ class Noosfero::Plugin @@ -88,7 +94,7 @@ class Noosfero::Plugin
88 # This is a generic method that initialize any possible filter defined by a 94 # This is a generic method that initialize any possible filter defined by a
89 # plugin to a specific controller 95 # plugin to a specific controller
90 def load_plugin_filters(plugin) 96 def load_plugin_filters(plugin)
91 - Rails.configuration.to_prepare do 97 + ActionDispatch::Reloader.to_prepare do
92 filters = plugin.new.send 'application_controller_filters' rescue [] 98 filters = plugin.new.send 'application_controller_filters' rescue []
93 Noosfero::Plugin.add_controller_filters ApplicationController, plugin, filters 99 Noosfero::Plugin.add_controller_filters ApplicationController, plugin, filters
94 100
@@ -116,7 +122,7 @@ class Noosfero::Plugin @@ -116,7 +122,7 @@ class Noosfero::Plugin
116 end 122 end
117 123
118 def load_plugin_extensions(dir) 124 def load_plugin_extensions(dir)
119 - Rails.configuration.to_prepare do 125 + ActionDispatch::Reloader.to_prepare do
120 Dir[File.join(dir, 'lib', 'ext', '*.rb')].each {|file| require_dependency file } 126 Dir[File.join(dir, 'lib', 'ext', '*.rb')].each {|file| require_dependency file }
121 end 127 end
122 end 128 end
@@ -422,7 +428,7 @@ class Noosfero::Plugin @@ -422,7 +428,7 @@ class Noosfero::Plugin
422 def upload_files_extra_fields(article) 428 def upload_files_extra_fields(article)
423 nil 429 nil
424 end 430 end
425 - 431 +
426 # -> Adds fields to the signup form 432 # -> Adds fields to the signup form
427 # returns = proc that creates html code 433 # returns = proc that creates html code
428 def signup_extra_contents 434 def signup_extra_contents
lib/noosfero/plugin/spammable.rb
1 -Rails.configuration.to_prepare do 1 +ActionDispatch::Reloader.to_prepare do
2 Spammable.module_eval do 2 Spammable.module_eval do
3 def marked_as_spam 3 def marked_as_spam
4 plugins.dispatch(:marked_as_spam, self) 4 plugins.dispatch(:marked_as_spam, self)