Commit 377749a2a90663ee09f9bd303fb56191b9a7bb7a

Authored by AntonioTerceiro
1 parent 93c6a41f

ActionItem65: avoiding the need to add controllers manually to config/routes.rb



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@490 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/helpers/application_helper.rb
@@ -81,7 +81,7 @@ module ApplicationHelper @@ -81,7 +81,7 @@ module ApplicationHelper
81 # TODO: remove the absolute path 81 # TODO: remove the absolute path
82 def link_to_cms(text, profile = nil, options = {}) 82 def link_to_cms(text, profile = nil, options = {})
83 profile ||= current_user.login 83 profile ||= current_user.login
84 - link_to text, cms_path(:profile => profile), options 84 + link_to text, profile_path(:controller => 'cms', :profile => profile), options
85 end 85 end
86 86
87 def link_to_profile(text, profile = nil, options = {}) 87 def link_to_profile(text, profile = nil, options = {})
config/routes.rb
  1 +require 'project_meta'
  2 +
1 ActionController::Routing::Routes.draw do |map| 3 ActionController::Routing::Routes.draw do |map|
2 # The priority is based upon order of creation: first created -> highest priority. 4 # The priority is based upon order of creation: first created -> highest priority.
3 5
@@ -24,23 +26,22 @@ ActionController::Routing::Routes.draw do |map| @@ -24,23 +26,22 @@ ActionController::Routing::Routes.draw do |map|
24 ###################################################### 26 ######################################################
25 # profile customization - "My profile" 27 # profile customization - "My profile"
26 map.myprofile 'myprofile/:profile', :controller => 'profile_editor', :action => 'index' 28 map.myprofile 'myprofile/:profile', :controller => 'profile_editor', :action => 'index'
27 - map.myprofile 'myprofile/:profile/:controller/:action/:id', :controller => /(enterprise|profile_editor)/ 29 + map.myprofile 'myprofile/:profile/:controller/:action/:id', :controller => Noosfero.pattern_for_controllers_in_directory('profile_admin')
28 # content administration 30 # content administration
29 - map.cms 'cms/:profile/:action/:id', :controller => 'cms'  
30 31
31 ###################################################### 32 ######################################################
32 ## Controllers that are used by environment admin 33 ## Controllers that are used by environment admin
33 ###################################################### 34 ######################################################
34 # administrative tasks for a virtual community 35 # administrative tasks for a virtual community
35 map.admin 'admin', :controller => 'admin_panel' 36 map.admin 'admin', :controller => 'admin_panel'
36 - map.admin 'admin/:controller/:action/:id', :controller => /(admin_panel|features|manage_tags|edit_template|role)/ 37 + map.admin 'admin/:controller/:action/:id', :controller => Noosfero.pattern_for_controllers_in_directory('environment_admin')
37 38
38 ###################################################### 39 ######################################################
39 ## Controllers that are used by system admin 40 ## Controllers that are used by system admin
40 ###################################################### 41 ######################################################
41 # administrative tasks for a virtual community 42 # administrative tasks for a virtual community
42 map.system 'system', :controller => 'system' 43 map.system 'system', :controller => 'system'
43 - map.system 'system/:controller/:action/:id', :controller => // 44 + map.system 'system/:controller/:action/:id', :controller => Noosfero.pattern_for_controllers_in_directory('system_admin')
44 45
45 46
46 ###################################################### 47 ######################################################
lib/project_meta.rb
@@ -2,4 +2,16 @@ module Noosfero @@ -2,4 +2,16 @@ module Noosfero
2 PROJECT = 'noosfero' 2 PROJECT = 'noosfero'
3 VERSION = '0.2.0~alpha' 3 VERSION = '0.2.0~alpha'
4 SVN_ROOT = 'https://svn.colivre.coop.br/svn/noosfero' 4 SVN_ROOT = 'https://svn.colivre.coop.br/svn/noosfero'
  5 +
  6 + def self.controllers_in_directory(dir)
  7 + Dir.glob(File.join(RAILS_ROOT, 'app', 'controllers', dir, '*_controller.rb')).map do |item|
  8 + item.gsub(/^.*\/([^\/]+)_controller.rb$/, '\1')
  9 + end
  10 + end
  11 +
  12 + def self.pattern_for_controllers_in_directory(dir)
  13 + disjunction = controllers_in_directory(dir).join('|')
  14 + pattern = disjunction.blank? ? '' : (('(' + disjunction + ')'))
  15 + Regexp.new(pattern)
  16 + end
