magic_addresses 0.0.42 → 0.0.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YWM1OGVmYWMxNzU3MDQ1MDY5ZjA0ZjExYzYxYzc0NDczNTA4OTZjZQ==
5
- data.tar.gz: !binary |-
6
- ZTU0YjRmZGE3NjY4NDZkNGRkMjFmNWYwY2EwZWNmYjVlMTFjNDBkNA==
2
+ SHA1:
3
+ metadata.gz: 846a852f80f648ff9d56dccd47e93e833b7d0cd9
4
+ data.tar.gz: efa7826e64ff4c6650217c0f96f692f7594db2f9
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZjFkOGIzYWYxYmVhNWU2NTUzYWRkNWIyNzgzZjNiODcwNTRhZWFlZGI2MzFl
10
- ODhjY2IwOTlkZTc0ZGE5YTJlNjQ4ODlmM2U0MmJiYzJiNmFmNTk4ZDAwYmU5
11
- ZDRiNzFkMGM5MTdmODczNTdlNWUyMzQzNTBjOGEyNDIyNjcwOTM=
12
- data.tar.gz: !binary |-
13
- NDA5MDdmMGZkNDBkYTY0N2ZmY2E4OThhMWRjZWFkMzIxNWEyZWQ5ZGU5M2Fi
14
- MzM0MTIxODU0MWE3YzkzYjE1NjNkNDBhODNiZTgzNTAzMWVhNzZiNWRhMjlh
15
- ZmNhY2M4OTc0ODIyMDBlNjZmYzg3ZjczOTM1MGY2YmYzYTVjOWI=
6
+ metadata.gz: 3cc46873a9cb142446e57ebb55a784370cc1133ab9610e25dc581c71dcd2932f4cd61a99fb2f021ddd43a772f10b9e806dc467580d1530cd1b3f533cbea750ef
7
+ data.tar.gz: 33c83e8709a761cf493b0795bb1c91d6d198e3b998a903ab5d41c38962ee975abebe851ad55ac711bf7d5b1d28342a92d62e0c873fba60cd82b451e1469c6f9c
@@ -40,6 +40,8 @@ class MagicAddresses::Address < ActiveRecord::Base
40
40
 
41
41
  serialize :fetch_address, Hash
42
42
 
43
+
44
+ # fetch getter and setter
43
45
  %w[fetch_street fetch_number fetch_city fetch_zipcode fetch_country fetch_country_code].each do |key|
44
46
  define_method(key) do
45
47
  fetch_address && fetch_address[key]
@@ -52,60 +54,85 @@ class MagicAddresses::Address < ActiveRecord::Base
52
54
  def street
53
55
  fetch_address && fetch_address["fetch_street"] || street_name
54
56
  end
55
- def street=(value)
56
- self.street_name = value
57
- self.fetch_address = (fetch_address || {}).merge("fetch_street" => value)
58
- end
59
57
  # attr_accessor :number (:street_number)
60
58
  def number
61
59
  fetch_address && fetch_address["fetch_number"] || street_number
62
60
  end
63
- def number=(value)
64
- self.fetch_address = (fetch_address || {}).merge("fetch_number" => value)
65
- end
66
61
  # attr_accessor :postalcode (:zipcode)
67
62
  def postalcode
68
63
  fetch_address && fetch_address["fetch_zipcode"] || zipcode
69
64
  end
70
- def postalcode=(value)
71
- self.fetch_address = (fetch_address || {}).merge("fetch_zipcode" => value)
65
+ # attr_accessor :country_code
66
+ def country_code
67
+ fetch_address && fetch_address["fetch_country_code"] || self.send("magic_country") && self.send("magic_country").iso_code
72
68
  end
73
-
69
+ # more getter
74
70
  %w[country state city district subdistrict].each do |key|
75
71
  # attr_accessor key
76
72
  define_method(key) do
77
73
  fetch_address && fetch_address["fetch_#{key}"] || self.send("magic_#{key}") && self.send("magic_#{key}").name
78
74
  end
