magic_addresses 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +37 -2
  3. data/app/models/magic_addresses/address.rb +3 -11
  4. data/app/models/magic_addresses/association.rb +35 -15
  5. data/app/models/magic_addresses/country.rb +5 -0
  6. data/app/models/magic_addresses/district.rb +4 -0
  7. data/app/models/magic_addresses/state.rb +4 -0
  8. data/app/models/magic_addresses/subdistrict.rb +2 -0
  9. data/lib/generators/magic_addresses/templates/magic_addresses_migration.rb +1 -10
  10. data/lib/generators/magic_addresses/templates/magic_initializer.rb +0 -2
  11. data/lib/helpers/mgca_helper.rb +6 -13
  12. data/lib/magic_addresses/configuration.rb +0 -2
  13. data/lib/magic_addresses/version.rb +1 -1
  14. data/spec/dummy/config/initializers/magic_addresses.rb +0 -2
  15. data/spec/dummy/db/development.sqlite3 +0 -0
  16. data/spec/dummy/db/migrate/{20150223223200_add_magic_addresses.rb → 20150225220219_add_magic_addresses.rb} +1 -10
  17. data/spec/dummy/db/schema.rb +1 -1
  18. data/spec/dummy/db/test.sqlite3 +0 -0
  19. data/spec/dummy/log/development.log +3729 -0
  20. data/spec/dummy/log/test.log +37882 -0
  21. data/spec/models/magic_addresses/address_spec.rb +171 -0
  22. data/spec/models/magic_addresses/association_spec.rb +93 -8
  23. metadata +4 -28
  24. data/app/models/concerns/address_builder.rb +0 -169
  25. data/app/models/concerns/has_address.rb +0 -67
  26. data/app/models/location/address.rb +0 -111
  27. data/app/models/location/city.rb +0 -39
  28. data/app/models/location/country.rb +0 -39
  29. data/app/models/location/district.rb +0 -37
  30. data/app/models/location/state.rb +0 -39
  31. data/app/models/location/subdistrict.rb +0 -38
  32. data/app/services/geo_coder.rb +0 -46
  33. data/lib/generators/magic_addresses/templates/location_addresses_migration.rb +0 -157
  34. data/spec/models/concerns/has_address_spec.rb +0 -92
  35. data/spec/models/location/address_spec.rb +0 -227
  36. data/spec/models/location/city_spec.rb +0 -27
  37. data/spec/models/location/country_spec.rb +0 -26
  38. data/spec/models/location/state_spec.rb +0 -27
  39. data/spec/models/location/subcity_spec.rb +0 -26
  40. data/spec/models/location/subsubcity_spec.rb +0 -27
@@ -203,11 +203,182 @@ describe MagicAddresses::Address do
203
203
  expect( address.translations.all.count ).to eq( 1 )
204
204
  expect( address.translations.where(street_name: [nil, ""]).count ).to eq( 0 )
205
205
 
206
+ # check has_one through connection
207
+ expect( address.magic_subdistrict.country.name ).to eq( "Germany" )
208
+ expect( address.magic_district.country.name ).to eq( "Germany" )
209
+
206
210
  end
207
211
 
208
212
  end
209
213
 
210
214
 
211
215
 