5 end 17 end
test/integration/manage_documents_test.rb
@@ -9,14 +9,14 @@ class ManageDocumentsTest < ActionController::IntegrationTest @@ -9,14 +9,14 @@ class ManageDocumentsTest < ActionController::IntegrationTest
9 9
10 login('ze', 'test') 10 login('ze', 'test')
11 11
12 - get '/cms/ze' 12 + get '/myprofile/ze/cms'
13 assert_response :success 13 assert_response :success
14 14
15 - get '/cms/ze/new' 15 + get '/myprofile/ze/cms/new'
16 assert_response :success 16 assert_response :success
17 - assert_tag :tag => 'form', :attributes => { :action => '/cms/ze/new' } 17 + assert_tag :tag => 'form', :attributes => { :action => '/myprofile/ze/cms/new' }
18 18
19 - post '/cms/ze/new', :page => { :title => 'my new article', :body => 'this is the text of my new article' , :parent_id => Article.find_by_path('ze').id } 19 + post '/myprofile/ze/cms/new', :page => { :title => 'my new article', :body => 'this is the text of my new article' , :parent_id => Article.find_by_path('ze').id }
20 assert_response :redirect 20 assert_response :redirect
21 21
22 follow_redirect! 22 follow_redirect!
@@ -29,15 +29,15 @@ class ManageDocumentsTest < ActionController::IntegrationTest @@ -29,15 +29,15 @@ class ManageDocumentsTest < ActionController::IntegrationTest
29 def test_update_of_an_existing_article 29 def test_update_of_an_existing_article
30 login('ze', 'test') 30 login('ze', 'test')
31 31
32 - get '/cms/ze' 32 + get '/myprofile/ze/cms'
33 assert_response :success 33 assert_response :success
34 34
35 id = Comatose::Page.find_by_path('ze').id 35 id = Comatose::Page.find_by_path('ze').id
36 - get "cms/ze/edit/#{id}" 36 + get "myprofile/ze/cms/edit/#{id}"
37 assert_response :success 37 assert_response :success
38 - assert_tag :tag => 'form', :attributes => { :action => "/cms/ze/edit/#{id}" } 38 + assert_tag :tag => 'form', :attributes => { :action => "/myprofile/ze/cms/edit/#{id}" }
39 39
40 - post "cms/ze/edit/#{id}", :page => { :body => 'changed_body' } 40 + post "myprofile/ze/cms/edit/#{id}", :page => { :body => 'changed_body' }
41 assert_response :redirect 41 assert_response :redirect
42 42
43 end 43 end
@@ -46,10 +46,10 @@ class ManageDocumentsTest < ActionController::IntegrationTest @@ -46,10 +46,10 @@ class ManageDocumentsTest < ActionController::IntegrationTest
46 article = Article.create!(:title => 'to be removed', :body => 'go to hell', :parent_id => Article.find_by_path('ze').id) 46 article = Article.create!(:title => 'to be removed', :body => 'go to hell', :parent_id => Article.find_by_path('ze').id)
47 count = Article.count 47 count = Article.count
48 48
49 - get '/cms/ze' 49 + get '/myprofile/ze/cms'
50 assert_response :success 50 assert_response :success
51 51
52 - post "/cms/ze/delete/#{article.id}" 52 + post "/myprofile/ze/cms/delete/#{article.id}"
53 assert_response :redirect 53 assert_response :redirect
54 54
55 assert_raise ActiveRecord::RecordNotFound do 55 assert_raise ActiveRecord::RecordNotFound do
test/integration/routing_test.rb
@@ -22,7 +22,7 @@ class RoutingTest < ActionController::IntegrationTest @@ -22,7 +22,7 @@ class RoutingTest < ActionController::IntegrationTest
22 end 22 end
23 23
24 def test_comatose_admin 24 def test_comatose_admin
25 - assert_routing('/cms/ze', :profile => 'ze', :controller => 'cms', :action => 'index') 25 + assert_routing('/myprofile/ze/cms', :profile => 'ze', :controller => 'cms', :action => 'index')
26 end 26 end
27 27
28 def test_edit_template 28 def test_edit_template