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 | 5 | |
6 | 6 | before_filter :setup_multitenancy |
7 | 7 | before_filter :detect_stuff_by_domain |
8 | - before_filter :init_noosfero_plugins_controller_filters | |
8 | + before_filter :init_noosfero_plugins | |
9 | 9 | before_filter :allow_cross_domain_access |
10 | 10 | |
11 | 11 | def allow_cross_domain_access |
... | ... | @@ -132,23 +132,9 @@ class ApplicationController < ActionController::Base |
132 | 132 | |
133 | 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 | 138 | end |
153 | 139 | |
154 | 140 | def render_not_found(path = nil) | ... | ... |
config/environment.rb
config/initializers/plugins.rb
lib/noosfero/plugin.rb
... | ... | @@ -20,8 +20,9 @@ class Noosfero::Plugin |
20 | 20 | return if !should_load |
21 | 21 | enabled.each do |plugin_dir| |
22 | 22 | plugin_name = File.basename(plugin_dir) |
23 | - load_plugin(plugin_name) | |
23 | + plugin = load_plugin(plugin_name) | |
24 | 24 | load_plugin_extensions(plugin_dir) |
25 | + load_plugin_filters(plugin) | |
25 | 26 | end |
26 | 27 | end |
27 | 28 | |
... | ... | @@ -76,6 +77,25 @@ class Noosfero::Plugin |
76 | 77 | (plugin_name.to_s.camelize + 'Plugin').constantize |
77 | 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 | 99 | def load_plugin_extensions(dir) |
80 | 100 | Rails.configuration.to_prepare do |
81 | 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 | 5 | alias_method :orig_initialize, :initialize |
6 | 6 | def initialize *args |
7 | 7 | orig_initialize *args |
8 | - @migrations_path = "{db/migrate,config/plugins/*/db/migrate}" | |
8 | + @migrations_paths = ["db/migrate", "config/plugins/*/db/migrate"] | |
9 | 9 | end |
10 | 10 | end |
11 | 11 | ... | ... |
test/functional/application_controller_test.rb
... | ... | @@ -507,6 +507,7 @@ class ApplicationControllerTest < ActionController::TestCase |
507 | 507 | end |
508 | 508 | end |
509 | 509 | |
510 | + Noosfero::Plugin.load_plugin_filters(FilterPlugin) | |
510 | 511 | Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([FilterPlugin.new]) |
511 | 512 | |
512 | 513 | get :index |
... | ... | @@ -525,6 +526,7 @@ class ApplicationControllerTest < ActionController::TestCase |
525 | 526 | end |
526 | 527 | end |
527 | 528 | |
529 | + Noosfero::Plugin.load_plugin_filters(OtherFilterPlugin) | |
528 | 530 | environment1 = fast_create(Environment, :name => 'test environment') |
529 | 531 | environment1.enable_plugin(OtherFilterPlugin.name) |
530 | 532 | environment2 = fast_create(Environment, :name => 'other test environment') | ... | ... |