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 @@ -