216
+ # => describe 'attribute changes' do
217
+ # => before(:each) do
218
+ # => @address = MagicAddresses::Address.new( )
219
+ # => end
220
+ # =>
221
+ # => it 'saves address childs correct' do
222
+ # => expect( MagicAddresses::Country.count ).to eq 0
223
+ # => expect( MagicAddresses::State.count ).to eq 0
224
+ # => expect( MagicAddresses::City.count ).to eq 0
225
+ # => expect( MagicAddresses::District.count ).to eq 0
226
+ # => expect( MagicAddresses::Subdistrict.count ).to eq 0
227
+ # =>
228
+ # => # expect( I18n.available_locales.count ).to eq 2
229
+ # => expect( I18n::available_locales.count ).to eq 2
230
+ # =>
231
+ # => @address.street = "Heinz-Kapelle-Str."
232
+ # => @address.number = 6
233
+ # => @address.postalcode = 10407
234
+ # => @address.city = "Berlin"
235
+ # =>
236
+ # => # puts @address.translations_attributes.to_yaml
237
+ # =>
238
+ # =>
239
+ # => @address.save
240
+ # =>
241
+ # => expect( MagicAddresses::Country.count ).to eq 1
242
+ # => expect( MagicAddresses::State.count ).to eq 1
243
+ # => expect( MagicAddresses::City.count ).to eq 1
244
+ # => expect( MagicAddresses::District.count ).to eq 1
245
+ # => expect( MagicAddresses::Subdistrict.count ).to eq 1
246
+ # =>
247
+ # => # trigger background task
248
+ # => # @address.build_association_translations
249
+ # =>
250
+ # => expect( @address.magic_city.read_attribute(:name, locale: :en) ).to eq ("Berlin")
251
+ # =>
252
+ # => expect( @address.magic_subdistrict.read_attribute(:name, locale: :en) ).to eq ("Prenzlauer Berg")
253
+ # => expect( @address.magic_district.read_attribute(:name, locale: :en) ).to eq ("Bezirk Pankow")
254
+ # =>
255
+ # => expect( @address.magic_country.read_attribute(:name, locale: :en) ).to eq ("Germany")
256
+ # => expect( @address.magic_country.read_attribute(:name, locale: :de) ).to eq ("Deutschland")
257
+ # =>
258
+ # => expect( @address.street_number ).to eq ("6")
259
+ # =>
260
+ # => # expect( @address.translations.count ).to eq 3
261
+ # => # new model only saves stuff when different than english!
262
+ # => expect( @address.translations.count ).to eq 1
263
+ # =>
264
+ # => # puts @address.translations.map{ |t| "'#{t.street}'"}
265
+ # =>
266
+ # => Rails.application.config.i18n.fallbacks = true
267
+ # => expect( @address.read_attribute(:street_name, locale: :en) ).to eq ("Heinz-Kapelle-Straße")
268
+ # => expect( @address.read_attribute(:street_name, locale: :de) ).to eq ("Heinz-Kapelle-Straße")
269
+ # =>
270
+ # => end
271
+ # =>
272
+ # => it 'saves street languages correct' do
273
+ # => I18n.locale = "de"
274
+ # => @address.street = "Sesam Str."
275
+ # => I18n.locale = "en"
276
+ # => @address.street = "Sesamstreet"
277
+ # => @address.save
278
+ # =>
279
+ # => expect( @address.read_attribute(:street_name, locale: :en) ).to eq ("Sesamstreet")
280
+ # => expect( @address.read_attribute(:street_name, locale: :de) ).to eq ("Sesam Str.")
281
+ # => end
282
+ # =>
283
+ # => # => it 'saves from values correct', sidekiq: :fake do
284
+ # => # => @address.street = "Heinz-Kapelle-Str."
285
+ # => # => @address.number = "6"
286
+ # => # => @address.city = "Berlin"
287
+ # => # => @address.postalcode = "10407"
288
+ # => # => @address.country = "Deutschland"
289
+ # => # => expect( AddressWorker.jobs.size ).to eq 0
290
+ # => # => # => expect {
291
+ # => # => # => @address.save
292
+ # => # => # => sleep 1
293
+ # => # => # => }.to change(AddressWorker.jobs, :size).by(1)
294
+ # => # => expect( AddressWorker ).to receive(:perform_async).with( 1 )
295
+ # => # => @address.save
296
+ # => # => # expect( AddressWorker.jobs.size ).to eq 1
297
+ # => # => # AddressWorker.should have_queued_job(1)
298
+ # => # => # => expect(AddressWorker).to have_enqueued_job( @address.id )
299
+ # => # => #expect(AddressWorker).to have_enqueued_job( @address.id, true )
300
+ # => # => # AddressWorker.should_receive(:perform_async).with( @address.id )
301
+ # => # => expect( @address.fetch_address ).to eq ("Heinz-Kapelle-Str. 6 Berlin 10407 Deutschland")
302
+ # => # => # trigger background task
303
+ # => # => @address.build_association_translations
304
+ # => # => expect( @address.city.read_attribute(:name, locale: :en) ).to eq ("Berlin")
305
+ # => # => expect( @address.subdistrict.read_attribute(:name, locale: :en) ).to eq ("Prenzlauer Berg")
306
+ # => # => expect( @address.district.read_attribute(:name, locale: :en) ).to eq ("Bezirk Pankow")
307
+ # => # => expect( @address.country.read_attribute(:name, locale: :en) ).to eq ("Germany")
308
+ # => # => expect( @address.country.read_attribute(:name, locale: :de) ).to eq ("Deutschland")
309
+ # => # => end
310
+ # =>
311
+ # =>
312
+ # => it 'saves right connections' do
313
+ # =>
314
+ # => expect( MagicAddresses::Country.count ).to eq 0
315
+ # => expect( MagicAddresses::State.count ).to eq 0
316
+ # => expect( MagicAddresses::City.count ).to eq 0
317
+ # => expect( MagicAddresses::District.count ).to eq 0
318
+ # => expect( MagicAddresses::Subdistrict.count ).to eq 0
319
+ # =>
320
+ # => @address.street = "Heinz-Kapelle-Str."
321
+ # => @address.number = 6
322
+ # => @address.postalcode = 10407
323
+ # => @address.city = "Berlin"
324
+ # =>
325
+ # => @address.save
326
+ # => sleep 0.3
327
+ # => # trigger background task
328
+ # => # @address.build_association_translations
329
+ # =>
330
+ # => @address2 = MagicAddresses::Address.new( )
331
+ # => @address2.street = "Grünbergerstr."
332
+ # => @address2.number = 60
333
+ # => @address2.postalcode = 10245
334
+ # => @address2.city = "Berlin"
335
+ # => @address2.save
336
+ # => sleep 0.3
337
+ # => # trigger background task
338
+ # => # @address2.build_association_translations
339
+ # =>
340
+ # => @address3 = MagicAddresses::Address.new( )
341
+ # => @address3.street = "Josef-orlopp-str."
342
+ # => @address3.number = 54
343
+ # => @address3.postalcode = 10365
344
+ # => @address3.city = "Berlin"
345
+ # => @address3.save
346
+ # => sleep 0.3
347
+ # => # trigger background task
348
+ # => # @address3.build_association_translations
349
+ # =>
350
+ # => sleep 1
351
+ # =>
352
+ # => expect( MagicAddresses::Country.count ).to eq 1
353
+ # => expect( MagicAddresses::State.count ).to eq 1
354
+ # => expect( MagicAddresses::City.count ).to eq 1
355
+ # => expect( MagicAddresses::District.count ).to eq 3
356
+ # => expect( MagicAddresses::Subdistrict.count ).to eq 3
357
+ # =>
358
+ # =>
359
+ # => expect( MagicAddresses::City.first.name ).to eq "Berlin"
360
+ # =>
361
+ # => # only save translation when different to "en"
362
+ # => # de: "Berlin"
363
+ # => # en: "Berlin"
364
+ # => # it: "Berlino"
365
+ # => expect( MagicAddresses::City.first.translations.count ).to eq 1
366
+ # =>
367
+ # => expect( @address.translations.count ).to eq 1
368
+ # => expect( @address2.translations.count ).to eq 1
369
+ # => expect( @address3.translations.count ).to eq 1
370
+ # => I18n.locale = "de"
371
+ # => expect( @address.city ).to eq "Berlin"
372
+ # => expect( @address2.city ).to eq "Berlin"
373
+ # => expect( @address3.city ).to eq "Berlin"
374
+ # => I18n.locale = "en"
375
+ # => expect( @address.city ).to eq "Berlin"
376
+ # => expect( @address2.city ).to eq "Berlin"
377
+ # => expect( @address3.city ).to eq "Berlin"
378
+ # => end
379
+ # =>
380
+ # =>
381
+ # => end
382
+
212
383
 
