Commit 4c67982418a4546b98bc599c573e2777f354dcf5

Authored by Leandro Santos
Committed by Joenio Costa
1 parent ca8b44f6

Add the possibility to create an enterprise by interface

(ActionItem1542)
app/controllers/public/enterprise_registration_controller.rb
@@ -15,14 +15,16 @@ class EnterpriseRegistrationController < ApplicationController @@ -15,14 +15,16 @@ class EnterpriseRegistrationController < ApplicationController
15 if params[:create_enterprise] && params[:create_enterprise][:target_id] 15 if params[:create_enterprise] && params[:create_enterprise][:target_id]
16 @create_enterprise.target = Profile.find(params[:create_enterprise][:target_id]) 16 @create_enterprise.target = Profile.find(params[:create_enterprise][:target_id])
17 end 17 end
18 - elsif @validation == :admin 18 + elsif @validation == :admin || @validation == :none
19 @create_enterprise.target = @create_enterprise.environment 19 @create_enterprise.target = @create_enterprise.environment
20 end 20 end
21 @create_enterprise.requestor = current_user.person 21 @create_enterprise.requestor = current_user.person
22 the_action = 22 the_action =
23 if request.post? 23 if request.post?
24 if @create_enterprise.valid_before_selecting_target? 24 if @create_enterprise.valid_before_selecting_target?
25 - if @create_enterprise.valid? || @validation == :admin 25 + if @create_enterprise.valid? && @validation == :none
  26 + :creation
  27 + elsif @create_enterprise.valid? || @validation == :admin
26 :confirmation 28 :confirmation
27 else 29 else
28 :select_validator 30 :select_validator
@@ -63,4 +65,11 @@ class EnterpriseRegistrationController < ApplicationController @@ -63,4 +65,11 @@ class EnterpriseRegistrationController < ApplicationController
63 @create_enterprise.save! 65 @create_enterprise.save!
64 end 66 end
65 67
  68 + # Records the enterprise and presents a confirmation message
  69 + # saying to the user that the enterprise was created.
  70 + def creation
  71 + @create_enterprise.perform
  72 + @enterprise = @create_enterprise.target.profiles.find_by_identifier(@create_enterprise.identifier)
  73 + end
  74 +
66 end 75 end
app/helpers/features_helper.rb
@@ -3,6 +3,7 @@ module FeaturesHelper @@ -3,6 +3,7 @@ module FeaturesHelper
3 choices = [ 3 choices = [
4 [ _('Administrator must approve all new organizations'), 'admin'], 4 [ _('Administrator must approve all new organizations'), 'admin'],
5 [ _('Administrator assigns validator organizations per region.'), 'region'], 5 [ _('Administrator assigns validator organizations per region.'), 'region'],
  6 + [ _('All new organizations are approve by default'), 'none'],
6 ] 7 ]
7 value = instance_variable_get("@#{object}").send(method).to_s 8 value = instance_variable_get("@#{object}").send(method).to_s
8 select_tag("#{object}[#{method}]", options_for_select(choices, value)) 9 select_tag("#{object}[#{method}]", options_for_select(choices, value))
app/models/environment.rb
@@ -275,6 +275,7 @@ class Environment < ActiveRecord::Base @@ -275,6 +275,7 @@ class Environment < ActiveRecord::Base
275 # * <tt>:region</tt>: organization registering must be approved by some other 275 # * <tt>:region</tt>: organization registering must be approved by some other
276 # organization asssigned as validator to the Region the new organization 276 # organization asssigned as validator to the Region the new organization
277 # belongs to. 277 # belongs to.
  278 + # * <tt>:none</tt>: organization registration is approved by default.
278 # 279 #
279 # Trying to set organization_approval_method to any other value will raise an 280 # Trying to set organization_approval_method to any other value will raise an
280 # ArgumentError. 281 # ArgumentError.
@@ -287,6 +288,7 @@ class Environment &lt; ActiveRecord::Base @@ -287,6 +288,7 @@ class Environment &lt; ActiveRecord::Base
287 accepted_values = %w[ 288 accepted_values = %w[
288 admin 289 admin
289 region 290 region
  291 + none
290 ].map(&:to_sym) 292 ].map(&:to_sym)
291 raise ArgumentError unless accepted_values.include?(actual_value) 293 raise ArgumentError unless accepted_values.include?(actual_value)
292 294
app/views/enterprise_registration/creation.rhtml 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +<h1><%= _('Enterprise registration completed') %></h1>
  2 +
  3 +<p>
  4 +<%= _("Your enterprise (%s) was successfully registered.") % @enterprise.name %>
  5 +</p>
  6 +
  7 +<p>
  8 +<%= link_to _('You can manage your enterprise now.'), @enterprise.admin_url %>
  9 +</p>