75
+ end
76
+ # settter methods
77
+ %w[country state city district subdistrict street number postalcode country_code].each do |key|
79
78
  define_method("#{key}=") do |value|
80
- # self["fetch_#{key}"] = value
81
- self.fetch_address = (fetch_address || {}).merge("fetch_#{key}" => value)
79
+ self.street_name = value if key == "street"
80
+ self.fetch_address = (fetch_address || {}).merge("fetch_#{ key == 'postalcode' ? 'zipcode' : key }" => value)
82
81
  end
83
82
  end
84
-
85
- def country_code
86
- fetch_address && fetch_address["fetch_country_code"] || self.send("magic_country") && self.send("magic_country").iso_code
87
- end
88
- def country_code=(value)
89
- self.fetch_address = (fetch_address || {}).merge("fetch_country_code" => value)
90
- end
91
-
92
- def owner=(this)
93
- self.addressibles.where( owner_type: this.class.to_s, owner_id: this.id ).first_or_create!
94
- end
95
-
96
-
97
-
83
+
84
+
85
+
98
86
  # =====> C A L L B A C K S <=============================================================== #
99
87
  ## now in addressible or doesnt work nested ..
100
88
  # => after_save :build_address_associations_if_needed
101
89
  # => after_create :build_address_associations_if_needed
102
-
90
+
91
+
103
92
  # =====> S C O P E S <===================================================================== #
104
-
93
+
94
+
105
95
  # =====> C L A S S - M E T H O D S <======================================================= #
