Commit de9842bdbbfa59de3e0680b9b312ac1460b0af91

Authored by Antonio Terceiro
1 parent 55517151

Adapt Noosfero plugin system to Rails 3

I should probably rethink the Noosfero plugin system and make it use the Rails
engines infrastructure.
config/application.rb
@@ -14,6 +14,8 @@ if defined?(Bundler) @@ -14,6 +14,8 @@ if defined?(Bundler)
14 # Bundler.require(:default, :assets, Rails.env) 14 # Bundler.require(:default, :assets, Rails.env)
15 end 15 end
16 16
  17 +require 'noosfero/plugin'
  18 +
17 module Noosfero 19 module Noosfero
18 class Application < Rails::Application 20 class Application < Rails::Application
19 21
@@ -39,9 +41,8 @@ module Noosfero @@ -39,9 +41,8 @@ module Noosfero
39 makemo 41 makemo
40 ] 42 ]
41 if $PROGRAM_NAME =~ /rake$/ && (ignore_rake_commands.include?(ARGV.first)) 43 if $PROGRAM_NAME =~ /rake$/ && (ignore_rake_commands.include?(ARGV.first))
42 - $NOOSFERO_LOAD_PLUGINS = false 44 + Noosfero::Plugin.should_load = false
43 else 45 else
44 - $NOOSFERO_LOAD_PLUGINS = true  
45 config.active_record.observers = :article_sweeper, :role_assignment_sweeper, :friendship_sweeper, :category_sweeper, :block_sweeper 46 config.active_record.observers = :article_sweeper, :role_assignment_sweeper, :friendship_sweeper, :category_sweeper, :block_sweeper
46 end 47 end
47 48
@@ -104,5 +105,7 @@ module Noosfero @@ -104,5 +105,7 @@ module Noosfero
104 105
105 config.autoload_paths += Dir["#{config.root}/app/controllers/**/"] 106 config.autoload_paths += Dir["#{config.root}/app/controllers/**/"]
106 107
  108 + Noosfero::Plugin.setup(config)
  109 +
107 end 110 end
108 end 111 end
config/environment.rb
@@ -39,3 +39,4 @@ if ![&#39;test&#39;, &#39;cucumber&#39;].include?(ENV[&#39;RAILS_ENV&#39;]) @@ -39,3 +39,4 @@ if ![&#39;test&#39;, &#39;cucumber&#39;].include?(ENV[&#39;RAILS_ENV&#39;])
39 end 39 end
40 40
41 Noosfero::Application.initialize! 41 Noosfero::Application.initialize!
  42 +Noosfero::Plugin.initialize!
lib/noosfero/plugin.rb
1 require 'noosfero' 1 require 'noosfero'
2 -include ActionView::Helpers::AssetTagHelper  
3 2
4 class Noosfero::Plugin 3 class Noosfero::Plugin
5 4
@@ -7,47 +6,73 @@ class Noosfero::Plugin @@ -7,47 +6,73 @@ class Noosfero::Plugin
7 6
8 class << self 7 class << self
9 8
10 - def klass(dir)  
11 - (dir.to_s.camelize + 'Plugin').constantize # load the plugin 9 + attr_writer :should_load
  10 +
  11 + def should_load
  12 + @should_load.nil? && true || @boot
12 end 13 end
13 14
14 - def init_system  
15 - enabled_plugins = Dir.glob(File.join(Rails.root, 'config', 'plugins', '*'))  
16 - if Rails.env.test? && !enabled_plugins.include?(File.join(Rails.root, 'config', 'plugins', 'foo'))  
17 - enabled_plugins << File.join(Rails.root, 'plugins', 'foo') 15 + def initialize!
  16 + return if !should_load
  17 + enabled.each do |plugin_dir|
  18 + plugin_name = File.basename(plugin_dir)
  19 + load_plugin(plugin_name)
18 end 20 end
19 - enabled_plugins.select do |entry|  
20 - File.directory?(entry)  
21 - end.each do |dir|  
22 - plugin_name = File.basename(dir)  
23 -  
24 - plugin_dependencies_ok = true  
25 - plugin_dependencies_file = File.join(dir, 'dependencies.rb')  
26 - if File.exists?(plugin_dependencies_file)  
27 - begin  
28 - require plugin_dependencies_file  
29 - rescue LoadError => ex  
30 - plugin_dependencies_ok = false  
31 - $stderr.puts "W: Noosfero plugin #{plugin_name} failed to load (#{ex})"  
32 - end 21 + end
  22 +
  23 + def setup(config)
  24 + return if !should_load
  25 + enabled.each do |dir|
  26 + setup_plugin(dir, config)
  27 + end
  28 + end
  29 +
  30 + def setup_plugin(dir, config)
  31 + plugin_name = File.basename(dir)
  32 +
  33 + plugin_dependencies_ok = true
  34 + plugin_dependencies_file = File.join(dir, 'dependencies.rb')
  35 + if File.exists?(plugin_dependencies_file)
  36 + begin
  37 + require plugin_dependencies_file
  38 + rescue LoadError => ex
  39 + plugin_dependencies_ok = false
  40 + $stderr.puts "W: Noosfero plugin #{plugin_name}: failed to load dependencies (#{ex})"
  41 + end
  42 + end
  43 +
  44 + if plugin_dependencies_ok
  45 + %w[
  46 + controllers
  47 + controllers/public
  48 + controllers/profile
  49 + controllers/myprofile
  50 + controllers/admin
  51 + ].each do |folder|
  52 + config.autoload_paths << File.join(dir, folder)
  53 + end
  54 + [ config.autoload_paths, $:].each do |path|
  55 + path << File.join(dir, 'models')
  56 + path << File.join(dir, 'lib')
33 end 57 end
  58 + end
  59 + end
34 60
35 - if plugin_dependencies_ok  
36 - Rails.configuration.controller_paths << File.join(dir, 'controllers')  
37 - ActiveSupport::Dependencies.load_paths << File.join(dir, 'controllers')  
38 - controllers_folders = %w[public profile myprofile admin]  
39 - controllers_folders.each do |folder|  
40 - Rails.configuration.controller_paths << File.join(dir, 'controllers', folder)  
41 - ActiveSupport::Dependencies.load_paths << File.join(dir, 'controllers', folder) 61 + def load_plugin(dir)
  62 + (dir.to_s.camelize + 'Plugin').constantize
  63 + end
  64 +
  65 + def enabled
  66 + @enabled ||=
  67 + begin
  68 + plugins = Dir.glob(File.join(Rails.root, 'config', 'plugins', '*'))
  69 + if Rails.env.test? && !plugins.include?(File.join(Rails.root, 'config', 'plugins', 'foo'))
  70 + plugins << File.join(Rails.root, 'plugins', 'foo')
42 end 71 end
43 - [ ActiveSupport::Dependencies.load_paths, $:].each do |path|  
44 - path << File.join(dir, 'models')  
45 - path << File.join(dir, 'lib') 72 + plugins.select do |entry|
  73 + File.directory?(entry)
46 end 74 end
47 -  
48 - klass(plugin_name)  
49 end 75 end
50 - end  
51 end 76 end
52 77
53 def all 78 def all
@@ -390,3 +415,9 @@ class Noosfero::Plugin @@ -390,3 +415,9 @@ class Noosfero::Plugin
390 end 415 end
391 416
392 end 417 end
  418 +
  419 +require 'noosfero/plugin/hot_spot'
  420 +require 'noosfero/plugin/manager'
  421 +require 'noosfero/plugin/active_record'
  422 +require 'noosfero/plugin/mailer_base'
  423 +require 'noosfero/plugin/settings'