Commit 526cca84087f50d48b66afe326ea940f5b43925b

Authored by Daniela Feitosa
1 parent 4d0beccb
Exists in master and in 1 other branch mezuro_spb

Validate format of siorg_code and add test

src/noosfero-spb/gov_user/lib/institution.rb
... ... @@ -46,18 +46,11 @@ class Institution < ActiveRecord::Base
46 46 }
47 47  
48 48 validate :validate_country, :validate_state, :validate_city,
49   - :verify_institution_type, :verify_siorg_code
  49 + :verify_institution_type
50 50  
51   - def verify_siorg_code
52   - if (self.siorg_code.present? && (self.siorg_code =~ /^[0-9]+$/).nil?)
53   - self.errors.add(
54   - :siorg_code,
55   - _("invalid, only numbers are allowed.")
56   - )
57   - return false
58   - end
59   - true
60   - end
  51 + validates :siorg_code,
  52 + format: {with: /\A[0-9]+\z/, message: _("invalid, only numbers are allowed.")},
  53 + allow_blank: true
61 54  
62 55 def has_accepted_rating? user_rating
63 56 rating_ids = OrganizationRating.where(institution_id: self.id, organization_id: user_rating.organization_id).map(&:id)
... ...
src/noosfero-spb/gov_user/test/unit/institution_test.rb
... ... @@ -73,4 +73,19 @@ class InstitutionTest < ActiveSupport::TestCase
73 73 assert_includes search_result_env1, @institution.id
74 74 assert_not_includes search_result_env2, @institution.id
75 75 end
  76 +
  77 + should "not save if siorg_code is not a number" do
  78 + @institution.siorg_code = "not a number"
  79 + assert_equal false, @institution.save
  80 +
  81 + @institution.siorg_code = "1111111"
  82 + assert_equal true, @institution.save
  83 + end
  84 +
  85 + should "validate siorg_code if it is blank" do
  86 + @institution.siorg_code = ""
  87 + assert_equal true, @institution.save
  88 + end
  89 +
  90 +
76 91 end
... ...