Commit 4efa9d5adb9a91e43e0a6ca5fc2b0a005428ff87

Authored by Rodrigo Souto
1 parent b5d67b0a

Generic routes for plugins

lib/noosfero/plugin.rb
... ... @@ -12,11 +12,20 @@ class Noosfero::Plugin
12 12 end
13 13  
14 14 def init_system
15   - Dir.glob(File.join(Rails.root, 'config', 'plugins', '*')).select do |entry|
  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')
  18 + end
  19 + enabled_plugins.select do |entry|
16 20 File.directory?(entry)
17 21 end.each do |dir|
18 22 Rails.configuration.controller_paths << File.join(dir, 'controllers')
19 23 ActiveSupport::Dependencies.load_paths << File.join(dir, 'controllers')
  24 + controllers_folders = %w[public profile myprofile admin]
  25 + controllers_folders.each do |folder|
  26 + Rails.configuration.controller_paths << File.join(dir, 'controllers', folder)
  27 + ActiveSupport::Dependencies.load_paths << File.join(dir, 'controllers', folder)
  28 + end
20 29 [ ActiveSupport::Dependencies.load_paths, $:].each do |path|
21 30 path << File.join(dir, 'models')
22 31 path << File.join(dir, 'lib')
... ...
lib/noosfero/plugin/routes.rb
1   -Dir.glob(File.join(Rails.root, 'config', 'plugins', '*', 'controllers')) do |dir|
2   - plugin_name = File.basename(File.dirname(dir))
  1 +plugins_root = Rails.env.test? ? 'plugins' : File.join('config', 'plugins')
  2 +
  3 +Dir.glob(File.join(Rails.root, plugins_root, '*', 'controllers')) do |controllers_dir|
  4 + prefixes_by_folder = {'public' => 'plugin',
  5 + 'profile' => 'profile/:profile/plugin',
  6 + 'myprofile' => 'myprofile/:profile/plugin',
  7 + 'admin' => 'admin/plugin'}
  8 +
  9 + controllers_by_folder = prefixes_by_folder.keys.inject({}) do |hash, folder|
  10 + hash.merge!({folder => Dir.glob(File.join(controllers_dir, folder, '*')).map {|full_names| File.basename(full_names).gsub(/_controller.rb$/,'')}})
  11 + end
  12 +
  13 + plugin_name = File.basename(File.dirname(controllers_dir))
  14 +
  15 + controllers_by_folder.each do |folder, controllers|
  16 + controllers.each do |controller|
  17 + controller_name = controller.gsub("#{plugin_name}_plugin_",'')
  18 + map.connect "#{prefixes_by_folder[folder]}/#{plugin_name}/#{controller_name}/:action/:id", :controller => controller
  19 + end
  20 + end
  21 +
3 22 map.connect 'plugin/' + plugin_name + '/:action/:id', :controller => plugin_name + '_plugin'
4 23 map.connect 'profile/:profile/plugins/' + plugin_name + '/:action/:id', :controller => plugin_name + '_plugin_profile'
5 24 map.connect 'myprofile/:profile/plugin/' + plugin_name + '/:action/:id', :controller => plugin_name + '_plugin_myprofile'
6 25 map.connect 'admin/plugin/' + plugin_name + '/:action/:id', :controller => plugin_name + '_plugin_admin'
7 26 end
8   -
... ...
test/integration/routing_test.rb
... ... @@ -221,4 +221,11 @@ class RoutingTest &lt; ActionController::IntegrationTest
221 221 assert_routing('/chat/avatar/chemical-brothers', :controller => 'chat', :action => 'avatar', :id => 'chemical-brothers')
222 222 end
223 223  
  224 + def test_plugins_generic_routes
  225 + assert_routing('/plugin/foo/public_bar/play/1', {:controller => 'foo_plugin_public_bar', :action => 'play', :id => '1'})
  226 + assert_routing('/profile/test/plugin/foo/profile_bar/play/1', {:controller => 'foo_plugin_profile_bar', :action => 'play', :id => '1', :profile => 'test'})
  227 + assert_routing('/myprofile/test/plugin/foo/myprofile_bar/play/1', {:controller => 'foo_plugin_myprofile_bar', :action => 'play', :id => '1', :profile => 'test'})
  228 + assert_routing('/admin/plugin/foo/admin_bar/play/1', {:controller => 'foo_plugin_admin_bar', :action => 'play', :id => '1'})
  229 + end
  230 +
224 231 end
... ...