From 377749a2a90663ee09f9bd303fb56191b9a7bb7a Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Sat, 15 Sep 2007 19:52:48 +0000 Subject: [PATCH] ActionItem65: avoiding the need to add controllers manually to config/routes.rb --- app/helpers/application_helper.rb | 2 +- config/routes.rb | 9 +++++---- lib/project_meta.rb | 12 ++++++++++++ test/integration/manage_documents_test.rb | 20 ++++++++++---------- test/integration/routing_test.rb | 2 +- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0a92b9b..f121639 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -81,7 +81,7 @@ module ApplicationHelper # TODO: remove the absolute path def link_to_cms(text, profile = nil, options = {}) profile ||= current_user.login - link_to text, cms_path(:profile => profile), options + link_to text, profile_path(:controller => 'cms', :profile => profile), options end def link_to_profile(text, profile = nil, options = {}) diff --git a/config/routes.rb b/config/routes.rb index 5769f87..0c23806 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,5 @@ +require 'project_meta' + ActionController::Routing::Routes.draw do |map| # The priority is based upon order of creation: first created -> highest priority. @@ -24,23 +26,22 @@ ActionController::Routing::Routes.draw do |map| ###################################################### # profile customization - "My profile" map.myprofile 'myprofile/:profile', :controller => 'profile_editor', :action => 'index' - map.myprofile 'myprofile/:profile/:controller/:action/:id', :controller => /(enterprise|profile_editor)/ + map.myprofile 'myprofile/:profile/:controller/:action/:id', :controller => Noosfero.pattern_for_controllers_in_directory('profile_admin') # content administration - map.cms 'cms/:profile/:action/:id', :controller => 'cms' ###################################################### ## Controllers that are used by environment admin ###################################################### # administrative tasks for a virtual community map.admin 'admin', :controller => 'admin_panel' - map.admin 'admin/:controller/:action/:id', :controller => /(admin_panel|features|manage_tags|edit_template|role)/ + map.admin 'admin/:controller/:action/:id', :controller => Noosfero.pattern_for_controllers_in_directory('environment_admin') ###################################################### ## Controllers that are used by system admin ###################################################### # administrative tasks for a virtual community map.system 'system', :controller => 'system' - map.system 'system/:controller/:action/:id', :controller => // + map.system 'system/:controller/:action/:id', :controller => Noosfero.pattern_for_controllers_in_directory('system_admin') ###################################################### diff --git a/lib/project_meta.rb b/lib/project_meta.rb index f7e95d9..68a38e5 100644 --- a/lib/project_meta.rb +++ b/lib/project_meta.rb @@ -2,4 +2,16 @@ module Noosfero PROJECT = 'noosfero' VERSION = '0.2.0~alpha' SVN_ROOT = 'https://svn.colivre.coop.br/svn/noosfero' + + def self.controllers_in_directory(dir) + Dir.glob(File.join(RAILS_ROOT, 'app', 'controllers', dir, '*_controller.rb')).map do |item| + item.gsub(/^.*\/([^\/]+)_controller.rb$/, '\1') + end + end + + def self.pattern_for_controllers_in_directory(dir) + disjunction = controllers_in_directory(dir).join('|') + pattern = disjunction.blank? ? '' : (('(' + disjunction + ')')) + Regexp.new(pattern) + end end diff --git a/test/integration/manage_documents_test.rb b/test/integration/manage_documents_test.rb index 8b7ef32..ad3cd33 100644 --- a/test/integration/manage_documents_test.rb +++ b/test/integration/manage_documents_test.rb @@ -9,14 +9,14 @@ class ManageDocumentsTest < ActionController::IntegrationTest login('ze', 'test') - get '/cms/ze' + get '/myprofile/ze/cms' assert_response :success - get '/cms/ze/new' + get '/myprofile/ze/cms/new' assert_response :success - assert_tag :tag => 'form', :attributes => { :action => '/cms/ze/new' } + assert_tag :tag => 'form', :attributes => { :action => '/myprofile/ze/cms/new' } - 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 } + 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 } assert_response :redirect follow_redirect! @@ -29,15 +29,15 @@ class ManageDocumentsTest < ActionController::IntegrationTest def test_update_of_an_existing_article login('ze', 'test') - get '/cms/ze' + get '/myprofile/ze/cms' assert_response :success id = Comatose::Page.find_by_path('ze').id - get "cms/ze/edit/#{id}" + get "myprofile/ze/cms/edit/#{id}" assert_response :success - assert_tag :tag => 'form', :attributes => { :action => "/cms/ze/edit/#{id}" } + assert_tag :tag => 'form', :attributes => { :action => "/myprofile/ze/cms/edit/#{id}" } - post "cms/ze/edit/#{id}", :page => { :body => 'changed_body' } + post "myprofile/ze/cms/edit/#{id}", :page => { :body => 'changed_body' } assert_response :redirect end @@ -46,10 +46,10 @@ class ManageDocumentsTest < ActionController::IntegrationTest article = Article.create!(:title => 'to be removed', :body => 'go to hell', :parent_id => Article.find_by_path('ze').id) count = Article.count - get '/cms/ze' + get '/myprofile/ze/cms' assert_response :success - post "/cms/ze/delete/#{article.id}" + post "/myprofile/ze/cms/delete/#{article.id}" assert_response :redirect assert_raise ActiveRecord::RecordNotFound do diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index 509578a..8c88691 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -22,7 +22,7 @@ class RoutingTest < ActionController::IntegrationTest end def test_comatose_admin - assert_routing('/cms/ze', :profile => 'ze', :controller => 'cms', :action => 'index') + assert_routing('/myprofile/ze/cms', :profile => 'ze', :controller => 'cms', :action => 'index') end def test_edit_template -- libgit2 0.21.2