diff --git a/app/controllers/enterprise_controller.rb b/app/controllers/enterprise_controller.rb index 1f17be2..c59e067 100644 --- a/app/controllers/enterprise_controller.rb +++ b/app/controllers/enterprise_controller.rb @@ -85,7 +85,7 @@ class EnterpriseController < ApplicationController # Activate a validated enterprise def activate @enterprise = Enterprise.find(params[:id]) - if @enterprise.update_attribute('active', true) + if @enterprise.activate flash[:notice] = _('Enterprise successfuly activacted') else flash[:notice] = _('Failed to activate the enterprise') @@ -93,6 +93,28 @@ class EnterpriseController < ApplicationController redirect_to :action => 'index' end + # Validates an eterprise + def approve + @enterprise = Enterprise.find(params[:id]) + if @enterprise.approve + flash[:notice] = _('Enterprise successfuly approved') + else + flash[:notice] = _('Failed to approve the enterprise') + end + redirect_to :action => 'index' + end + + def reject + @enterprise = Enterprise.find(params[:id]) + if @enterprise.reject + flash[:notice] = _('Enterprise successfuly rejected') + else + flash[:notice] = _('Failed to reject the enterprise') + end + redirect_to :action => 'index' + end + + protected # Make sure that the user is logged before access this controller diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index c5fdabb..55d5924 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -1,4 +1,42 @@ #An enterprise is a kind of organization. According to the system concept, only enterprises can offer products/services and ahave to be validated by an validation entity class Enterprise < Organization belongs_to :validation_entity, :class_name => 'organization', :foreign_key => 'validation_entity_id' + has_one :enterprise_info + + after_create do |enterprise| + EnterpriseInfo.create!(:enterprise_id => enterprise.id) + end + + # Test that an enterprise can't be activated unless was previously approved + def validate + if self.active && !self.approved? + errors.add('active', _('Not approved enterprise can\'t be activated')) + end + end + + # Activate the enterprise so it can be seen by other users + def activate + self.active = true + self.save + end + + # Approve the enterprise so it can be activated by its owner + def approve + enterprise_info.update_attribute('approval_status', 'approved') + end + + # Reject the approval of the enterprise giving a status message describing its problem + def reject(msg = 'rejected', comments = '') + enterprise_info.update_attribute('approval_status', msg) + enterprise_info.update_attribute('approval_comments', comments) + end + + # Check if the enterprise was approved, that is if the fild approval_status holds the string 'approved' + def approved? + enterprise_info.approval_status == 'approved' + end + # Check if the enterprise was rejected, that is if the fild approval_status holds the string 'rejected' + def rejected? + enterprise_info.approval_status == 'rejected' + end end diff --git a/app/models/enterprise_info.rb b/app/models/enterprise_info.rb new file mode 100644 index 0000000..0dd3a22 --- /dev/null +++ b/app/models/enterprise_info.rb @@ -0,0 +1,3 @@ +class EnterpriseInfo < ActiveRecord::Base + belongs_to :enterprise +end diff --git a/app/models/person.rb b/app/models/person.rb index a245307..62dce7a 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -3,7 +3,7 @@ class Person < Profile ENTERPRISE = {:class_name => 'Enterprise', :through => :affiliations, :foreign_key => 'person_id', :source => 'profile'} belongs_to :user - has_many :affiliations + has_many :affiliations, :dependent => :destroy has_many :profiles, :through => :affiliations has_many :enterprises, ENTERPRISE has_many :pending_enterprises, ENTERPRISE.merge(:conditions => ['active = ?', false]) diff --git a/app/models/profile.rb b/app/models/profile.rb index a926fff..6977f46 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -27,7 +27,7 @@ class Profile < ActiveRecord::Base has_many :domains, :as => :owner belongs_to :virtual_community - has_many :affiliations + has_many :affiliations, :dependent => :destroy has_many :people, :through => :affiliations diff --git a/app/views/enterprise/_enterprise.rhtml b/app/views/enterprise/_enterprise.rhtml index 346106b..f2f72ec 100644 --- a/app/views/enterprise/_enterprise.rhtml +++ b/app/views/enterprise/_enterprise.rhtml @@ -6,6 +6,12 @@ <%= help _('Remove the enterprise from the system') %> <%= link_to _('Affiliate'), :action => 'affiliate', :id => enterprise unless @my_enterprises.include?(enterprise) %> <%= help _('Be a member of the enterprise') unless @my_enterprises.include?(enterprise) %> -<%= link_to _('Activate'), :action => 'activate', :id => enterprise if @my_pending_enterprises.include?(enterprise) %> -<%= help _('Activate the profile of an approved enterprise') if @my_pending_enterprises.include?(enterprise) %> +<%= link_to _('Activate'), :action => 'activate', :id => enterprise unless enterprise.active %> +<%= help _('Activate the profile of an approved enterprise') unless enterprise.active %> +<% unless enterprise.approved? %> + <%= link_to _('Approve'), :action => 'approve', :id => enterprise %> + <%= help _('Approve a submitted enterprise profile') %> + <%= link_to _('Reject'), :action => 'reject', :id => enterprise %> + <%= help _('Reject a submitted enterprise profile') %> +<% end %> diff --git a/app/views/enterprise/list.rhtml b/app/views/enterprise/list.rhtml index 27d9e2a..0ca3692 100644 --- a/app/views/enterprise/list.rhtml +++ b/app/views/enterprise/list.rhtml @@ -1,5 +1,8 @@ <%= render :partial => 'search_box' %> +<%= error_messages_for 'enterprise' %> +<%= error_messages_for 'enterprise_info' %> +

