Commit 28165631c959a03085ff480ffcf74208e171f44c
1 parent
c973fa96
Exists in
master
and in
29 other branches
rails3: fix plugin initialization
Showing
6 changed files
with
29 additions
and
22 deletions
Show diff stats
app/controllers/application_controller.rb
@@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base | @@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base | ||
5 | 5 | ||
6 | before_filter :setup_multitenancy | 6 | before_filter :setup_multitenancy |
7 | before_filter :detect_stuff_by_domain | 7 | before_filter :detect_stuff_by_domain |
8 | - before_filter :init_noosfero_plugins_controller_filters | 8 | + before_filter :init_noosfero_plugins |
9 | before_filter :allow_cross_domain_access | 9 | before_filter :allow_cross_domain_access |
10 | 10 | ||
11 | def allow_cross_domain_access | 11 | def allow_cross_domain_access |
@@ -132,23 +132,9 @@ class ApplicationController < ActionController::Base | @@ -132,23 +132,9 @@ class ApplicationController < ActionController::Base | ||
132 | 132 | ||
133 | include Noosfero::Plugin::HotSpot | 133 | include Noosfero::Plugin::HotSpot |
134 | 134 | ||
135 | - # This is a generic method that initialize any possible filter defined by a | ||
136 | - # plugin to the current controller being initialized. | ||
137 | - def init_noosfero_plugins_controller_filters | ||
138 | - plugins.each do |plugin| | ||
139 | - filters = plugin.send(self.class.name.underscore + '_filters') | ||
140 | - filters = [filters] if !filters.kind_of?(Array) | ||
141 | - controller_filters = self.class._process_action_callbacks.map {|c| c.filter.to_sym } | ||
142 | - filters.each do |plugin_filter| | ||
143 | - filter_method = (plugin.class.name.underscore.gsub('/','_') + '_' + plugin_filter[:method_name]).to_sym | ||
144 | - unless controller_filters.include?(filter_method) | ||
145 | - self.class.send(plugin_filter[:type], filter_method.to_sym, (plugin_filter[:options] || {})) | ||
146 | - self.class.send(:define_method, filter_method) do | ||
147 | - instance_eval(&plugin_filter[:block]) if environment.plugin_enabled?(plugin.class) | ||
148 | - end | ||
149 | - end | ||
150 | - end | ||
151 | - end | 135 | + # FIXME this filter just loads @plugins to children controllers and helpers |
136 | + def init_noosfero_plugins | ||
137 | + plugins | ||
152 | end | 138 | end |
153 | 139 | ||
154 | def render_not_found(path = nil) | 140 | def render_not_found(path = nil) |
config/environment.rb
@@ -39,4 +39,3 @@ if !['test', 'cucumber'].include?(ENV['RAILS_ENV']) | @@ -39,4 +39,3 @@ if !['test', 'cucumber'].include?(ENV['RAILS_ENV']) | ||
39 | end | 39 | end |
40 | 40 | ||
41 | Noosfero::Application.initialize! | 41 | Noosfero::Application.initialize! |
42 | -Noosfero::Plugin.initialize! |
config/initializers/plugins.rb
@@ -5,4 +5,4 @@ require 'noosfero/plugin/active_record' | @@ -5,4 +5,4 @@ require 'noosfero/plugin/active_record' | ||
5 | require 'noosfero/plugin/mailer_base' | 5 | require 'noosfero/plugin/mailer_base' |
6 | require 'noosfero/plugin/settings' | 6 | require 'noosfero/plugin/settings' |
7 | require 'noosfero/plugin/spammable' | 7 | require 'noosfero/plugin/spammable' |
8 | -Noosfero::Plugin.init_system if $NOOSFERO_LOAD_PLUGINS | 8 | +Noosfero::Plugin.initialize! |
lib/noosfero/plugin.rb
@@ -20,8 +20,9 @@ class Noosfero::Plugin | @@ -20,8 +20,9 @@ class Noosfero::Plugin | ||
20 | return if !should_load | 20 | return if !should_load |
21 | enabled.each do |plugin_dir| | 21 | enabled.each do |plugin_dir| |
22 | plugin_name = File.basename(plugin_dir) | 22 | plugin_name = File.basename(plugin_dir) |
23 | - load_plugin(plugin_name) | 23 | + plugin = load_plugin(plugin_name) |
24 | load_plugin_extensions(plugin_dir) | 24 | load_plugin_extensions(plugin_dir) |
25 | + load_plugin_filters(plugin) | ||
25 | end | 26 | end |
26 | end | 27 | end |
27 | 28 | ||
@@ -76,6 +77,25 @@ class Noosfero::Plugin | @@ -76,6 +77,25 @@ class Noosfero::Plugin | ||
76 | (plugin_name.to_s.camelize + 'Plugin').constantize | 77 | (plugin_name.to_s.camelize + 'Plugin').constantize |
77 | end | 78 | end |
78 | 79 | ||
80 | + # This is a generic method that initialize any possible filter defined by a | ||
81 | + # plugin to a specific controller | ||
82 | + def load_plugin_filters(plugin) | ||
83 | + plugin_methods = plugin.instance_methods.select {|m| m.to_s.end_with?('_filters')} | ||
84 | + plugin_methods.each do |plugin_method| | ||
85 | + controller_class = plugin_method.to_s.gsub('_filters', '').camelize.constantize | ||
86 | + filters = plugin.new.send(plugin_method) | ||
87 | + filters = [filters] if !filters.kind_of?(Array) | ||
88 | + | ||
89 | + filters.each do |plugin_filter| | ||
90 | + filter_method = (plugin.name.underscore.gsub('/','_') + '_' + plugin_filter[:method_name]).to_sym | ||
91 | + controller_class.send(plugin_filter[:type], filter_method, (plugin_filter[:options] || {})) | ||
92 | + controller_class.send(:define_method, filter_method) do | ||
93 | + instance_eval(&plugin_filter[:block]) if environment.plugin_enabled?(plugin) | ||
94 | + end | ||
95 | + end | ||
96 | + end | ||
97 | + end | ||
98 | + | ||
79 | def load_plugin_extensions(dir) | 99 | def load_plugin_extensions(dir) |
80 | Rails.configuration.to_prepare do | 100 | Rails.configuration.to_prepare do |
81 | Dir[File.join(dir, 'lib', 'ext', '*.rb')].each {|file| require_dependency file } | 101 | Dir[File.join(dir, 'lib', 'ext', '*.rb')].each {|file| require_dependency file } |
lib/tasks/plugins.rake
@@ -5,7 +5,7 @@ class ActiveRecord::Migrator | @@ -5,7 +5,7 @@ class ActiveRecord::Migrator | ||
5 | alias_method :orig_initialize, :initialize | 5 | alias_method :orig_initialize, :initialize |
6 | def initialize *args | 6 | def initialize *args |
7 | orig_initialize *args | 7 | orig_initialize *args |
8 | - @migrations_path = "{db/migrate,config/plugins/*/db/migrate}" | 8 | + @migrations_paths = ["db/migrate", "config/plugins/*/db/migrate"] |
9 | end | 9 | end |
10 | end | 10 | end |
11 | 11 |
test/functional/application_controller_test.rb
@@ -507,6 +507,7 @@ class ApplicationControllerTest < ActionController::TestCase | @@ -507,6 +507,7 @@ class ApplicationControllerTest < ActionController::TestCase | ||
507 | end | 507 | end |
508 | end | 508 | end |
509 | 509 | ||
510 | + Noosfero::Plugin.load_plugin_filters(FilterPlugin) | ||
510 | Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([FilterPlugin.new]) | 511 | Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([FilterPlugin.new]) |
511 | 512 | ||
512 | get :index | 513 | get :index |
@@ -525,6 +526,7 @@ class ApplicationControllerTest < ActionController::TestCase | @@ -525,6 +526,7 @@ class ApplicationControllerTest < ActionController::TestCase | ||
525 | end | 526 | end |
526 | end | 527 | end |
527 | 528 | ||
529 | + Noosfero::Plugin.load_plugin_filters(OtherFilterPlugin) | ||
528 | environment1 = fast_create(Environment, :name => 'test environment') | 530 | environment1 = fast_create(Environment, :name => 'test environment') |
529 | environment1.enable_plugin(OtherFilterPlugin.name) | 531 | environment1.enable_plugin(OtherFilterPlugin.name) |
530 | environment2 = fast_create(Environment, :name => 'other test environment') | 532 | environment2 = fast_create(Environment, :name => 'other test environment') |