213
384
  end
@@ -1,37 +1,122 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MagicAddresses::Association do
4
+
5
+
4
6
  class TestPerson < ActiveRecord::Base
5
7
  self.table_name = "users"
6
- has_one_address
8
+ has_nested_address
7
9
  end
8
10
 
9
11
  class TestUser < ActiveRecord::Base
10
12
  self.table_name = "users"
13
+ has_one_address
11
14
  end
12
15
 
13
16
  class TestDude < ActiveRecord::Base
14
17
  self.table_name = "users"
15
18
  has_addresses
16
19
  end
20
+
21
+ class TestGuy < ActiveRecord::Base
22
+ self.table_name = "users"
23
+ end
17
24
 
18
25
  describe "has some kind of address => inherit methods" do
19
26
 
20
- let(:micha){ TestPerson.create!(name: "Micha Schmidt") }
21
- let(:ingo){ TestUser.create!(name: "Ingo Mueller") }
22
- let(:dude){ TestDude.create!(name: "The Dude") }
27
+ let(:person){ TestPerson.create!( name: "Bud Spencer" ) }
28
+ let(:user){ TestUser.create!( name: "Terence Hill" ) }
29
+ let(:dude){ TestDude.create!( name: "The Dude" ) }
30
+ let(:guy){ TestGuy.create!( name: "Some Guy" ) }
23
31
 
