diff --git a/lib/noosfero/plugin.rb b/lib/noosfero/plugin.rb index 765c0c8..4d33221 100644 --- a/lib/noosfero/plugin.rb +++ b/lib/noosfero/plugin.rb @@ -1,4 +1,5 @@ require_dependency 'noosfero' +require 'noosfero/plugin/parent_methods' class Noosfero::Plugin @@ -14,13 +15,9 @@ class Noosfero::Plugin class << self - attr_writer :should_load + include Noosfero::Plugin::ParentMethods - # Called for each ActiveRecord class with parents - # See http://apidock.com/rails/ActiveRecord/ModelSchema/ClassMethods/full_table_name_prefix - def table_name_prefix - @table_name_prefix ||= "#{name.to_s.underscore}_" - end + attr_writer :should_load def should_load @should_load.nil? && true || @boot @@ -92,8 +89,14 @@ class Noosfero::Plugin end end - def load_plugin(plugin_name) - (plugin_name.to_s.camelize + 'Plugin').constantize + def load_plugin_identifier identifier + klass = identifier.to_s.camelize.constantize + klass = klass.const_get :Base if klass.class == Module + klass + end + + def load_plugin public_name + load_plugin_identifier "#{public_name.to_s.camelize}Plugin" end # This is a generic method that initialize any possible filter defined by a @@ -135,7 +138,7 @@ class Noosfero::Plugin filters = [filters] end filters.each do |plugin_filter| - filter_method = (plugin.name.underscore.gsub('/','_') + '_' + plugin_filter[:method_name]).to_sym + filter_method = "#{plugin.identifier}_#{plugin_filter[:method_name]}".to_sym controller_class.send(plugin_filter[:type], filter_method, (plugin_filter[:options] || {})) controller_class.send(:define_method, filter_method) do instance_exec(&plugin_filter[:block]) if environment.plugin_enabled?(plugin) @@ -168,38 +171,6 @@ class Noosfero::Plugin @all ||= available_plugins.map{ |dir| (File.basename(dir) + "_plugin").camelize } end - def public_name - self.name.underscore.gsub('_plugin','') - end - - def public_path file = '', relative=false - File.join "#{if relative then '' else '/' end}plugins", public_name, file - end - - def root_path - Rails.root.join('plugins', public_name) - end - - def view_path - File.join(root_path,'views') - end - - # Here the developer should specify the meta-informations that the plugin can - # inform. - def plugin_name - self.name.underscore.humanize - end - def plugin_description - _("No description informed.") - end - - def admin_url - {:controller => "#{name.underscore}_admin", :action => 'index'} - end - - def has_admin_url? - File.exists?(File.join(root_path, 'controllers', "#{name.underscore}_admin_controller.rb")) - end end def expanded_template(file_path, locals = {}) diff --git a/lib/noosfero/plugin/manager.rb b/lib/noosfero/plugin/manager.rb index 322818d..673807e 100644 --- a/lib/noosfero/plugin/manager.rb +++ b/lib/noosfero/plugin/manager.rb @@ -76,7 +76,7 @@ class Noosfero::Plugin::Manager def enabled_plugins @enabled_plugins ||= (Noosfero::Plugin.all & environment.enabled_plugins).map do |plugin| - plugin.constantize.new(context) + Noosfero::Plugin.load_plugin_identifier(plugin).new context end end diff --git a/lib/noosfero/plugin/parent_methods.rb b/lib/noosfero/plugin/parent_methods.rb new file mode 100644 index 0000000..90f9f06 --- /dev/null +++ b/lib/noosfero/plugin/parent_methods.rb @@ -0,0 +1,62 @@ +class Noosfero::Plugin + + # Plugins that are defined as modules should extend + # this module manually, for example: + # module MyPlugin + # extend Noosfero::Plugin::ParentMethods + # end + module ParentMethods + + def identifier + @identifier ||= (if self.parents.first.instance_of? Module then self.parents.first else self end).name.underscore + end + + def public_name + @public_name ||= self.identifier.gsub '_plugin', '' + end + + # Here the developer should specify the meta-informations that the plugin can + # inform. + def plugin_name + self.identifier.humanize + end + def plugin_description + _("No description informed.") + end + + # Called for each ActiveRecord model with parents + # See http://apidock.com/rails/ActiveRecord/ModelSchema/ClassMethods/full_table_name_prefix + def table_name_prefix + @table_name_prefix ||= "#{self.identifier}_" + end + + def public_path file = '', relative=false + File.join "#{if relative then '' else '/' end}plugins", public_name, file + end + + def root_path + Rails.root.join('plugins', public_name) + end + + def view_path + File.join(root_path,'views') + end + + def admin_url + {:controller => "#{self.identifier}_admin", :action => 'index'} + end + + def has_admin_url? + File.exists?(File.join(root_path, 'controllers', "#{self.identifier}_admin_controller.rb")) + end + + def controllers + @controllers ||= Dir.glob("#{self.root_path}/controllers/*/*").map do |controller_file| + next unless controller_file =~ /_controller.rb$/ + controller = File.basename(controller_file).gsub(/.rb$/, '').camelize + end.compact + end + + end + +end diff --git a/test/unit/plugin_test.rb b/test/unit/plugin_test.rb index ecf08ea..a7337a3 100644 --- a/test/unit/plugin_test.rb +++ b/test/unit/plugin_test.rb @@ -23,7 +23,7 @@ class PluginTest < ActiveSupport::TestCase end should 'returns empty hash for class method extra_blocks by default if no blocks are defined on plugin' do - + class SomePlugin1 < Noosfero::Plugin end -- libgit2 0.21.2