Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
geraldb committed Jun 3, 2015
1 parent 1cb566c commit 8350450
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 26 deletions.
16 changes: 16 additions & 0 deletions TODO.md
@@ -0,0 +1,16 @@
# TODOS


in models/city.rb - fix:

~~~
values.each_with_index do |value,index|
### fix,fix,fix: use match_state(s)_for_country - where defined - check!!!
## defined in textutils/helper/value_helper!!!
## move back to worlddb-models!!!!!!
if match_region_for_country( value, new_attributes[:country_id] ) do |state|
new_attributes[ :state_id ] = state.id
end
~~~


5 changes: 4 additions & 1 deletion lib/worlddb/models/city.rb
Expand Up @@ -147,7 +147,10 @@ def self.create_or_update_from_attribs( new_attributes, values, opts={} )
## check for optional values

values.each_with_index do |value,index|
if match_state_for_country( value, new_attributes[:country_id] ) do |state|
### fix,fix,fix: use match_state(s)_for_country - where defined - check!!!
## defined in textutils/helper/value_helper!!!
## move back to worlddb-models!!!!!!
if match_region_for_country( value, new_attributes[:country_id] ) do |state|
new_attributes[ :state_id ] = state.id
end
elsif match_country( value ) do |country|
Expand Down
4 changes: 2 additions & 2 deletions lib/worlddb/models/state_compat.rb
Expand Up @@ -19,12 +19,12 @@ def synonyms=(value) self.alt_names = value; end

def title_w_synonyms( opts={} ) all_names( opts ); end # depreciated: use all_names instead

## has_many :regions, class_name: 'State', foreign_key: 'state_id' ## subregions
has_many :regions, class_name: 'State', foreign_key: 'state_id' ## subregions

end # class State

### add "old" alias for State class
## Region = State
Region = State

end # module Model
end # module WorldDb
15 changes: 9 additions & 6 deletions lib/worlddb/reader.rb
Expand Up @@ -109,8 +109,9 @@ def load( name )
end
logger.debug "State (Adm2) #{adm2.key} >#{adm2.title}<"

r = create_state_reader( name, country_id: country.id, state_id: adm2.id, level:3, c:true )
r.read()
st = create_state_reader( name, country_id: country.id, state_id: adm2.id, level:3 )
## was: r = create_state_reader( name, country_id: country.id, state_id: adm2.id, level:3, c:true )
st.read()
end
elsif match_adm2_for_country( name ) do |country_key,state_key|
## auto-add required country code (from folder structure)
Expand All @@ -119,17 +120,19 @@ def load( name )
state = State.find_by_key_and_country_id!( state_key, country.id )
logger.debug "State (Adm1) #{state.key} >#{state.title}<"

r = create_state_reader( name, country_id: country.id, state_id: state.id, level:2, d:true )
r.read()
st = create_state_reader( name, country_id: country.id, state_id: state.id, level:2 )
## was: r = create_state_reader( name, country_id: country.id, state_id: state.id, level:2, d:true )
st.read()
end
### fix: change to match_adm1_for_country()
elsif match_states_for_country( name ) do |country_key| # name =~ /\/([a-z]{2})\/states/
## auto-add required country code (from folder structure)
country = Country.find_by_key!( country_key )
logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"

r = create_state_reader( name, country_id: country.id, state_id: nil, level:1, s:true )
r.read()
st = create_state_reader( name, country_id: country.id, state_id: nil, level:1 )
## was: r = create_state_reader( name, country_id: country.id, state_id: nil, level:1, s:true )
st.read()
end
else
logger.error "unknown world.db fixture type >#{name}<"
Expand Down
6 changes: 3 additions & 3 deletions test/test_models.rb
Expand Up @@ -6,7 +6,7 @@
class TestModels < MiniTest::Test

def setup
# delete all countries, regions, cities in in-memory only db
# delete all countries, states, cities in in-memory only db
WorldDb.delete!
end

Expand All @@ -15,7 +15,7 @@ def test_counts
assert_equal 0, Place.count
assert_equal 0, Continent.count
assert_equal 0, Country.count
assert_equal 0, Region.count
assert_equal 0, State.count
assert_equal 0, City.count

assert_equal 0, Lang.count
Expand All @@ -27,7 +27,7 @@ def test_counts
def test_place_assoc_counts
# assert_equal 0, Continent.new.place.count
# assert_equal 0, Country.new.place.count
# assert_equal 0, Region.new.place.count
# assert_equal 0, State.new.place.count
# assert_equal 0, City.new.place.count
end

Expand Down
28 changes: 14 additions & 14 deletions test/test_read_adm.rb
Expand Up @@ -10,7 +10,7 @@
class TestReadAdm < MiniTest::Test

def setup
# delete all countries, regions, cities in in-memory only db
# delete all countries, states, cities in in-memory only db
WorldDb.delete!
end

Expand All @@ -26,7 +26,7 @@ def test_at

###
## todo/fix: shorten level to l -- why, why not???
assert_equal 9, at.regions.where(level:1).count
assert_equal 9, at.states.where(level:1).count
end


Expand All @@ -44,24 +44,24 @@ def test_de
###
## todo/fix: shorten level to l -- why, why not???

assert_equal 16, de.regions.where(level:1).count
## assert_equal 16+??, de.regions.count
assert_equal 16, de.states.where(level:1).count
## assert_equal 16+??, de.states.count

by = Region.find_by_key!( 'by' )
assert_equal 7, by.regions.where(level:2).count
## assert_equal 7+??, by.regions.count
by = State.find_by_key!( 'by' )
assert_equal 7, by.states.where(level:2).count
## assert_equal 7+??, by.states.count

## pp by.regions
## pp by.states

ob = Region.find_by_key!( 'ob' )
ob = State.find_by_key!( 'ob' )
assert_equal 'Oberbayern', ob.name
assert_equal 23, ob.regions.where(level:3).count
assert_equal 23, ob.regions.count
assert_equal 23, ob.states.where(level:3).count
assert_equal 23, ob.states.count

ofr = Region.find_by_key!( 'ofr' )
ofr = State.find_by_key!( 'ofr' )
assert_equal 'Oberfranken', ofr.name
assert_equal 13, ofr.regions.where(level:3).count
assert_equal 13, ofr.regions.count
assert_equal 13, ofr.states.where(level:3).count
assert_equal 13, ofr.states.count
end

end # class TestReadAdm

0 comments on commit 8350450

Please sign in to comment.