Commit c26aa8f8a6d58cf2051e12e639e77d0cb2e9e860
1 parent
2f79d0b4
Exists in
master
and in
23 other branches
ActionItem14: checkpoint, implementing CreateEnterprise
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@676 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
65 additions
and
1 deletions
Show diff stats
app/models/create_enterprise.rb
| 1 | class CreateEnterprise < Task | 1 | class CreateEnterprise < Task |
| 2 | + | ||
| 3 | + DATA_FIELDS = %w[ name identifier address contact_phone contact_person acronym foundation_year legal_form economic_activity management_information ] | ||
| 4 | + | ||
| 5 | + serialize :data, Hash | ||
| 6 | + attr_protected :data | ||
| 7 | + def data | ||
| 8 | + self[:data] ||= Hash.new | ||
| 9 | + end | ||
| 10 | + | ||
| 11 | + DATA_FIELDS.each do |field| | ||
| 12 | + # getter | ||
| 13 | + define_method(field) do | ||
| 14 | + self.data[field.to_sym] | ||
| 15 | + end | ||
| 16 | + # setter | ||
| 17 | + define_method("#{field}=") do |value| | ||
| 18 | + self.data[field.to_sym] = value | ||
| 19 | + end | ||
| 20 | + end | ||
| 21 | + | ||
| 22 | + # checks for virtual attributes | ||
| 23 | + validates_presence_of :name, :identifier, :address, :contact_phone, :contact_person, :legal_form, :economic_activity | ||
| 24 | + validates_format_of :foundation_year, :with => /^\d*$/ | ||
| 25 | + | ||
| 26 | + # checks for actual attributes | ||
| 27 | + validates_presence_of :requestor_id | ||
| 28 | + | ||
| 2 | end | 29 | end |
test/unit/create_enterprise_test.rb
| @@ -2,6 +2,43 @@ require File.dirname(__FILE__) + '/../test_helper' | @@ -2,6 +2,43 @@ require File.dirname(__FILE__) + '/../test_helper' | ||
| 2 | 2 | ||
| 3 | class CreateEnterpriseTest < Test::Unit::TestCase | 3 | class CreateEnterpriseTest < Test::Unit::TestCase |
| 4 | 4 | ||
| 5 | - # TODO: add tests | 5 | + should 'provide needed data' do |
| 6 | + task = CreateEnterprise.new | ||
| 7 | + | ||
| 8 | + %w[ name identifier address contact_phone contact_person acronym foundation_year legal_form economic_activity management_information ].each do |field| | ||
| 9 | + assert task.respond_to?(field) | ||
| 10 | + assert task.respond_to?("#{field}=") | ||
| 11 | + end | ||
| 12 | + end | ||
| 13 | + | ||
| 14 | + should 'accept only numbers as foundation year' do | ||
| 15 | + task = CreateEnterprise.new | ||
| 16 | + | ||
| 17 | + task.foundation_year = "test" | ||
| 18 | + task.valid? | ||
| 19 | + assert task.errors.invalid?(:foundation_year) | ||
| 20 | + | ||
| 21 | + task.foundation_year = 2006 | ||
| 22 | + task.valid? | ||
| 23 | + assert !task.errors.invalid?(:foundation_year) | ||
| 24 | + end | ||
| 25 | + | ||
| 26 | + should 'require a requestor' do | ||
| 27 | + task = CreateEnterprise.new | ||
| 28 | + task.valid? | ||
| 29 | + | ||
| 30 | + assert task.errors.invalid?(:requestor_id) | ||
| 31 | + task.requestor = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person | ||
| 32 | + task.valid? | ||
| 33 | + assert !task.errors.invalid?(:requestor_id) | ||
| 34 | + end | ||
| 35 | + | ||
| 36 | + should 'require a target (validator organization)' do | ||
| 37 | + flunk 'need to write' | ||
| 38 | + end | ||
| 39 | + | ||
| 40 | + should 'associate task requestor as enterprise administrator when creating' do | ||
| 41 | + flunk 'need to write' | ||
| 42 | + end | ||
| 6 | 43 | ||
| 7 | end | 44 | end |