<%= link_to _('Register new enterprise'), :action => 'register_form' %> <%= help _('Creates a new enterprise') %>

diff --git a/app/views/enterprise/search.rhtml b/app/views/enterprise/search.rhtml index 10bd8bc..0387a8e 100644 --- a/app/views/enterprise/search.rhtml +++ b/app/views/enterprise/search.rhtml @@ -1,3 +1,3 @@ -

<%= @tagged_enterprises.size.to_s + _(' tags found') %>

+

<%= @tagged_enterprises.size.to_s + _(' enterprises found') %>

<%= render :partial => 'enterprise', :collection => @tagged_enterprises %> diff --git a/db/migrate/003_create_profiles.rb b/db/migrate/003_create_profiles.rb index 8f979bf..061c7b6 100644 --- a/db/migrate/003_create_profiles.rb +++ b/db/migrate/003_create_profiles.rb @@ -17,6 +17,7 @@ class CreateProfiles < ActiveRecord::Migration #enterprise fields t.column :validation_entity_id, :integer + t.column :approved, :boolean end end diff --git a/db/migrate/012_create_enterprise_infos.rb b/db/migrate/012_create_enterprise_infos.rb new file mode 100644 index 0000000..29478a4 --- /dev/null +++ b/db/migrate/012_create_enterprise_infos.rb @@ -0,0 +1,13 @@ +class CreateEnterpriseInfos < ActiveRecord::Migration + def self.up + create_table :enterprise_infos do |t| + t.column :approval_status, :string, :default => 'not evaluated' + t.column :approval_comments, :text + t.column :enterprise_id, :integer + end + end + + def self.down + drop_table :enterprise_infos + end +end diff --git a/test/fixtures/enterprise_infos.yml b/test/fixtures/enterprise_infos.yml new file mode 100644 index 0000000..6878365 --- /dev/null +++ b/test/fixtures/enterprise_infos.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +one: + id: 1 + enterprise_id: 5 + approval_status: 'approved' +two: + id: 2 + enterprise_id: 6 + approval_status: 'approved' diff --git a/test/unit/enterprise_info_test.rb b/test/unit/enterprise_info_test.rb new file mode 100644 index 0000000..5b2d958 --- /dev/null +++ b/test/unit/enterprise_info_test.rb @@ -0,0 +1,10 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class EnterpriseInfoTest < Test::Unit::TestCase + fixtures :enterprise_infos + + # Replace this with your real tests. + def test_truth + assert true + end +end diff --git a/test/unit/enterprise_test.rb b/test/unit/enterprise_test.rb index a9522b0..0691c6e 100644 --- a/test/unit/enterprise_test.rb +++ b/test/unit/enterprise_test.rb @@ -45,4 +45,14 @@ class EnterpriseTest < Test::Unit::TestCase p1.identifier = 'bli' end end + + def test_cannot_be_activated_without_approval + e = Enterprise.create(:identifier => 'bli', :name => 'Bli') + assert !e.approved + e.activate + assert !e.valid? + e.approve + e.activate + assert e.valid? + end end -- libgit2 0.21.2