24
32
 
25
33
  it "add address attributes only if asked for" do
26
- expect( micha.respond_to?(:address) ).to be true
27
- expect( ingo.respond_to?(:address) ).to be false
34
+ expect( person.respond_to?(:address) ).to be true
35
+ expect( user.respond_to?(:address) ).to be true
28
36
  expect( dude.respond_to?(:address) ).to be false
37
+ expect( guy.respond_to?(:address) ).to be false
29
38
 
30
- expect( micha.respond_to?(:addresses) ).to be false
31
- expect( ingo.respond_to?(:addresses) ).to be false
39
+ expect( person.respond_to?(:addresses) ).to be false
40
+ expect( user.respond_to?(:addresses) ).to be false
32
41
  expect( dude.respond_to?(:addresses) ).to be true
42
+ expect( guy.respond_to?(:addresses) ).to be false
33
43
  end
34
44
 
35
45
  end
46
+
47
+ describe "has_one_address => inherit methods" do
48
+
49
+ let(:micha){ TestPerson.create!(name: "Micha Schmidt") }
50
+
51
+ let(:ingo){ TestGuy.create!(name: "Ingo Mueller") }
52
+
53
+ # => it "add address attributes" do
54
+ # => expect( micha.respond_to?(:address) ).to be true
55
+ # => expect( micha.address.new_record? ).to be true
56
+ # => micha.street = "BornholmerStr"
57
+ # => micha.city = "Berlin"
58
+ # => micha.save
59
+ # => expect( micha.address.new_record? ).to be false
60
+ # => expect( micha.street ).to eql "BornholmerStr"
61
+ # => expect( micha.address.street ).to eql "BornholmerStr"
62
+ # => expect( micha.city ).to eql "Berlin"
63
+ # => end
64
+
65
+ it "add address attributes only if asked for" do
66
+ expect( micha.respond_to?(:address) ).to be true
67
+ expect( ingo.respond_to?(:address) ).to be false
68
+ end
69
+
70
+ end
71
+
72
+ # Stack level too deep ???
73
+ # => describe "saves address attributes" do
74
+ # => class TestTheUser < ActiveRecord::Base
75
+ # => self.table_name = "users"
76
+ # => has_nested_address
77
+ # => end
78
+ # =>
79
+ # => # class TestTheCompany < ActiveRecord::Base
80
+ # => # self.table_name = "companies"
81
+ # => # has_one_address
82
+ # => # end
83
+ # =>
84
+ # => let(:user){ TestTheUser.create!(name: "Ingo Mueller") }
85
+ # => # let(:company){ TestTheCompany.create!( name: "Mueller Inc." ) }
86
+ # =>
87
+ # => it "save as own attributes" do
88
+ # => user.street = "BornholmerStr."
89
+ # => user.number = "84"
90
+ # => user.postalcode = "10439"
91
+ # => user.city = "Berlin"
92
+ # => expect( user.valid? ).to eq true
93
+ # => user.save
94
+ # => expect( user.street ).to eq "BornholmerStr."
95
+ # => expect( user.address.street ).to eq "BornholmerStr."
96
+ # => expect( user.number ).to eq "84"
97
+ # => expect( user.address.number ).to eq "84"
98
+ # => expect( user.postalcode ).to eq "10439"
99
+ # => expect( user.address.postalcode ).to eq "10439"
100
+ # => expect( user.city).to eq "Berlin"
101
+ # => expect( user.address.city ).to eq "Berlin"
102
+ # => end
103
+ # =>
104
+ # => it "save as child attributes" do
105
+ # => expect( user.valid? ).to eq true
106
+ # => addr = { street: "Grünberger Str", number: 50, postalcode: 10245, city: "Berlin", country: "Germany" }
107
+ # =>
108
+ # => user.address_attributes = addr
109
+ # => expect( user.valid? ).to eq true
110
+ # =>
111
+ # => expect( user.street ).to eq addr[:street]
112
+ # => expect( user.address.street ).to eq addr[:street]
113
+ # => expect( user.number ).to eq addr[:number]
114
+ # => expect( user.address.number ).to eq addr[:number]
115
+ # => expect( user.postalcode ).to eq addr[:postalcode]
116
+ # => expect( user.address.postalcode ).to eq addr[:postalcode]
117
+ # => expect( user.city ).to eq addr[:city]
118
+ # => expect( user.address.city ).to eq addr[:city]
119
+ # => end
120
+ # => end
36
121
 
