Commit a8c2d855e42ad70d46665f1f55fd76abda342b73

Authored by MoisesMachado
1 parent 95942fcc

ActionItem16: added approve and reject capabilities to enterprise


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@399 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/enterprise_controller.rb
... ... @@ -85,7 +85,7 @@ class EnterpriseController < ApplicationController
85 85 # Activate a validated enterprise
86 86 def activate
87 87 @enterprise = Enterprise.find(params[:id])
88   - if @enterprise.update_attribute('active', true)
  88 + if @enterprise.activate
89 89 flash[:notice] = _('Enterprise successfuly activacted')
90 90 else
91 91 flash[:notice] = _('Failed to activate the enterprise')
... ... @@ -93,6 +93,28 @@ class EnterpriseController < ApplicationController
93 93 redirect_to :action => 'index'
94 94 end
95 95  
  96 + # Validates an eterprise
  97 + def approve
  98 + @enterprise = Enterprise.find(params[:id])
  99 + if @enterprise.approve
  100 + flash[:notice] = _('Enterprise successfuly approved')
  101 + else
  102 + flash[:notice] = _('Failed to approve the enterprise')
  103 + end
  104 + redirect_to :action => 'index'
  105 + end
  106 +
  107 + def reject
  108 + @enterprise = Enterprise.find(params[:id])
  109 + if @enterprise.reject
  110 + flash[:notice] = _('Enterprise successfuly rejected')
  111 + else
  112 + flash[:notice] = _('Failed to reject the enterprise')
  113 + end
  114 + redirect_to :action => 'index'
  115 + end
  116 +
  117 +
96 118 protected
97 119  
98 120 # Make sure that the user is logged before access this controller
... ...
app/models/enterprise.rb
1 1 #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
2 2 class Enterprise < Organization
3 3 belongs_to :validation_entity, :class_name => 'organization', :foreign_key => 'validation_entity_id'
  4 + has_one :enterprise_info
  5 +
  6 + after_create do |enterprise|
  7 + EnterpriseInfo.create!(:enterprise_id => enterprise.id)
  8 + end
  9 +
  10 + # Test that an enterprise can't be activated unless was previously approved
  11 + def validate
  12 + if self.active && !self.approved?
  13 + errors.add('active', _('Not approved enterprise can\'t be activated'))
  14 + end
  15 + end
  16 +
  17 + # Activate the enterprise so it can be seen by other users
  18 + def activate
  19 + self.active = true
  20 + self.save
  21 + end
  22 +
  23 + # Approve the enterprise so it can be activated by its owner
  24 + def approve
  25 + enterprise_info.update_attribute('approval_status', 'approved')
  26 + end
  27 +
  28 + # Reject the approval of the enterprise giving a status message describing its problem
  29 + def reject(msg = 'rejected', comments = '')
  30 + enterprise_info.update_attribute('approval_status', msg)
  31 + enterprise_info.update_attribute('approval_comments', comments)
  32 + end
  33 +
  34 + # Check if the enterprise was approved, that is if the fild approval_status holds the string 'approved'
  35 + def approved?
  36 + enterprise_info.approval_status == 'approved'
  37 + end
  38 + # Check if the enterprise was rejected, that is if the fild approval_status holds the string 'rejected'
  39 + def rejected?
  40 + enterprise_info.approval_status == 'rejected'
  41 + end
4 42 end
... ...
app/models/enterprise_info.rb 0 → 100644
... ... @@ -0,0 +1,3 @@
  1 +class EnterpriseInfo < ActiveRecord::Base
  2 + belongs_to :enterprise
  3 +end
... ...
app/models/person.rb
... ... @@ -3,7 +3,7 @@ class Person &lt; Profile
3 3 ENTERPRISE = {:class_name => 'Enterprise', :through => :affiliations, :foreign_key => 'person_id', :source => 'profile'}
4 4  
5 5 belongs_to :user
6   - has_many :affiliations
  6 + has_many :affiliations, :dependent => :destroy
7 7 has_many :profiles, :through => :affiliations
8 8 has_many :enterprises, ENTERPRISE
9 9 has_many :pending_enterprises, ENTERPRISE.merge(:conditions => ['active = ?', false])
... ...
app/models/profile.rb
... ... @@ -27,7 +27,7 @@ class Profile &lt; ActiveRecord::Base
27 27  
28 28 has_many :domains, :as => :owner
29 29 belongs_to :virtual_community
30   - has_many :affiliations
  30 + has_many :affiliations, :dependent => :destroy
