Commit 66331e33c053587eeb3a5cd5ee92f99087f9f9cd
1 parent
85914506
Exists in
master
and in
23 other branches
ActionItem14: checkpoint; I'm getting there
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@682 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
6 changed files
with
168 additions
and
17 deletions
Show diff stats
app/controllers/profile_admin/membership_editor_controller.rb
| ... | ... | @@ -20,7 +20,6 @@ class MembershipEditorController < ProfileAdminController |
| 20 | 20 | redirect_to :action => 'index' |
| 21 | 21 | else |
| 22 | 22 | flash[:notice] = _('Enterprise was not created') |
| 23 | - @vitual_communities = Environment.find(:all) | |
| 24 | 23 | @validation_entities = Organization.find(:all) |
| 25 | 24 | render :action => 'register_form' |
| 26 | 25 | end | ... | ... |
app/models/create_enterprise.rb
| 1 | 1 | class CreateEnterprise < Task |
| 2 | 2 | |
| 3 | - DATA_FIELDS = %w[ name identifier address contact_phone contact_person acronym foundation_year legal_form economic_activity management_information ] | |
| 3 | + DATA_FIELDS = %w[ name identifier address contact_phone contact_person acronym foundation_year legal_form economic_activity management_information region_id ] | |
| 4 | 4 | |
| 5 | 5 | serialize :data, Hash |
| 6 | 6 | attr_protected :data |
| ... | ... | @@ -20,10 +20,63 @@ class CreateEnterprise < Task |
| 20 | 20 | end |
| 21 | 21 | |
| 22 | 22 | # checks for virtual attributes |
| 23 | - validates_presence_of :name, :identifier, :address, :contact_phone, :contact_person, :legal_form, :economic_activity | |
| 23 | + validates_presence_of :name, :identifier, :address, :contact_phone, :contact_person, :legal_form, :economic_activity, :region_id | |
| 24 | 24 | validates_format_of :foundation_year, :with => /^\d*$/ |
| 25 | 25 | |
| 26 | 26 | # checks for actual attributes |
| 27 | - validates_presence_of :requestor_id | |
| 27 | + validates_presence_of :requestor_id, :target_id | |
| 28 | + | |
| 29 | + def validate | |
| 30 | + if self.region && self.target | |
| 31 | + unless self.region.validators.include?(self.target) | |
| 32 | + self.errors.add(:target, '%{fn} is not a validator for the chosen region') | |
| 33 | + end | |
| 34 | + end | |
| 35 | + end | |
| 36 | + | |
| 37 | + # gets the associated region for the enterprise creation | |
| 38 | + def region(reload = false) | |
| 39 | + if self.region_id | |
| 40 | + if reload || @region.nil? | |
| 41 | + @region = Region.find(self.region_id) | |
| 42 | + end | |
| 43 | + end | |
| 44 | + @region | |
| 45 | + end | |
| 46 | + | |
| 47 | + # sets the associated region for the enterprise creation | |
| 48 | + def region=(value) | |
| 49 | + raise ArgumentError.new("Region expected, but got #{value.class}") unless value.kind_of?(Region) | |
| 50 | + | |
| 51 | + @region = value | |
| 52 | + self.region_id = value.id | |
| 53 | + end | |
| 54 | + | |
| 55 | + # Rejects the enterprise registration request. | |
| 56 | + def reject | |
| 57 | + cancel | |
| 58 | + end | |
| 59 | + | |
| 60 | + # Approves the enterprise registration request. | |
| 61 | + def approve | |
| 62 | + finish | |
| 63 | + end | |
| 64 | + | |
| 65 | + # actually creates the enterprise after the request is approved. | |
| 66 | + def perform | |
| 67 | + enterprise = Enterprise.new | |
| 68 | + | |
| 69 | + profile_fields = %w[ name identifier contact_phone address region_id ] | |
| 70 | + profile_fields.each do |field| | |
| 71 | + enterprise.send("#{field}=", self.send(field)) | |
| 72 | + end | |
| 73 | + | |
| 74 | + organization_info_data = self.data.delete_if do |key,value| | |
| 75 | + profile_fields.include?(key.to_s) | |
| 76 | + end | |
| 77 | + | |
| 78 | + enterprise.organization_info = OrganizationInfo.new(organization_info_data) | |
| 79 | + enterprise.save! | |
| 80 | + end | |
| 28 | 81 | |
| 29 | 82 | end | ... | ... |
app/models/organization.rb
db/migrate/003_create_profiles.rb
| 1 | 1 | class CreateProfiles < ActiveRecord::Migration |
| 2 | 2 | def self.up |
| 3 | 3 | create_table :profiles do |t| |
| 4 | - t.column :name, :string | |
| 5 | - t.column :type, :string | |
| 6 | - t.column :identifier, :string | |
| 7 | - t.column :environment_id, :integer | |
| 4 | + t.column :name, :string | |
| 5 | + t.column :type, :string | |
| 6 | + t.column :identifier, :string | |
| 7 | + t.column :environment_id, :integer | |
| 8 | 8 | |
| 9 | 9 | |
| 10 | - t.column :active, :boolean, :default => true | |
| 11 | - t.column :address, :string | |
| 12 | - t.column :contact_phone, :string | |
| 10 | + t.column :active, :boolean, :default => true | |
| 11 | + t.column :address, :string | |
| 12 | + t.column :contact_phone, :string | |
| 13 | 13 | |
| 14 | 14 | #person fields |
| 15 | - t.column :user_id, :integer | |
| 15 | + t.column :user_id, :integer | |
| 16 | 16 | |
| 17 | 17 | #enterprise fields |
| 18 | - t.column :validation_entity_id, :integer | |
| 19 | - t.column :approved, :boolean | |
| 18 | + t.column :region_id, :integer | |
| 19 | + | |
| 20 | 20 | end |
| 21 | 21 | end |
| 22 | 22 | ... | ... |
test/fixtures/environments.yml
test/unit/create_enterprise_test.rb
| ... | ... | @@ -34,11 +34,106 @@ class CreateEnterpriseTest < Test::Unit::TestCase |
| 34 | 34 | end |
| 35 | 35 | |
| 36 | 36 | should 'require a target (validator organization)' do |
| 37 | - flunk 'need to write' | |
| 37 | + task = CreateEnterprise.new | |
| 38 | + task.valid? | |
| 39 | + | |
| 40 | + assert task.errors.invalid?(:target_id) | |
| 41 | + task.target = Organization.create!(:name => "My organization", :identifier => 'validator_organization') | |
| 42 | + | |
| 43 | + task.valid? | |
| 44 | + assert !task.errors.invalid?(:target_id) | |
| 45 | + end | |
| 46 | + | |
| 47 | + should 'require that the informed target (validator organization) actually validates for the chosen region' do | |
| 48 | + environment = Environment.create!(:name => "My environment") | |
| 49 | + region = Region.create!(:name => 'My region', :environment_id => environment.id) | |
| 50 | + validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) | |
| 51 | + | |
| 52 | + task = CreateEnterprise.new | |
| 53 | + | |
| 54 | + task.region = region | |
| 55 | + task.target = validator | |
| 56 | + | |
| 57 | + task.valid? | |
| 58 | + assert task.errors.invalid?(:target) | |
| 59 | + | |
| 60 | + region.validators << validator | |
| 61 | + | |
| 62 | + task.valid? | |
| 63 | + assert !task.errors.invalid?(:target) | |
| 64 | + end | |
| 65 | + | |
| 66 | + should 'cancel task when rejected ' do | |
| 67 | + task = CreateEnterprise.new | |
| 68 | + task.expects(:cancel) | |
| 69 | + task.reject | |
| 38 | 70 | end |
| 39 | 71 | |
| 40 | - should 'associate task requestor as enterprise administrator when creating' do | |
| 41 | - flunk 'need to write' | |
| 72 | + should 'finish task when approved' do | |
| 73 | + task = CreateEnterprise.new | |
| 74 | + task.expects(:finish) | |
| 75 | + task.approve | |
| 76 | + end | |
| 77 | + | |
| 78 | + should 'actually create an enterprise when finishing the task' do | |
| 79 | + | |
| 80 | + Environment.destroy_all | |
| 81 | + environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain', :is_default => true) | |
| 82 | + region = Region.create!(:name => 'My region', :environment_id => environment.id) | |
| 83 | + validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) | |
| 84 | + region.validators << validator | |
| 85 | + person = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person | |
| 86 | + | |
| 87 | + task = CreateEnterprise.create!({ | |
| 88 | + :name => 'My new enterprise', | |
| 89 | + :identifier => 'mynewenterprise', | |
| 90 | + :address => 'satan street, 666', | |
| 91 | + :contact_phone => '1298372198', | |
| 92 | + :contact_person => 'random joe', | |
| 93 | + :legal_form => 'cooperative', | |
| 94 | + :economic_activity => 'free software', | |
| 95 | + :region_id => region.id, | |
| 96 | + :requestor_id => person.id, | |
| 97 | + :target_id => validator.id, | |
| 98 | + }) | |
| 99 | + | |
| 100 | + enterprise = Enterprise.new | |
| 101 | + Enterprise.expects(:new).returns(enterprise) | |
| 102 | + | |
| 103 | + task.finish | |
| 104 | + | |
| 105 | + assert !enterprise.new_record? | |
| 106 | + end | |
| 107 | + | |
| 108 | + should 'associate task requestor as enterprise administrator upon enterprise creation' do | |
| 109 | + | |
| 110 | + Environment.destroy_all | |
| 111 | + environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain', :is_default => true) | |
| 112 | + region = Region.create!(:name => 'My region', :environment_id => environment.id) | |
| 113 | + validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) | |
| 114 | + region.validators << validator | |
| 115 | + person = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person | |
| 116 | + | |
| 117 | + task = CreateEnterprise.create!({ | |
| 118 | + :name => 'My new enterprise', | |
| 119 | + :identifier => 'mynewenterprise', | |
| 120 | + :address => 'satan street, 666', | |
| 121 | + :contact_phone => '1298372198', | |
| 122 | + :contact_person => 'random joe', | |
| 123 | + :legal_form => 'cooperative', | |
| 124 | + :economic_activity => 'free software', | |
| 125 | + :region_id => region.id, | |
| 126 | + :requestor_id => person.id, | |
| 127 | + :target_id => validator.id, | |
| 128 | + }) | |
| 129 | + | |
| 130 | + enterprise = Enterprise.new | |
| 131 | + Enterprise.expects(:new).returns(enterprise) | |
| 132 | + | |
| 133 | + task.finish | |
| 134 | + | |
| 135 | + | |
| 136 | + flunk "don't know howt to test it yet" | |
| 42 | 137 | end |
| 43 | 138 | |
| 44 | 139 | end | ... | ... |