Commit bfce4eb13212734b675c4fd07f23ecca6946cb3e

Authored by Rodrigo Souto
1 parent ddc24ef4

[stoa] Fixing plugin routes

lib/noosfero/plugin/routes.rb
1 1 Dir.glob(File.join(Rails.root, 'config', 'plugins', '*', 'controllers')) do |dir|
2 2 plugin_name = File.basename(File.dirname(dir))
3   - map.connect 'plugin/' + plugin_name + '/:action/:id', :controller => plugin_name + '_plugin_environment'
  3 + map.connect 'plugin/' + plugin_name + '/:action/:id', :controller => plugin_name + '_plugin'
4 4 map.connect 'profile/:profile/plugins/' + plugin_name + '/:action/:id', :controller => plugin_name + '_plugin_profile'
5 5 map.connect 'myprofile/:profile/plugin/' + plugin_name + '/:action/:id', :controller => plugin_name + '_plugin_myprofile'
6 6 map.connect 'admin/plugin/' + plugin_name + '/:action/:id', :controller => plugin_name + '_plugin_admin'
... ...
plugins/bsc/controllers/bsc_plugin_admin_controller.rb 0 → 100644
... ... @@ -0,0 +1,37 @@
  1 +class BscPluginAdminController < AdminController
  2 +
  3 + include BscPlugin::BscHelper
  4 +
  5 + def new
  6 + @bsc = BscPlugin::Bsc.new(params[:profile_data])
  7 + if request.post? && @bsc.valid?
  8 + @bsc.user = current_user
  9 + @bsc.save!
  10 + @bsc.add_admin(user)
  11 + session[:notice] = _('Your Bsc was created.')
  12 + redirect_to :controller => 'profile_editor', :profile => @bsc.identifier
  13 + end
  14 + end
  15 +
  16 + def save_validations
  17 + enterprises = [Enterprise.find(params[:q].split(','))].flatten
  18 +
  19 + begin
  20 + enterprises.each { |enterprise| enterprise.validated = true ; enterprise.save! }
  21 + session[:notice] = _('Enterprises validated.')
  22 + redirect_to :controller => 'admin_panel'
  23 + rescue Exception => ex
  24 + session[:notice] = _('Enterprise validations couldn\'t be saved.')
  25 + logger.info ex
  26 + redirect_to :action => 'validate_enterprises'
  27 + end
  28 + end
  29 +
  30 + def search_enterprise
  31 + render :text => Enterprise.not_validated.find(:all, :conditions => ["type <> 'BscPlugin::Bsc' AND (name LIKE ? OR identifier LIKE ?)", "%#{params[:q]}%", "%#{params[:q]}%"]).
  32 + map {|enterprise| {:id => enterprise.id, :name => enterprise.name} }.
  33 + to_json
  34 + end
  35 +
  36 +end
  37 +