31 31 has_many :people, :through => :affiliations
32 32  
33 33  
... ...
app/views/enterprise/_enterprise.rhtml
... ... @@ -6,6 +6,12 @@
6 6 <%= help _('Remove the enterprise from the system') %>
7 7 <%= link_to _('Affiliate'), :action => 'affiliate', :id => enterprise unless @my_enterprises.include?(enterprise) %>
8 8 <%= help _('Be a member of the enterprise') unless @my_enterprises.include?(enterprise) %>
9   -<%= link_to _('Activate'), :action => 'activate', :id => enterprise if @my_pending_enterprises.include?(enterprise) %>
10   -<%= help _('Activate the profile of an approved enterprise') if @my_pending_enterprises.include?(enterprise) %>
  9 +<%= link_to _('Activate'), :action => 'activate', :id => enterprise unless enterprise.active %>
  10 +<%= help _('Activate the profile of an approved enterprise') unless enterprise.active %>
  11 +<% unless enterprise.approved? %>
  12 + <%= link_to _('Approve'), :action => 'approve', :id => enterprise %>
  13 + <%= help _('Approve a submitted enterprise profile') %>
  14 + <%= link_to _('Reject'), :action => 'reject', :id => enterprise %>
  15 + <%= help _('Reject a submitted enterprise profile') %>
  16 +<% end %>
11 17 </li>
... ...
app/views/enterprise/list.rhtml
1 1 <%= render :partial => 'search_box' %>
2 2  
  3 +<%= error_messages_for 'enterprise' %>
  4 +<%= error_messages_for 'enterprise_info' %>
  5 +
3 6 <p> <%= link_to _('Register new enterprise'), :action => 'register_form' %>
4 7 <%= help _('Creates a new enterprise') %>
5 8 </p>
... ...
app/views/enterprise/search.rhtml
1   -<h2> <%= @tagged_enterprises.size.to_s + _(' tags found') %> </h2>
  1 +<h2> <%= @tagged_enterprises.size.to_s + _(' enterprises found') %> </h2>
2 2  
3 3 <%= render :partial => 'enterprise', :collection => @tagged_enterprises %>
... ...
db/migrate/003_create_profiles.rb
... ... @@ -17,6 +17,7 @@ class CreateProfiles &lt; ActiveRecord::Migration
17 17  
18 18 #enterprise fields
19 19 t.column :validation_entity_id, :integer
  20 + t.column :approved, :boolean
20 21 end
21 22 end
22 23  
... ...
db/migrate/012_create_enterprise_infos.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +class CreateEnterpriseInfos < ActiveRecord::Migration
  2 + def self.up
  3 + create_table :enterprise_infos do |t|
  4 + t.column :approval_status, :string, :default => 'not evaluated'
  5 + t.column :approval_comments, :text
  6 + t.column :enterprise_id, :integer
  7 + end
  8 + end
  9 +
  10 + def self.down
  11 + drop_table :enterprise_infos
  12 + end
  13 +end
... ...
test/fixtures/enterprise_infos.yml 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
  2 +one:
  3 + id: 1
  4 + enterprise_id: 5
  5 + approval_status: 'approved'
  6 +two:
  7 + id: 2
  8 + enterprise_id: 6
  9 + approval_status: 'approved'
... ...
test/unit/enterprise_info_test.rb 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class EnterpriseInfoTest < Test::Unit::TestCase
  4 + fixtures :enterprise_infos
  5 +
  6 + # Replace this with your real tests.
  7 + def test_truth
  8 + assert true
  9 + end
  10 +end
... ...
test/unit/enterprise_test.rb
... ... @@ -45,4 +45,14 @@ class EnterpriseTest &lt; Test::Unit::TestCase
45 45 p1.identifier = 'bli'
46 46 end
47 47 end
  48 +
  49 + def test_cannot_be_activated_without_approval
  50 + e = Enterprise.create(:identifier => 'bli', :name => 'Bli')
  51 + assert !e.approved
  52 + e.activate
  53 + assert !e.valid?
  54 + e.approve
  55 + e.activate
  56 + assert e.valid?
  57 + end
48 58 end
... ...