Commit d35abd3152757a131d5a1917030a030e71861f9e
1 parent
74589b8f
Exists in
master
and in
22 other branches
rails: add City/State attr_accessible and fix test
Showing
3 changed files
with
26 additions
and
5 deletions
Show diff stats
app/models/city.rb
app/models/state.rb
test/unit/set_profile_region_from_city_state_test.rb
| @@ -3,8 +3,8 @@ require File.dirname(__FILE__) + '/../test_helper' | @@ -3,8 +3,8 @@ require File.dirname(__FILE__) + '/../test_helper' | ||
| 3 | class SetProfileRegionFromCityStateTest < ActiveSupport::TestCase | 3 | class SetProfileRegionFromCityStateTest < ActiveSupport::TestCase |
| 4 | 4 | ||
| 5 | should 'set city and state from names' do | 5 | should 'set city and state from names' do |
| 6 | - s = State.create!(:name => 'Sao Paulo', :acronym => 'SP', :environment_id => Environment.default.id) | ||
| 7 | - c = City.create!(:name => 'Pindamonhangaba', :parent_id => s.id, :environment_id => Environment.default.id) | 6 | + c, _ = create_city_in_state('Pindamonhangaba', 'Sao Paulo', 'SP') |
| 7 | + | ||
| 8 | p = fast_create(Person, :user_id => fast_create(User).id) | 8 | p = fast_create(Person, :user_id => fast_create(User).id) |
| 9 | p.state_with_region = 'SP' | 9 | p.state_with_region = 'SP' |
| 10 | p.city_with_region = 'Pindamonhangaba' | 10 | p.city_with_region = 'Pindamonhangaba' |
| @@ -13,7 +13,8 @@ class SetProfileRegionFromCityStateTest < ActiveSupport::TestCase | @@ -13,7 +13,8 @@ class SetProfileRegionFromCityStateTest < ActiveSupport::TestCase | ||
| 13 | end | 13 | end |
| 14 | 14 | ||
| 15 | should 'set region to null if city not found' do | 15 | should 'set region to null if city not found' do |
| 16 | - s = State.create!(:name => 'Sao Paulo', :acronym => 'SP', :environment_id => Environment.default.id) | 16 | + create_city_in_state(nil, 'Sao Paulo', 'SP') |
| 17 | + | ||
| 17 | p = fast_create(Person, :user_id => fast_create(User).id) | 18 | p = fast_create(Person, :user_id => fast_create(User).id) |
| 18 | p.state_with_region = 'SP' | 19 | p.state_with_region = 'SP' |
| 19 | p.city_with_region = 'Pindamonhangaba' | 20 | p.city_with_region = 'Pindamonhangaba' |
| @@ -22,12 +23,30 @@ class SetProfileRegionFromCityStateTest < ActiveSupport::TestCase | @@ -22,12 +23,30 @@ class SetProfileRegionFromCityStateTest < ActiveSupport::TestCase | ||
| 22 | end | 23 | end |
| 23 | 24 | ||
| 24 | should 'set region to null if state not found' do | 25 | should 'set region to null if state not found' do |
| 25 | - s = State.create!(:name => 'Sao Paulo', :acronym => 'SP', :environment_id => Environment.default.id) | ||
| 26 | - c = City.create!(:name => 'Pindamonhangaba', :parent_id => s.id, :environment_id => Environment.default.id) | 26 | + create_city_in_state('Pindamonhangaba', 'Sao Paulo', 'SP') |
| 27 | + | ||
| 27 | p = fast_create(Person, :user_id => fast_create(User).id) | 28 | p = fast_create(Person, :user_id => fast_create(User).id) |
| 28 | p.state_with_region = 'RJ' | 29 | p.state_with_region = 'RJ' |
| 29 | p.city_with_region = 'Pindamonhangaba' | 30 | p.city_with_region = 'Pindamonhangaba' |
| 30 | p.save! | 31 | p.save! |
| 31 | assert p.region.nil? | 32 | assert p.region.nil? |
| 32 | end | 33 | end |
| 34 | + | ||
| 35 | + def create_city_in_state(city_name, state_name, state_acronym) | ||
| 36 | + environment_id = Environment.default.id | ||
| 37 | + | ||
| 38 | + state = State.new(:name => state_name, :acronym => state_acronym) | ||
| 39 | + state.environment_id = environment_id | ||
| 40 | + state.save! | ||
| 41 | + | ||
| 42 | + city = nil | ||
| 43 | + if city_name | ||
| 44 | + city = City.new(:name => city_name, :parent_id => state.id) | ||
| 45 | + city.environment_id = environment_id | ||
| 46 | + city.save! | ||
| 47 | + end | ||
| 48 | + | ||
| 49 | + return [city, state] | ||
| 50 | + end | ||
| 51 | + | ||
| 33 | end | 52 | end |