... ...
plugins/bsc/controllers/bsc_plugin_environment_controller.rb
... ... @@ -1,37 +0,0 @@
1   -class BscPluginEnvironmentController < AdminController
2   -
3   - include BscPlugin::BscHelper
4   -
5   - def new
6   - @bsc = BscPlugin::Bsc.new(params[:profile_data])
7   - if request.post? && @bsc.valid?
8   - @bsc.user = current_user
9   - @bsc.save!
10   - @bsc.add_admin(user)
11   - session[:notice] = _('Your Bsc was created.')
12   - redirect_to :controller => 'profile_editor', :profile => @bsc.identifier
13   - end
14   - end
15   -
16   - def save_validations
17   - enterprises = [Enterprise.find(params[:q].split(','))].flatten
18   -
19   - begin
20   - enterprises.each { |enterprise| enterprise.validated = true ; enterprise.save! }
21   - session[:notice] = _('Enterprises validated.')
22   - redirect_to :controller => 'admin_panel'
23   - rescue Exception => ex
24   - session[:notice] = _('Enterprise validations couldn\'t be saved.')
25   - logger.info ex
26   - redirect_to :action => 'validate_enterprises'
27   - end
28   - end
29   -
30   - def search_enterprise
31   - render :text => Enterprise.not_validated.find(:all, :conditions => ["type <> 'BscPlugin::Bsc' AND (name LIKE ? OR identifier LIKE ?)", "%#{params[:q]}%", "%#{params[:q]}%"]).
32   - map {|enterprise| {:id => enterprise.id, :name => enterprise.name} }.
33   - to_json
34   - end
35   -
36   -end
37   -
plugins/bsc/lib/bsc_plugin.rb
... ... @@ -14,8 +14,8 @@ class BscPlugin &lt; Noosfero::Plugin
14 14 end
15 15  
16 16 def admin_panel_links
17   - [{:title => _('Create Bsc'), :url => {:controller => 'bsc_plugin_environment', :action => 'new'}},
18   - {:title => _('Validate Enterprises'), :url => {:controller => 'bsc_plugin_environment', :action => 'validate_enterprises'}} ]
  17 + [{:title => _('Create Bsc'), :url => {:controller => 'bsc_plugin_admin', :action => 'new'}},
  18 + {:title => _('Validate Enterprises'), :url => {:controller => 'bsc_plugin_admin', :action => 'validate_enterprises'}} ]
19 19 end
20 20  
21 21 def control_panel_buttons
... ...
plugins/bsc/test/functional/bsc_plugin_admin_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,82 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +require File.dirname(__FILE__) + '/../../controllers/bsc_plugin_admin_controller'
  3 +require File.dirname(__FILE__) + '/../../../../app/models/uploaded_file'
  4 +
  5 +# Re-raise errors caught by the controller.
  6 +class BscPluginAdminController; def rescue_action(e) raise e end; end
  7 +
  8 +class BscPluginAdminControllerTest < ActionController::TestCase
  9 +
  10 + VALID_CNPJ = '94.132.024/0001-48'
  11 +
  12 + def setup
  13 + @controller = BscPluginAdminController.new
  14 + @request = ActionController::TestRequest.new
  15 + @response = ActionController::TestResponse.new
  16 + user_login = create_admin_user(Environment.default)
  17 + login_as(user_login)
  18 + @admin = User[user_login].person
  19 + e = Environment.default
  20 + e.enabled_plugins = ['BscPlugin']
  21 + e.save!
  22 + end
  23 +
  24 + attr_accessor :admin
  25 +
  26 + should 'create a new bsc' do
  27 + assert_difference BscPlugin::Bsc, :count, 1 do
  28 + post :new, :profile_data => {:business_name => 'Sample Bsc', :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => VALID_CNPJ}
  29 + end
  30 +
  31 + assert_redirected_to :controller => 'profile_editor', :profile => 'sample-bsc'
  32 + end
  33 +
  34 + should 'not create an invalid bsc' do
  35 + assert_difference BscPlugin::Bsc, :count, 0 do
  36 + post :new, :profile_data => {:business_name => 'Sample Bsc', :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => '29837492304'}
  37 + end
  38 +
  39 + assert_response 200
  40 + end
  41 +
  42 + should 'set the current user as the bsc admin' do
  43 + name = 'Sample Bsc'
  44 + post :new, :profile_data => {:business_name => name, :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => VALID_CNPJ}
  45 + bsc = BscPlugin::Bsc.find_by_name(name)
  46 + assert_includes bsc.admins, admin
  47 + end
  48 +
  49 + should 'list correct enterprises on search' do
  50 + # Should list if: not validated AND (name matches OR identifier matches) AND not bsc
  51 + e1 = Enterprise.create!(:name => 'Sample Enterprise 1', :identifier => 'bli', :validated => false)
  52 + e2 = Enterprise.create!(:name => 'Bla', :identifier => 'sample-enterprise-6', :validated => false)
  53 + e3 = Enterprise.create!(:name => 'Blo', :identifier => 'blo', :validated => false)
  54 + e4 = BscPlugin::Bsc.create!(:business_name => "Sample Bsc", :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => VALID_CNPJ, :validated => false)
  55 + e5 = Enterprise.create!(:name => 'Sample Enterprise 5', :identifier => 'sample-enterprise-5')
  56 + e5.validated = true
  57 + e5.save!
  58 +
  59 + get :search_enterprise, :q => 'sampl'
  60 +
  61 + assert_match /#{e1.name}/, @response.body
  62 + assert_match /#{e2.name}/, @response.body
  63 + assert_no_match /#{e3.name}/, @response.body
  64 + assert_no_match /#{e4.name}/, @response.body
  65 + assert_no_match /#{e5.name}/, @response.body
  66 + end
  67 +
  68 + should 'save validations' do
  69 + e1 = fast_create(Enterprise, :validated => false)
  70 + e2 = fast_create(Enterprise, :validated => false)
  71 + e3 = fast_create(Enterprise, :validated => false)
  72 +
  73 + post :save_validations, :q => "#{e1.id},#{e2.id}"
  74 + e1.reload
  75 + e2.reload
  76 + e3.reload
  77 +
  78 + assert e1.validated
  79 + assert e2.validated
  80 + assert !e3.validated
  81 + end
  82 +end
