From 66331e33c053587eeb3a5cd5ee92f99087f9f9cd Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Fri, 12 Oct 2007 21:52:51 +0000 Subject: [PATCH] ActionItem14: checkpoint; I'm getting there --- app/controllers/profile_admin/membership_editor_controller.rb | 1 - app/models/create_enterprise.rb | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- app/models/organization.rb | 2 ++ db/migrate/003_create_profiles.rb | 20 ++++++++++---------- test/fixtures/environments.yml | 2 ++ test/unit/create_enterprise_test.rb | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 6 files changed, 168 insertions(+), 17 deletions(-) diff --git a/app/controllers/profile_admin/membership_editor_controller.rb b/app/controllers/profile_admin/membership_editor_controller.rb index c9d9608..56e3775 100644 --- a/app/controllers/profile_admin/membership_editor_controller.rb +++ b/app/controllers/profile_admin/membership_editor_controller.rb @@ -20,7 +20,6 @@ class MembershipEditorController < ProfileAdminController redirect_to :action => 'index' else flash[:notice] = _('Enterprise was not created') - @vitual_communities = Environment.find(:all) @validation_entities = Organization.find(:all) render :action => 'register_form' end diff --git a/app/models/create_enterprise.rb b/app/models/create_enterprise.rb index 17905b7..2f393dd 100644 --- a/app/models/create_enterprise.rb +++ b/app/models/create_enterprise.rb @@ -1,6 +1,6 @@ class CreateEnterprise < Task - DATA_FIELDS = %w[ name identifier address contact_phone contact_person acronym foundation_year legal_form economic_activity management_information ] + DATA_FIELDS = %w[ name identifier address contact_phone contact_person acronym foundation_year legal_form economic_activity management_information region_id ] serialize :data, Hash attr_protected :data @@ -20,10 +20,63 @@ class CreateEnterprise < Task end # checks for virtual attributes - validates_presence_of :name, :identifier, :address, :contact_phone, :contact_person, :legal_form, :economic_activity + validates_presence_of :name, :identifier, :address, :contact_phone, :contact_person, :legal_form, :economic_activity, :region_id validates_format_of :foundation_year, :with => /^\d*$/ # checks for actual attributes - validates_presence_of :requestor_id + validates_presence_of :requestor_id, :target_id + + def validate + if self.region && self.target + unless self.region.validators.include?(self.target) + self.errors.add(:target, '%{fn} is not a validator for the chosen region') + end + end + end + + # gets the associated region for the enterprise creation + def region(reload = false) + if self.region_id + if reload || @region.nil? + @region = Region.find(self.region_id) + end + end + @region + end + + # sets the associated region for the enterprise creation + def region=(value) + raise ArgumentError.new("Region expected, but got #{value.class}") unless value.kind_of?(Region) + + @region = value + self.region_id = value.id + end + + # Rejects the enterprise registration request. + def reject + cancel + end + + # Approves the enterprise registration request. + def approve + finish + end + + # actually creates the enterprise after the request is approved. + def perform + enterprise = Enterprise.new + + profile_fields = %w[ name identifier contact_phone address region_id ] + profile_fields.each do |field| + enterprise.send("#{field}=", self.send(field)) + end + + organization_info_data = self.data.delete_if do |key,value| + profile_fields.include?(key.to_s) + end + + enterprise.organization_info = OrganizationInfo.new(organization_info_data) + enterprise.save! + end end diff --git a/app/models/organization.rb b/app/models/organization.rb index 9d8d0c0..7d3e518 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -3,6 +3,8 @@ class Organization < Profile has_one :organization_info has_many :validated_enterprises, :class_name => 'enterprise' + belongs_to :region + # def info # organization_info # end diff --git a/db/migrate/003_create_profiles.rb b/db/migrate/003_create_profiles.rb index 646ddeb..e90b432 100644 --- a/db/migrate/003_create_profiles.rb +++ b/db/migrate/003_create_profiles.rb @@ -1,22 +1,22 @@ class CreateProfiles < ActiveRecord::Migration def self.up create_table :profiles do |t| - t.column :name, :string - t.column :type, :string - t.column :identifier, :string - t.column :environment_id, :integer + t.column :name, :string + t.column :type, :string + t.column :identifier, :string + t.column :environment_id, :integer - t.column :active, :boolean, :default => true - t.column :address, :string - t.column :contact_phone, :string + t.column :active, :boolean, :default => true + t.column :address, :string + t.column :contact_phone, :string #person fields - t.column :user_id, :integer + t.column :user_id, :integer #enterprise fields - t.column :validation_entity_id, :integer - t.column :approved, :boolean + t.column :region_id, :integer + end end diff --git a/test/fixtures/environments.yml b/test/fixtures/environments.yml index 4228f5f..03660a1 100644 --- a/test/fixtures/environments.yml +++ b/test/fixtures/environments.yml @@ -2,8 +2,10 @@ colivre_net: id: 1 name: 'Colivre.net' + contact_email: 'colivre@localhost.localdomain' is_default: true anhetegua_net: id: 2 name: 'Anheteguá' is_default: false + contact_email: 'anhetegua@localhost.localdomain' diff --git a/test/unit/create_enterprise_test.rb b/test/unit/create_enterprise_test.rb index 2b8c850..80dbf98 100644 --- a/test/unit/create_enterprise_test.rb +++ b/test/unit/create_enterprise_test.rb @@ -34,11 +34,106 @@ class CreateEnterpriseTest < Test::Unit::TestCase end should 'require a target (validator organization)' do - flunk 'need to write' + task = CreateEnterprise.new + task.valid? + + assert task.errors.invalid?(:target_id) + task.target = Organization.create!(:name => "My organization", :identifier => 'validator_organization') + + task.valid? + assert !task.errors.invalid?(:target_id) + end + + should 'require that the informed target (validator organization) actually validates for the chosen region' do + environment = Environment.create!(:name => "My environment") + region = Region.create!(:name => 'My region', :environment_id => environment.id) + validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) + + task = CreateEnterprise.new + + task.region = region + task.target = validator + + task.valid? + assert task.errors.invalid?(:target) + + region.validators << validator + + task.valid? + assert !task.errors.invalid?(:target) + end + + should 'cancel task when rejected ' do + task = CreateEnterprise.new + task.expects(:cancel) + task.reject end - should 'associate task requestor as enterprise administrator when creating' do - flunk 'need to write' + should 'finish task when approved' do + task = CreateEnterprise.new + task.expects(:finish) + task.approve + end + + should 'actually create an enterprise when finishing the task' do + + Environment.destroy_all + environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain', :is_default => true) + region = Region.create!(:name => 'My region', :environment_id => environment.id) + validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) + region.validators << validator + person = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person + + task = CreateEnterprise.create!({ + :name => 'My new enterprise', + :identifier => 'mynewenterprise', + :address => 'satan street, 666', + :contact_phone => '1298372198', + :contact_person => 'random joe', + :legal_form => 'cooperative', + :economic_activity => 'free software', + :region_id => region.id, + :requestor_id => person.id, + :target_id => validator.id, + }) + + enterprise = Enterprise.new + Enterprise.expects(:new).returns(enterprise) + + task.finish + + assert !enterprise.new_record? + end + + should 'associate task requestor as enterprise administrator upon enterprise creation' do + + Environment.destroy_all + environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain', :is_default => true) + region = Region.create!(:name => 'My region', :environment_id => environment.id) + validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) + region.validators << validator + person = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person + + task = CreateEnterprise.create!({ + :name => 'My new enterprise', + :identifier => 'mynewenterprise', + :address => 'satan street, 666', + :contact_phone => '1298372198', + :contact_person => 'random joe', + :legal_form => 'cooperative', + :economic_activity => 'free software', + :region_id => region.id, + :requestor_id => person.id, + :target_id => validator.id, + }) + + enterprise = Enterprise.new + Enterprise.expects(:new).returns(enterprise) + + task.finish + + + flunk "don't know howt to test it yet" end end -- libgit2 0.21.2