app/views/search/enterprises.rhtml
@@ -15,6 +15,12 @@ @@ -15,6 +15,12 @@
15 15
16 <%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %> 16 <%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %>
17 17
  18 +<% if logged_in? && environment.enabled?('enterprise_registration') %>
  19 + <% button_bar do %>
  20 + <%= button(:add, __('New enterprise'), {:controller => 'enterprise_registration'}) %>
  21 + <% end %>
  22 +<% end %>
  23 +
18 <% if @categories_menu %> 24 <% if @categories_menu %>
19 <div class="has_cat_list"> 25 <div class="has_cat_list">
20 <% end %> 26 <% end %>
features/register_enterprise.feature
@@ -192,3 +192,12 @@ Feature: register enterprise @@ -192,3 +192,12 @@ Feature: register enterprise
192 And I am on Joao Silva's control panel 192 And I am on Joao Silva's control panel
193 When I follow "Manage my groups" 193 When I follow "Manage my groups"
194 Then I should not see "My Enterprise" 194 Then I should not see "My Enterprise"
  195 +
  196 + Scenario: a user can see button to register new enterprise
  197 + When I am on /assets/enterprises
  198 + Then I should see "New enterprise" link
  199 +
  200 + Scenario: a user cant see button to register new enterprise if enterprise_registration disabled
  201 + Given feature "enterprise_registration" is disabled on environment
  202 + When I am on /assets/enterprises
  203 + Then I should not see "New enterprise" link
test/functional/enterprise_registration_controller_test.rb
@@ -49,6 +49,19 @@ all_fixtures @@ -49,6 +49,19 @@ all_fixtures
49 assert_template 'confirmation' 49 assert_template 'confirmation'
50 end 50 end
51 51
  52 + should 'skip prompt for selection validator if approval method is none' do
  53 + env = Environment.default
  54 + env.organization_approval_method = :none
  55 + env.save
  56 + region = fast_create(Region)
  57 +
  58 + data = { :name => 'My new enterprise', :identifier => 'mynew', :region => region }
  59 + create_enterprise = CreateEnterprise.new(data)
  60 +
  61 + post :index, :create_enterprise => data
  62 + assert_template 'creation'
  63 + end
  64 +
52 should 'prompt for selecting validator if approval method is region' do 65 should 'prompt for selecting validator if approval method is region' do
53 env = Environment.default 66 env = Environment.default
54 env.organization_approval_method = :region 67 env.organization_approval_method = :region
@@ -77,7 +90,7 @@ all_fixtures @@ -77,7 +90,7 @@ all_fixtures
77 90
78 # all data but validator selected 91 # all data but validator selected
79 create_enterprise.expects(:valid_before_selecting_target?).returns(true) 92 create_enterprise.expects(:valid_before_selecting_target?).returns(true)
80 - create_enterprise.expects(:valid?).returns(false) 93 + create_enterprise.stubs(:valid?).returns(false)
81 94
82 post :index, :create_enterprise => data 95 post :index, :create_enterprise => data
83 assert_template 'select_validator' 96 assert_template 'select_validator'
@@ -93,7 +106,7 @@ all_fixtures @@ -93,7 +106,7 @@ all_fixtures
93 validator = mock() 106 validator = mock()
94 validator.stubs(:name).returns("lalala") 107 validator.stubs(:name).returns("lalala")
95 create_enterprise.expects(:valid_before_selecting_target?).returns(true) 108 create_enterprise.expects(:valid_before_selecting_target?).returns(true)
96 - create_enterprise.expects(:valid?).returns(true) # validator already selected 109 + create_enterprise.stubs(:valid?).returns(true) # validator already selected
97 create_enterprise.expects(:save!) 110 create_enterprise.expects(:save!)
98 create_enterprise.expects(:target).returns(validator) 111 create_enterprise.expects(:target).returns(validator)
99 112
test/unit/environment_test.rb
@@ -241,6 +241,7 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -241,6 +241,7 @@ class EnvironmentTest &lt; Test::Unit::TestCase
241 valid = %w[ 241 valid = %w[
242 admin 242 admin
243 region 243 region
  244 + none
244 ].each do |item| 245 ].each do |item|
245 env.organization_approval_method = item 246 env.organization_approval_method = item
246 env.organization_approval_method = item.to_sym 247 env.organization_approval_method = item.to_sym