37
122
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magic_addresses
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Torsten Wetzel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-24 00:00:00.000000000 Z
11
+ date: 2015-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -122,14 +122,6 @@ files:
122
122
  - app/controllers/magic_addresses/districts_controller.rb
123
123
  - app/controllers/magic_addresses/states_controller.rb
124
124
  - app/controllers/magic_addresses/subdistricts_controller.rb
125
- - app/models/concerns/address_builder.rb
126
- - app/models/concerns/has_address.rb
127
- - app/models/location/address.rb
128
- - app/models/location/city.rb
129
- - app/models/location/country.rb
130
- - app/models/location/district.rb
131
- - app/models/location/state.rb
132
- - app/models/location/subdistrict.rb
133
125
  - app/models/magic_addresses/address.rb
134
126
  - app/models/magic_addresses/association.rb
135
127
  - app/models/magic_addresses/city.rb
@@ -140,7 +132,6 @@ files:
140
132
  - app/models/magic_addresses/subdistrict.rb
141
133
  - app/models/magic_addresses/translator.rb
142
134
  - app/models/magic_addresses.rb
143
- - app/services/geo_coder.rb
144
135
  - app/views/magic_addresses/addresses/_address_fields.html.erb
145
136
  - app/views/magic_addresses/addresses/_addresses_fields.html.erb
146
137
  - app/views/magic_addresses/addresses/fields/_address.html.erb
@@ -155,7 +146,6 @@ files:
155
146
  - lib/generators/magic_addresses/install_generator.rb
156
147
  - lib/generators/magic_addresses/templates/country_seeds.rb
157
148
  - lib/generators/magic_addresses/templates/geocoder_initializer.rb
158
- - lib/generators/magic_addresses/templates/location_addresses_migration.rb
159
149
  - lib/generators/magic_addresses/templates/magic_addresses_migration.rb
160
150
  - lib/generators/magic_addresses/templates/magic_initializer.rb
161
151
  - lib/geocoder/exceptions.rb
@@ -560,7 +550,7 @@ files:
560
550
  - spec/dummy/config.ru
561
551
  - spec/dummy/db/development.sqlite3
562
552
  - spec/dummy/db/migrate/20150213105831_create_users.rb
563
- - spec/dummy/db/migrate/20150223223200_add_magic_addresses.rb
553
+ - spec/dummy/db/migrate/20150225220219_add_magic_addresses.rb
564
554
  - spec/dummy/db/schema.rb
565
555
  - spec/dummy/db/seed_countries.rb
566
556
  - spec/dummy/db/test.sqlite3
@@ -1263,13 +1253,6 @@ files:
1263
1253
  - spec/dummy/tmp/cache/assets/development/sprockets/fbeae887906c8a9e094b9de8fc7d52fe