... ...
plugins/bsc/test/functional/bsc_plugin_environment_controller_test.rb
... ... @@ -1,82 +0,0 @@
1   -require File.dirname(__FILE__) + '/../../../../test/test_helper'
2   -require File.dirname(__FILE__) + '/../../controllers/bsc_plugin_environment_controller'
3   -require File.dirname(__FILE__) + '/../../../../app/models/uploaded_file'
4   -
5   -# Re-raise errors caught by the controller.
6   -class BscPluginEnvironmentController; def rescue_action(e) raise e end; end
7   -
8   -class BscPluginEnvironmentControllerTest < ActionController::TestCase
9   -
10   - VALID_CNPJ = '94.132.024/0001-48'
11   -
12   - def setup
13   - @controller = BscPluginEnvironmentController.new
14   - @request = ActionController::TestRequest.new
15   - @response = ActionController::TestResponse.new
16   - user_login = create_admin_user(Environment.default)
17   - login_as(user_login)
18   - @admin = User[user_login].person
19   - e = Environment.default
20   - e.enabled_plugins = ['BscPlugin']
21   - e.save!
22   - end
23   -
24   - attr_accessor :admin
25   -
26   - should 'create a new bsc' do
27   - assert_difference BscPlugin::Bsc, :count, 1 do
28   - post :new, :profile_data => {:business_name => 'Sample Bsc', :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => VALID_CNPJ}
29   - end
30   -
31   - assert_redirected_to :controller => 'profile_editor', :profile => 'sample-bsc'
32   - end
33   -
34   - should 'not create an invalid bsc' do
35   - assert_difference BscPlugin::Bsc, :count, 0 do
36   - post :new, :profile_data => {:business_name => 'Sample Bsc', :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => '29837492304'}
37   - end
38   -
39   - assert_response 200
40   - end
41   -
42   - should 'set the current user as the bsc admin' do
43   - name = 'Sample Bsc'
44   - post :new, :profile_data => {:business_name => name, :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => VALID_CNPJ}
45   - bsc = BscPlugin::Bsc.find_by_name(name)
46   - assert_includes bsc.admins, admin
47   - end
48   -
49   - should 'list correct enterprises on search' do
50   - # Should list if: not validated AND (name matches OR identifier matches) AND not bsc
51   - e1 = Enterprise.create!(:name => 'Sample Enterprise 1', :identifier => 'bli', :validated => false)
52   - e2 = Enterprise.create!(:name => 'Bla', :identifier => 'sample-enterprise-6', :validated => false)
53   - e3 = Enterprise.create!(:name => 'Blo', :identifier => 'blo', :validated => false)
54   - e4 = BscPlugin::Bsc.create!(:business_name => "Sample Bsc", :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => VALID_CNPJ, :validated => false)
55   - e5 = Enterprise.create!(:name => 'Sample Enterprise 5', :identifier => 'sample-enterprise-5')
56   - e5.validated = true
57   - e5.save!
58   -
59   - get :search_enterprise, :q => 'sampl'
60   -
61   - assert_match /#{e1.name}/, @response.body
62   - assert_match /#{e2.name}/, @response.body
63   - assert_no_match /#{e3.name}/, @response.body
64   - assert_no_match /#{e4.name}/, @response.body
65   - assert_no_match /#{e5.name}/, @response.body
66   - end
67   -
68   - should 'save validations' do
69   - e1 = fast_create(Enterprise, :validated => false)
70   - e2 = fast_create(Enterprise, :validated => false)
71   - e3 = fast_create(Enterprise, :validated => false)
72   -
73   - post :save_validations, :q => "#{e1.id},#{e2.id}"
74   - e1.reload
75   - e2.reload
76   - e3.reload
77   -
78   - assert e1.validated
79   - assert e2.validated
80   - assert !e3.validated
81   - end
82   -end
plugins/bsc/views/bsc_plugin_admin/new.html.erb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +<%= error_messages_for :bsc %>
  2 +<h1><%= _('BSC registration') %></h1>
  3 +
  4 +<% labelled_form_for :profile_data, @bsc do |f| %>
  5 + <%= render :partial => 'shared/fields', :locals => {:f => f, :profile => @bsc} %>
  6 +
  7 + <% button_bar do %>
  8 + <%= submit_button('save', _('Save')) %>
  9 + <%= button('cancel', _('Cancel'), {:controller => 'admin_panel'}) %>
  10 + <% end %>
  11 +<% end %>