96
+ def self.get_one( owner, params )
97
+ # => that = self.new( city: params[:city], postalcode: params[:postalcode], street: params[:street], number: params[:number], country: params[:country] )
98
+ # => that.owner = owner
99
+ # => that.save
100
+ # => that.trigger_build_address_associations
101
+ # puts "> get_one( #{ owner.class }, #{params} )"
102
+ if params[:id].present? && self.find( params[:id].to_i )
103
+ current = self.find( params[:id].to_i )
104
+ # if params[:postalcode].present? || params[:postalcode].present? || params[:postalcode].present? || params[:postalcode].present?
105
+ checker = []
106
+ [:street, :number, :postalcode, :city, :country].each do |prm|
107
+ if params[prm].present? && ( "#{ params[prm] }" != "#{ current.send("#{prm}") }" )
108
+ checker << true
109
+ puts "#{prm}: #{ params[prm] } != #{ current.send("#{prm}") }"
110
+ end
111
+ end
112
+ if checker.include?( true )
113
+ that = self.new( city: params[:city], postalcode: params[:postalcode], street: params[:street], number: params[:number], country: params[:country] )
114
+ that.trigger_build_address_associations
115
+ else
116
+ current
117
+ end
118
+ else
119
+ if params[:postalcode].present? && params[:city].present?
120
+ that = self.new( city: params[:city], postalcode: params[:postalcode], street: params[:street], number: params[:number], country: params[:country] )
121
+ that.trigger_build_address_associations
122
+ else
123
+ nil
124
+ end
125
+ end
126
+ end
127
+
106
128
 
107
129
  # =====> I N S T A N C E - M E T H O D S <================================================= #
108
130
 
131
+ def update_fetch_address( atts = {} )
132
+ self.fetch_address = (self.fetch_address || {}).merge( atts )
133
+ end
134
+
135
+
109
136
  def presentation( style = "full", type = "inline" )
110
137
  adr = []
111
138
  adr << "#{street} #{number}".strip if street.present?
@@ -126,6 +153,10 @@ class MagicAddresses::Address < ActiveRecord::Base
126
153
  addressibles.map { |that| ::MagicAddresses::OwnerProxy.new( that ) }
127
154
  end
128
155
 
156
+ def owner=(this)
157
+ self.addressibles.where( owner_type: this.class.to_s, owner_id: this.id ).first_or_create!
158
+ end
159
+
129
160
  def touch_owners
130
161
  addressibles.map { |that| that.owner.touch }
131
162
  end
@@ -152,9 +183,7 @@ private
152
183
  end
153
184
 
154
185
 
155
- def build_address_associations
156
- dev_log "triggered A D D R E S S - B U I L D E R ! - - #{self.street} #{self.number} #{self.city} #{self.zipcode}"
157
-
186
+ def build_fetch_address_addr
158
187
  if self.fetch_city.present? || self.fetch_zipcode.present?
159
188
  self.fetch_address["addr"] = []
160
189
  # MagicAddresses::GeoCoder
@@ -166,8 +195,10 @@ private
166
195
  self.fetch_address["addr"] << "#{self.fetch_country || self.fetch_country_code}".strip if self.fetch_country || self.fetch_country
167
196
  end
168
197
  end
169
-
170
-
198
+ end
199
+
200
+
201
+ def build_fetch_address_geo_data
171
202
  if self.fetch_address["addr"] && self.fetch_address["addr"].any?
172
203
  self.fetch_address["geo_data"] = {}
173
204
  gd = MagicAddresses::GeoCoder.search(self.fetch_address["addr"].join(", "), default_locale)
@@ -175,55 +206,76 @@ private
175
206
  self.fetch_address["fetched"] = true
176
207
  self.status = "fetched"
177
208
  end
209
+ end
210
+
211
+ def set_address_defaults( geo_data )
212
+ if geo_data
213
+ self.street_default = geo_data.street if geo_data.street
214
+ self.zipcode = geo_data.postal_code if geo_data.postal_code
215
+ self.street_number = geo_data.street_number if geo_data.street_number
216
+ # set geo-position
217
+ self.latitude = geo_data.latitude if geo_data.latitude
218
+ self.longitude = geo_data.longitude if geo_data.longitude
219
+ end
220
+ end
221
+
222
+
223
+ def build_address_associations
224
+ dev_log "- - - - - - - - - - - - - - - - -"
225
+ dev_log " A D D R E S S - B U I L D E R ! -> #{self.street} #{self.number} #{self.city} #{self.postalcode}"
178
226
 
227
+ build_fetch_address_addr( )
228
+
229
+ build_fetch_address_geo_data( )
179
230
 
180
231
  if self.fetch_address["geo_data"] && self.fetch_address["geo_data"].any?
181
232
  geo_data = self.fetch_address["geo_data"][default_locale]
182
- sames = MagicAddresses::Address.where( latitude: geo_data.latitude,
183
- longitude: geo_data.longitude,
184
- zipcode: geo_data.postal_code,
185
- street_number: geo_data.street_number )
186
- dev_log "Address.count: #{ MagicAddresses::Address.all.count } .. . .. SAMES.count: #{ sames.count }"
187
- if sames.any? && sames.first != self
188
- dev_log " found similar address .. will use that !!!"
189
- dev_log "addressibles #{self.addressibles.count}"
233
+ if self.id && self.id.present?
234
+ loockups = MagicAddresses::Address.where.not( id: self.id )
235
+ else
236
+ loockups = MagicAddresses::Address.unscoped
237
+ end
238
+ sames = loockups.where(
239
+ latitude: geo_data.latitude, longitude: geo_data.longitude,
240
+ zipcode: geo_data.postal_code, street_default: geo_data.street, street_number: geo_data.street_number
241
+ )
242
+ if sames.any?
243
+ dev_log "! found similar address .. will use that !"
190
244
  that = sames.first
191
- dev_log "owners - #{self.owners.inspect}"
192
- self.addressibles.each do |x|
193
- dev_log "kill - #{x.address_id}"
194
- x.address_id = that.id
195
- x.save
196
- # x.owner.touch
197
- dev_log "kill - #{x.address_id}"
198
- end
245
+ self.addressibles.map{ |x| x.update_column(:address_id, that.id) }
199
246
  self.destroy
200
-
247
+ dev_log
201
248
  ## inform address owners
202
249
  that.touch_owners
250
+ ## return address
251
+ that
203
252
  else
204
- dev_log " geo_data is present !!!"
205
- self.street_default = geo_data.street if geo_data && geo_data.street
206
- self.zipcode = geo_data.postal_code if geo_data && geo_data.postal_code
207
- self.street_number = geo_data.street_number if geo_data && geo_data.street_number
208
- # set geo-position
209
- self.latitude = geo_data.latitude if geo_data && geo_data.latitude
210
- self.longitude = geo_data.longitude if geo_data && geo_data.longitude
211
- # translate other languages
212
253
 
213
- if MagicAddresses.configuration.job_backend == :sidekiq
214
- ::MagicAddresses::AddressWorker.perform_async( self.id )
254
+ if self.status == "translated"
255
+ dev_log "! allready set-up ... change!"
256
+ dev_log
215
257
  else
216
- complete_translated_attributes()
258
+ # set default values
259
+ set_address_defaults( geo_data )
260
+ # translate other languages
261
+ if MagicAddresses.configuration.job_backend == :sidekiq
262
+ ::MagicAddresses::AddressWorker.perform_async( self.id )
263
+ else
264
+ complete_translated_attributes()
265
+ end
266
+ self.save
267
+ dev_log
268
+ ## inform address owners
269
+ self.touch_owners
270
+ ## return address
271
+ self
217
272
  end
218
273
 
219
- self.save
220
-
221
- ## inform address owners
222
- self.touch_owners
223
274
  end
224
275
 
225
276
  end
226
277
 
278
+
227
279
  end
228
280
 
229
281
 
@@ -241,8 +293,10 @@ private
241
293
 
242
294
  if geo_data.any?
243
295
  # build street parameters
296
+ dev_log ">>> Street"
244
297
  street_params = []
245
298
  geo_data.each do |key, stuff|
299
+ dev_log " #{key}: #{ stuff.street }"
246
300
  if MagicAddresses.configuration.uniq_translations
247
301
  # => only save locale if different from default-locale:
248
302
  street_params << { locale: key.to_s, street_name: stuff.street } if stuff.street && ((stuff.street != geo_data[default_locale].street) || (default_locale == key.to_s))
@@ -252,13 +306,12 @@ private
252
306
  end
253
307
  # set street parameters if present
254
308
  if street_params.any?
255
- dev_log "empty street translations"
256
309
  # reset translations (avoid empty translation in default language)
257
310
  # => self.translations = []
258
311
  self.translations.delete_all
259
312
  # set street parameters
260
313
  self.translations_attributes = street_params
261
- dev_log "set new street translations"
314
+ dev_log " set translations: #{ street_params.map{ |k| k[:locale] }.join(", ") }"
262
315
  end
263
316
 
264
317
  self.street_default = geo_data[default_locale].street if geo_data[default_locale] && geo_data[default_locale].street
@@ -294,7 +347,8 @@ private
294
347
  ## Build Associations
295
348
 
296
349
  def connet_address_association( that, geo_data )
297
- dev_log ">>> #{that.to_s.upcase}: #{ geo_data[default_locale].send(that) }"
350
+ this = "magic_#{that}".to_sym
351
+ dev_log "#{that.to_s.upcase} (#{this}): #{ geo_data[default_locale].send(that) }"
298
352
  that_params = { default_name: geo_data[default_locale].send(that).to_s }
299
353
  that_params[ that == :country ? :iso_code : :short_name ] = geo_data[default_locale].send( "#{that}_code".to_sym ).to_s
300
354
  that_params.merge!({ country_id: self.magic_country.id }) if [:city, :state].include?(that) && self.magic_country
@@ -302,12 +356,8 @@ private
302
356
  that_params.merge!({ city_id: self.magic_city.id }) if [:district, :subdistrict].include?(that) && self.magic_city
303
357
  that_params.merge!({ district_id: self.magic_district.id }) if that == :subdistrict && self.magic_district
304
358
 
305
- this = "magic_#{that}".to_sym
306
-
307
359
  self.attributes = { this => "MagicAddresses::#{that.to_s.classify}".constantize.unscoped.where( that_params ).first_or_create! }# unless self.send(that)
308
360
 
309
- dev_log "#{that} .. #{this}"
310
-
311
361
  # self.send(this).translations = []
312
362
  self.send(this).translations.delete_all
313
363
  self.send(this).translations_attributes = lng_params( that, geo_data )
@@ -319,7 +369,7 @@ private
319
369
  def lng_params( that, geo_data )
320
370
  lng_params = []
321
371
  geo_data.each do |key, stuff|
322
- dev_log "#{that.to_s.titleize}-Params (#{key}) ... #{stuff.send(that)}"
372
+ dev_log " #{key}: #{stuff.send(that)}"
323
373
 
324
374
  if MagicAddresses.configuration.uniq_translations
325
375
  # => only save locale if different from default-locale:
@@ -331,11 +381,15 @@ private
331
381
  lng_params
332
382
  end
333
383
 
384
+ def update_fetch_address( atts = {} )
385
+ self.fetch_address = (self.fetch_address || {}).merge( atts )
386
+ end
387
+
334
388
  def dev_log( stuff = " " )
335
- if !Rails.env.production?
336
- Rails.logger.info "###"
389
+ if Rails.env.test?
390
+ puts "#: #{stuff}"
391
+ elsif !Rails.env.production?
337
392
  Rails.logger.info "### #{stuff}"
338
- Rails.logger.info "###"
339
393
  end
340
394
  end
341
395
 
@@ -15,7 +15,7 @@ class MagicAddresses::Addressible < ActiveRecord::Base
15
15
 
16
16
 
17
17
  # =====> C A L L B A C K S <=============================================================== #
18
- after_commit :log_some_stuff
18
+ # after_commit :log_some_stuff
19
19
 
20
20
  # =====> P R I V A T E ! <======================================================== # # # # # # # #
21
21
  private
@@ -47,7 +47,7 @@ module MagicAddresses
47
47
  through: :addressible,
48
48
  source: :address
49
49
 
50
- accepts_nested_attributes_for :addressible, :address, allow_destroy: true, reject_if: :all_blank
50
+ # accepts_nested_attributes_for :addressible, :address, allow_destroy: true, reject_if: :all_blank
51
51
  end
52
52
 
53
53
 
@@ -83,6 +83,16 @@ module MagicAddresses
83
83
  self.addressible.build_address(params)
84
84
  end
85
85
 
86
+ def address_attributes=( params )
87
+ # puts "address_attributes"
88
+ self.address = MagicAddresses::Address.get_one( self, params )
89
+ end
90
+
91
+ def addressible_attributes=( params )
92
+ # puts "addressible_attributes"
93
+ self.address = MagicAddresses::Address.get_one( self, params[:address_attributes] )
94
+ end
95
+
86
96
  end #> InstanceMethods
87
97
 
88
98
  module NestedInstanceMethods
@@ -8,17 +8,17 @@ class MagicAddresses::GeoCoder
8
8
  begin
9
9
  search_via_google
10
10
  results = Geocoder.search( q, language: language )
11
- log "google Geocoded: #{results.to_yaml}"
11
+ log " - google: [#{results.count}]"
12
12
  return results
13
13
  rescue Exception => e
14
- log "Google -- #{e}"
14
+ log "! Google-Error: #{e} !"
15
15
  sleep 0.3
16
16
  attempts += 1
17
17
  end while attempts < 3
18
18
  unless results
19
19
  search_via_nominatim
20
20
  results = Geocoder.search( q, language: language )
21
- log "nominatim Geocoded: #{results.to_yaml}"
21
+ log " - nominatim: [#{results.count}]"
22
22
  end
23
23
  else
24
24
  results = []
@@ -35,11 +35,11 @@ class MagicAddresses::GeoCoder
35
35
  Geocoder.configure( :lookup => :nominatim )
36
36
  end
37
37
 
38
- def self.log( stuff = "+ geo + geo + geo + geo + geo + geo + " )
39
- if Rails.env.development?
40
- Rails.logger.info "+ geo + geo + geo + geo + geo + geo + "
41
- Rails.logger.info stuff
42
- Rails.logger.info "+ geo + geo + geo + geo + geo + geo + "
38
+ def self.log( stuff = " " )
39
+ if Rails.env.test?
40
+ puts "#: #{stuff}"
41
+ elsif !Rails.env.production?
42
+ Rails.logger.info "### #{stuff}"
43
43
  end
44
44
  end
45
45
 
@@ -1,6 +1,9 @@
1
1
  # encoding: utf-8
2
2
  module MagicAddresses
3
+
4
+ # =====> C L A S S - M E T H O D S <======================================================= #
3
5
  def self.table_name_prefix
4
6
  'mgca_'
5
7
  end
8
+
6
9
  end
@@ -1,3 +1,3 @@
1
1
  module MagicAddresses
2
- VERSION = "0.0.42"
2
+ VERSION = "0.0.43"
3
3
  end
@@ -136,6 +136,9 @@ describe MagicAddresses::Address do
136
136
  describe "fetches correct address after save" do
137
137
 
138
138
  let(:user){ User.create!(name: "Some User", address_attributes: { street: "Heinz-Kapelle-Str.", number: "6", postalcode: 10407, city: "Berlin", country:"Germany"}) }
139
+
140
+ # let(:user){ User.create!(name: "Some User", addressible_attributes: { address_attributes: { street: "Heinz-Kapelle-Str.", number: "6", postalcode: 10407, city: "Berlin", country:"Germany"}}) }
141
+
139
142
  # let(:address){ MagicAddresses::Address.create!(name: "Another", owner: user) }
140
143
  let(:address){
141
144
  user.address
@@ -182,12 +185,12 @@ describe MagicAddresses::Address do
182
185
  expect( MagicAddresses::Subdistrict.all.count ).to eq( 1 )
183
186
 
184
187
  # address attributes should be set
185
- expect( address.number ).to eq( "6" )
186
- expect( address.street_number ).to eq( "6" )
187
- expect( address.postalcode ).to eq( 10407 )
188
- expect( address.zipcode ).to eq( 10407 )
189
- expect( address.street ).to eq( "Heinz-Kapelle-Straße" )
190
- expect( address.street_name ).to eq( "Heinz-Kapelle-Straße" )
188
+ expect( user.address.number ).to eq( "6" )
189
+ expect( user.address.street_number ).to eq( "6" )
190
+ expect( user.address.postalcode ).to eq( 10407 )
191
+ expect( user.address.zipcode ).to eq( 10407 )
192
+ expect( user.address.street ).to match( /Heinz-Kapelle-Str[.|aße]{1,3}/ )
193
+ expect( user.address.street_name ).to match( /Heinz-Kapelle-Str[.|aße]{1,3}/ )
191
194
 
192
195
  # all virtual atts and associations should be equal
193
196
  expect( address.country ).to eq( "Germany" )
@@ -35,4 +35,167 @@ RSpec.describe User, type: :model do
35
35
 
36
36
  end
37
37
 
38
+
39
+ it "does not change address for other owners" do
40
+
41
+ addr = { street: "Heinz-Kapelle-Str.", number: "6", postalcode: "10407", city: "Berlin"}
42
+ usr1 = { name: "tom", address_attributes: addr }
43
+ usr2 = { name: "tim", address_attributes: addr }
44
+
45
+ u1 = User.create!( usr1 )
46
+ u2 = User.create!( usr2 )
47
+
48
+ # sleep 3
49
+
50
+ expect( User.all.count ).to eq 2
51
+ expect( MagicAddresses::Address.all.count ).to eq 1
52
+
53
+ u1 = User.find_by( name: "tom" )
54
+ u2 = User.find_by( name: "tim" )
55
+
56
+ u2.update( address_attributes: addr.merge({ number: 8 }) )
57
+
58
+ expect( MagicAddresses::Address.all.count ).to eq 2
59
+ expect( MagicAddresses::Addressible.where(default: true).count ).to eq 2
60
+
61
+ expect( u1.address.id ).to eq MagicAddresses::Address.first.id
62
+ expect( u2.address.id ).to eq MagicAddresses::Address.last.id
63
+
64
+ expect( u1.address.id ).not_to eq u2.address.id
65
+
66
+ end
67
+
68
+
69
+ it "does not change address for other owners" do
70
+
71
+ addr = { street: "Heinz-Kapelle-Str.", number: "6", postalcode: "10407", city: "Berlin"}
72
+ usr1 = { name: "tom", addressible_attributes: {address_attributes: addr} }
73
+ usr2 = { name: "tim", addressible_attributes: {address_attributes: addr} }
74
+
75
+ u1 = User.create!( usr1 )
76
+ u2 = User.create!( usr2 )
77
+
78
+ # sleep 3
79
+
80
+ expect( User.all.count ).to eq 2
81
+ expect( MagicAddresses::Address.all.count ).to eq 1
82
+
83
+ u1 = User.find_by( name: "tom" )
84
+ u2 = User.find_by( name: "tim" )
85
+
86
+ u2.update( addressible_attributes: { address_attributes: addr.merge({ number: 8 }) } )
87
+
88
+ expect( u1.address.id ).to eq MagicAddresses::Address.first.id
89
+ expect( u2.address.id ).to eq MagicAddresses::Address.last.id
90
+
91
+ expect( u1.address.id ).not_to eq u2.address.id
92
+
93
+ end
94
+
95
+
96
+ it "does not save address without changes" do
97
+ addr = { street: "Heinz-Kapelle-Str.", number: "6", postalcode: "10407", city: "Berlin"}
98
+ usr1 = { name: "tom", address_attributes: addr }
99
+ usr2 = { name: "tim", addressible_attributes: {address_attributes: addr} }
100
+
101
+ u1 = User.create!( usr1 )
102
+ u2 = User.create!( usr2 )
103
+
104
+ expect( User.all.count ).to eq 2
105
+ expect( MagicAddresses::Address.all.count ).to eq 1
106
+
107
+ u1 = User.find_by( name: "tom" )
108
+ u2 = User.find_by( name: "tim" )
109
+
110
+ u2.update( addressible_attributes: { id: u2.addressible.id, address_attributes: addr.merge({ id: u2.address.id, number: "4" }) } )
111
+
112
+ expect( u1.address.id ).to eq MagicAddresses::Address.first.id
113
+ expect( u2.address.id ).to eq MagicAddresses::Address.last.id
114
+
115
+ expect( u1.address.id ).not_to eq u2.address.id
116
+
117
+ end
118
+
119
+ it "does not update address without changes" do
120
+ addr = { street: "Heinz-Kapelle-Str.", number: "6", postalcode: "10407", city: "Berlin"}
121
+ usr1 = { name: "tom", address_attributes: addr }
122
+ usr2 = { name: "tim", addressible_attributes: {address_attributes: addr} }
123
+
124
+ u1 = User.create!( usr1 )
125
+ u2 = User.create!( usr2 )
126
+
127
+ expect( User.all.count ).to eq 2
128
+ expect( MagicAddresses::Address.all.count ).to eq 1
129
+
130
+ u1 = User.find_by( name: "tom" )
131
+ u2 = User.find_by( name: "tim" )
132
+
133
+ u1.update( address_attributes: addr.merge({ id: u1.address.id }) )
134
+ u2.update( addressible_attributes: { id: u2.addressible.id, address_attributes: addr.merge({ id: u2.address.id }) } )
135
+
136
+ expect( MagicAddresses::Address.all.count ).to eq 1
137
+
138
+ expect( u1.address.id ).to eq MagicAddresses::Address.first.id
139
+ expect( u2.address.id ).to eq MagicAddresses::Address.first.id
140
+
141
+ end
142
+
143
+ describe "(magic) Address" do
144
+ it { should respond_to :addressible }
145
+ it { should respond_to :address }
146
+ it 'creates address magic' do
147
+ @that = User.create!( name: "Acme" )
148
+ # @that = User.create!( name: "Acme", address_attributes: {street: "Reinhardtstraße", number: "9", postalcode: "10117", city: "Berlin", country: "Germany"} )
149
+ # @that.build_address( {street: "Reinhardtstraße", number: "9", postalcode: "10117", city: "Berlin", country: "Germany"} )
150
+ @that.address = MagicAddresses::Address.create( street: "Reinhardtstraße", number: "9", postalcode: "10117", city: "Berlin", country: "Germany" )
151
+ @that.save
152
+ @that.address.trigger_build_address_associations
153
+ # sleep 2
154
+ @that = User.last
155
+ expect( @that.address ).not_to eql( nil )
156
+ expect( @that.address.street ).to eql( "Reinhardtstraße" )
157
+ expect( @that.address.longitude ).not_to eql( nil )
158
+ end
159
+
160
+ it 'doesn`t change address for multiple companies', focus: true do
161
+ @that = User.create!( name: "Acme", addressible_attributes: {address_attributes: {street: "Reinhardtstraße", number: "9", postalcode: "10117", city: "Berlin", country: "Germany"}} )
162
+ # @that.address.trigger_build_address_associations
163
+ # sleep 4
164
+ @that = @that.reload
165
+ expect( @that.address ).not_to eql( nil )
166
+ expect( @that.address.street ).to eql( "Reinhardtstraße" )
167
+ expect( @that.address.longitude ).not_to eql( nil )
168
+
169
+ @this = User.create!( name: "Apple", addressible_attributes: {address_attributes: {street: "Reinhardtstraße", number: "9", postalcode: "10117", city: "Berlin", country: "Germany"}} )
170
+
171
+ @this = @this.reload
172
+ # @this.address.trigger_build_address_associations
173
+ # sleep 2
174
+ @this = @this.reload
175
+ expect( @this.address ).not_to eql( nil )
176
+ expect( @this.address.street ).to eql( "Reinhardtstraße" )
177
+ expect( @this.address.longitude ).not_to eql( nil )
178
+
179
+ puts "#####"
180
+
181
+ # => @that.address.street = "Reinhardtstraße"
182
+ # => @that.address.number = "7"
183
+ # => @that.address.postalcode = "10117"
184
+ # => @that.address.city = "Berlin"
185
+ # => @that.save
186
+
187
+ @that.update( addressible_attributes: {address_attributes: {street: "Reinhardtstraße", number: "7", postalcode: "10117", city: "Berlin", country: "Germany"}} )
188
+
189
+ @that = @that.reload
190
+ @this = @this.reload
191
+
192
+ expect( @that.address.id ).not_to eql( @this.address.id )
193
+ expect( @that.address.number ).not_to eql( @this.address.number )
194
+ expect( @that.address.number ).to eql( "7" )
195
+ expect( @this.address.number ).to eql( "9" )
196
+
197
+ end
198
+
199
+ end
200
+
38
201
  end
metadata CHANGED
@@ -1,111 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magic_addresses
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.42
4
+ version: 0.0.43
5
5
  platform: ruby
6
6
  authors:
7
7
  - Torsten Wetzel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-28 00:00:00.000000000 Z
11
+ date: 2016-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '4.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: globalize
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 5.0.1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 5.0.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: geocoder
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.2'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.2'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: activerecord-postgres-earthdistance
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ! '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0.3'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ! '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0.3'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: railties
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ! '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '4.0'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ! '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '4.0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: sqlite3
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ! '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '3.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ! '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '3.0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rspec-rails
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ! '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '1.3'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ! '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '1.3'
111
111
  description: Easy sortable and translated addresses uses google-api with nominatim
@@ -1101,17 +1101,17 @@ require_paths:
1101
1101
  - lib
1102
1102
  required_ruby_version: !ruby/object:Gem::Requirement
1103
1103
  requirements:
1104
- - - ! '>='
1104
+ - - ">="
1105
1105
  - !ruby/object:Gem::Version
1106
1106
  version: '0'
1107
1107
  required_rubygems_version: !ruby/object:Gem::Requirement
1108
1108
  requirements:
1109
- - - ! '>='
1109
+ - - ">="
1110
1110
  - !ruby/object:Gem::Version
1111
1111
  version: '0'
1112
1112
  requirements: []
1113
1113
  rubyforge_project:
1114
- rubygems_version: 2.4.8
1114
+ rubygems_version: 2.5.0
1115
1115
  signing_key:
1116
1116
  specification_version: 4
1117
1117
  summary: A really nice address plugin we offten use.