1264
1254
  - spec/dummy/tmp/cache/assets/development/sprockets/fe322514affb78a8dc2e29095b28d577
1265
1255
  - spec/dummy/tmp/cache/assets/development/sprockets/fe51c58ea609c72d7706c18fd127b049
1266
- - spec/models/concerns/has_address_spec.rb
1267
- - spec/models/location/address_spec.rb
1268
- - spec/models/location/city_spec.rb
1269
- - spec/models/location/country_spec.rb
1270
- - spec/models/location/state_spec.rb
1271
- - spec/models/location/subcity_spec.rb
1272
- - spec/models/location/subsubcity_spec.rb
1273
1256
  - spec/models/magic_addresses/address_spec.rb
1274
1257
  - spec/models/magic_addresses/association_spec.rb
1275
1258
  - spec/models/magic_addresses/city_spec.rb
@@ -1346,7 +1329,7 @@ test_files:
1346
1329
  - spec/dummy/config.ru
1347
1330
  - spec/dummy/db/development.sqlite3
1348
1331
  - spec/dummy/db/migrate/20150213105831_create_users.rb
1349
- - spec/dummy/db/migrate/20150223223200_add_magic_addresses.rb
1332
+ - spec/dummy/db/migrate/20150225220219_add_magic_addresses.rb
1350
1333
  - spec/dummy/db/schema.rb
1351
1334
  - spec/dummy/db/seed_countries.rb
1352
1335
  - spec/dummy/db/test.sqlite3
@@ -2049,13 +2032,6 @@ test_files:
2049
2032
  - spec/dummy/tmp/cache/assets/development/sprockets/fbeae887906c8a9e094b9de8fc7d52fe
2050
2033
  - spec/dummy/tmp/cache/assets/development/sprockets/fe322514affb78a8dc2e29095b28d577
2051
2034
  - spec/dummy/tmp/cache/assets/development/sprockets/fe51c58ea609c72d7706c18fd127b049
2052
- - spec/models/concerns/has_address_spec.rb
2053
- - spec/models/location/address_spec.rb
2054
- - spec/models/location/city_spec.rb
2055
- - spec/models/location/country_spec.rb
2056
- - spec/models/location/state_spec.rb
2057
- - spec/models/location/subcity_spec.rb
2058
- - spec/models/location/subsubcity_spec.rb
2059
2035
  - spec/models/magic_addresses/address_spec.rb
2060
2036
  - spec/models/magic_addresses/association_spec.rb
2061
2037
  - spec/models/magic_addresses/city_spec.rb