... ...
plugins/bsc/views/bsc_plugin_admin/validate_enterprises.html.erb 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +<h1><%= _('Validate enterprises') %></h1>
  2 +
  3 +<% form_tag :action => 'save_validations' do %>
  4 + <%= token_input_field_tag(:q, 'search-enterprises', {:action => 'search_enterprise'},
  5 + { :hint_text => _('Type in a search term for enterprise'),
  6 + :focus => true }) %>
  7 +
  8 + <% button_bar do %>
  9 + <%= submit_button('save', _('Save'))%>
  10 + <%= button('cancel', _('Cancel'), {:controller => 'admin_panel'})%>
  11 + <% end %>
  12 +<% end %>
... ...
plugins/bsc/views/bsc_plugin_environment/new.html.erb
... ... @@ -1,11 +0,0 @@
1   -<%= error_messages_for :bsc %>
2   -<h1><%= _('BSC registration') %></h1>
3   -
4   -<% labelled_form_for :profile_data, @bsc do |f| %>
5   - <%= render :partial => 'shared/fields', :locals => {:f => f, :profile => @bsc} %>
6   -
7   - <% button_bar do %>
8   - <%= submit_button('save', _('Save')) %>
9   - <%= button('cancel', _('Cancel'), {:controller => 'admin_panel'}) %>
10   - <% end %>
11   -<% end %>
plugins/bsc/views/bsc_plugin_environment/validate_enterprises.html.erb
... ... @@ -1,12 +0,0 @@
1   -<h1><%= _('Validate enterprises') %></h1>
2   -
3   -<% form_tag :action => 'save_validations' do %>
4   - <%= token_input_field_tag(:q, 'search-enterprises', {:action => 'search_enterprise'},
5   - { :hint_text => _('Type in a search term for enterprise'),
6   - :focus => true }) %>
7   -
8   - <% button_bar do %>
9   - <%= submit_button('save', _('Save'))%>
10   - <%= button('cancel', _('Cancel'), {:controller => 'admin_panel'})%>
11   - <% end %>
12   -<% end %>