@@ -1,169 +0,0 @@
1
- # encoding: utf-8
2
- module AddressBuilder
3
- extend ActiveSupport::Concern
4
-
5
-
6
- included do
7
- before_validation :build_address_associations_if_needed
8
- end
9
-
10
- module ClassMethods
11
- # => ...
12
- end
13
-
14
-
15
- def street_is_changed?
16
- self.street_name.present? && self.street.blank? || self.street && (self.street.to_s.downcase != self.street_name.to_s.downcase.strip)
17
- end
18
-
19
- def city_is_changed?
20
- self.city_name.present? && !self.city || self.city && self.city.name && (self.city.name.to_s.downcase != self.city_name.to_s.downcase.strip)
21
- end
22
-
23
- def country_is_changed?
24
- self.country_name.present? && !self.country || self.country && self.country.name && (self.country.name.to_s.downcase != self.country_name.to_s.downcase.strip)
25
- end
26
-
27
- def country_code_is_changed?
28
- self.country_code.present? && !self.country || self.country && self.country.iso_code && (self.country.iso_code.to_s.downcase != self.country_code.to_s.downcase.strip)
29
- end
30
-
31
- # =====> I N S T A N C E - M E T H O D S <================================================= #
32
- private
33
-
34
-
35
- def build_address_associations_if_needed
36
- dev_log "address ... #{self.owner.class.name.to_s if self.owner} .. #{self.owner_type}"
37
- # build_address_associations() if self.new_record? || self.street_number_changed? || self.zipcode_changed? || city_is_changed? || country_is_changed?
38
- if self.new_record? || self.street_number_changed? || self.zipcode_changed? || street_is_changed? || city_is_changed? || country_is_changed?
39
- dev_log
40
- dev_log "build_address_associations needed !!!"
41
- dev_log
42
- build_address_associations
43
- else
44
- dev_log
45
- dev_log "build_address_associations is not needed .. .. .."
46
- dev_log
47
- end
48
- end
49
-
50
-
51
- def build_address_associations
52
- dev_log "triggered A D D R E S S - B U I L D E R ! - - #{self.street_name} #{self.street_number} #{self.city_name} #{self.zipcode}"
53
-
54
- if self.address.present?
55
- self.fetch_address = self.address
56
- elsif (city_is_changed? || (self.zipcode.present? && self.zipcode_changed?)) && (self.country_name.present? || self.country_code.present?)
57
- self.fetch_address = "#{self.street_name} #{self.street_number} #{self.city_name} #{self.zipcode} #{self.country_name} #{self.country_code}".strip
58
- elsif (city_is_changed? || (self.zipcode.present? && self.zipcode_changed?)) || street_is_changed? || (self.street_number.present? && self.street_number_changed?)
59
- street = self.street if self.street.present?
60
- street = self.street_name if self.street_name.present?
61
- self.fetch_address = "#{street} #{self.street_number} #{self.city_name} #{self.zipcode} #{self.country_name || 'Germany'}".strip
62
- end
63
- geo_data = {}
64
- if self.fetch_address.present?
65
- # => gd = Geocoder.search( self.fetch_address, params: { language: "en" })
66
- gd = GeoCoder.search( self.fetch_address, "de" )
67
- geo_data["de"] = gd.first if gd.first
68
- sleep 0.2
69
- gd = GeoCoder.search( self.fetch_address, "en" )
70
- geo_data["en"] = gd.first if gd.first
71
- end
72
- if geo_data.any?
73
- dev_log " geo_data is present !!!"
74
- # set default values
75
- self.street_default = geo_data["en"].street if geo_data["en"] && geo_data["en"].street
76
- self.zipcode = geo_data["de"].postal_code if geo_data["de"] && geo_data["de"].postal_code
77
- self.street_number = geo_data["de"].street_number if geo_data["de"] && geo_data["de"].street_number
78
- # set geo-position
79
- self.latitude = geo_data["en"].latitude if geo_data["en"] && geo_data["en"].latitude
80
- self.longitude = geo_data["en"].longitude if geo_data["en"] && geo_data["en"].longitude
81
- end
82
- complete_translated_attributes( geo_data )
83
- end
84
-
85
- def complete_translated_attributes( geo_data = {} )
86
- if geo_data.any?
87
- # build street parameters
88
- street_params = []
89
- geo_data.each do |key, stuff|
90
- street_params << { locale: key.to_s, street: stuff.street } if stuff.street && ((stuff.street != geo_data["en"].street) || ["en","de"].include?(key.to_s))
91
- end
92
- # set street parameters if present
93
- if street_params.any?
94
- dev_log "empty street translations"
95
- # reset translations (avoid empty translation in default language)
96
- # => self.translations = []
97
- self.translations.delete_all
98
- # set street parameters
99
- self.translations_attributes = street_params
100
- dev_log "set new street translations"
101
- end
102
-
103
- self.street_default = geo_data["en"].street if geo_data["en"] && geo_data["en"].street
104
-
105
- # build country
106
- if geo_data["de"].country_code.present?
107
- connet_address_association( :country, geo_data )
108
- end
109
- # build state
110
- if geo_data["de"].state_code.present? && self.country
111
- connet_address_association( :state, geo_data )
112
- end
113
- # build city
114
- if geo_data["de"].city.present? && self.country
115
- connet_address_association( :city, geo_data )
116
- end
117
- # build district
118
- if geo_data["de"].district.present? && self.city
119
- connet_address_association( :district, geo_data )
120
- end
121
- # build subdistrict
122
- if geo_data["de"].subdistrict.present? && self.city && self.district
123
- connet_address_association( :subdistrict, geo_data )
124
- end
125
-
126
- end
127
- end
128
-
129
- ## Build Associations
130
-
131
- def connet_address_association( that, geo_data )
132
- dev_log ">>> #{that.to_s.upcase}: #{ geo_data["en"].send(that) }"
133
- that_params = { name_default: geo_data["en"].send(that).to_s }
134
- that_params[ that == :country ? :iso_code : :short_name ] = geo_data["en"].send( "#{that}_code".to_sym ).to_s
135
- that_params.merge!({ country_id: self.country.id }) if [:city, :state].include?(that) && self.country
136
- that_params.merge!({ state_id: self.state.id }) if that == :city && self.state
137
- that_params.merge!({ city_id: self.city.id }) if [:district, :subdistrict].include?(that) && self.city
138
- that_params.merge!({ district_id: self.district.id }) if that == :subdistrict && self.district
139
-
140
- self.attributes = { that => "Location::#{that.to_s.classify}".constantize.unscoped.where( that_params ).first_or_create }# unless self.send(that)
141
-
142
- dev_log "#{that}"
143
-
144
- self.send(that).translations = []
145
- self.send(that).translations_attributes = lng_params( that, geo_data )
146
- self.send(that).save
147
- end
148
-
149
- ## Build Association Params
150
-
151
- def lng_params( that, geo_data )
152
- lng_params = []
153
- geo_data.each do |key, stuff|
154
- dev_log "#{that.to_s.titleize}-Params (#{key}) ... #{stuff.send(that)}"
155
- lng_params << { locale: key.to_s, name: stuff.send(that) } if stuff.send(that) && ((stuff.send(that) != geo_data["en"].send(that)) || (key.to_s == "en"))
156
- end
157
- lng_params
158
- end
159
-
160
- def dev_log( stuff = " " )
161
- if !Rails.env.production?
162
- Rails.logger.info "###"
163
- Rails.logger.info "### #{stuff}"
164
- Rails.logger.info "###"
165
- end
166
- end
167
-
168
-
169
- end
@@ -1,67 +0,0 @@
1
- # encoding: utf-8
2
- module HasAddress
3
-
4
- def self.included(base)
5
- base.send :extend, ClassMethods
6
- end
7
-
8
- ## C L A S S - M E T H O D S # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
9
- module ClassMethods
10
-
11
- def has_addresses
12
- # => send :include, InstanceMethods
13
- has_many :addresses,
14
- :as => :owner,
15
- :class_name => "Location::Address",
16
- :dependent => :destroy
17
- accepts_nested_attributes_for :addresses, allow_destroy: true, reject_if: :all_blank
18
- end
19
-
20
- def has_one_address
21
- has_one :address, -> { where(default: true) },
22
- :as => :owner,
23
- :class_name => "Location::Address",
24
- :autosave => true,
25
- :dependent => :destroy
26
- # => delegate :street, :street_number, :zipcode, :city, :district, :subdistrict, :state, :country,
27
- # => to: :address, allow_nil: true
28
- accepts_nested_attributes_for :address, allow_destroy: true # => , reject_if: :all_blank
29
- end
30
-
31
-
32
-
33
- # => def has_nested_address
34
- # => send :include, InstanceMethods
35
- # => has_one :address,
36
- # => :as => :owner,
37
- # => :class_name => "Address",
38
- # => :autosave => true,
39
- # => :dependent => :destroy
40
- # => delegate :street, :number, :zip, :city, :district, :state, :country,
41
- # => :street=, :number=, :zip=, :city=, :district=, :state=, :country=,
42
- # => to: :address, allow_nil: true
43
- # => accepts_nested_attributes_for :address, allow_destroy: true, reject_if: :all_blank
44
- # => alias_method_chain :address, :build
45
- # => end
46
-
47
- end #> ClassMethods
48
-
49
- ## I N S T A N C E - M E T H O D S # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
50
- module InstanceMethods
51
-
52
- # http://stackoverflow.com/a/4033761
53
- def address_with_build
54
- self.address_without_build || self.build_address
55
- end
56
-
57
- # => def check_for_location_stuff
58
- # => if self.locations && !self.locations.blank?
59
- # => self.locations.each do |key, value|
60
- # => # bla
61
- # => end
62
- # => end
63
- # => end
64
-
65
- end #> InstanceMethods
66
-
67
- end