duty_free 1.0.2 → 1.0.3

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,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa22e01d02997c6b8bc562f19805d1f0f529e17793b0dd14282f43840ea2c86c
4
- data.tar.gz: afa848b412ea1514e0afeaa36ba1093ccb1baecdd4384d225b07f76145e9457e
3
+ metadata.gz: 4451cbb29d90fb32347b1ff91e91a249da97769e7a64f2524efa912b8971bf86
4
+ data.tar.gz: ee3b06059aa89be48329ef48605287e8cb1725dd9f61460d9b3d044354405540
5
5
  SHA512:
6
- metadata.gz: c60d6f2a72d34f4f99d3f9fea16b0ed2d1ff71b6088e4b6782a0b54decc15ef5ba45cef17f846079f4e5a55fd2f5dfcf25945b80351ff6006318b59fedcffd13
7
- data.tar.gz: 6be734a732268268ef86768d88215e62b947c97326d823f3b631bab16de3f919877fedbf4184da1e1490904a752ed8e675bb173966cb9f2922860ae0b606020d
6
+ metadata.gz: 953bb38945daf16d0f22daa921ad1e0f46881583d51145a59b1983869da8d79c0675640677f1774dbdb9321695aeb69dc29dd4f66250704be0199fed33ab84b6
7
+ data.tar.gz: 8e3a0c166acb9bc22919218764e1e39ffa9d495e22ea93eaeb8ef6810b545a99f6af08bf594cc3958e7d9169ccfe5a7841c08b2799e4981066cb47968daaf26b
@@ -14,6 +14,7 @@ module DutyFree
14
14
 
15
15
  # :nodoc:
16
16
  module ClassMethods
17
+ MAX_ID = Arel.sql('MAX(id)')
17
18
  # def self.extended(model)
18
19
  # end
19
20
 
@@ -203,8 +204,9 @@ module DutyFree
203
204
  is_do_save = true
204
205
  existing_unique = valid_unique.inject([]) do |s, v|
205
206
  s << if v.last.is_a?(Array)
206
- v.last[0].where(v.last[1] => row[v.last[2]]).limit(1).pluck('MAX(id)').first.to_s
207
+ v.last[0].where(v.last[1] => row[v.last[2]]).limit(1).pluck(MAX_ID).first.to_s
207
208
  else
209
+ binding.pry if v.last.nil?
208
210
  row[v.last].to_s
209
211
  end
210
212
  end
@@ -292,19 +294,21 @@ module DutyFree
292
294
  if klass == sub_obj.class # Self-referencing thing pointing to us?
293
295
  # %%% This should be more general than just for self-referencing things.
294
296
  sub_cols = cols.map { |c| c.start_with?(trim_prefix) ? c[trim_prefix.length..-1] : nil }
295
- sub_bt, criteria = klass.find_existing(uniques, sub_cols, starred, import_template, keepers, assoc, row, klass, all, trim_prefix)
297
+ # assoc
298
+ sub_bt, criteria = klass.find_existing(uniques, sub_cols, starred, import_template, keepers, nil, row, klass, all, '')
296
299
  else
297
300
  sub_bt, criteria = klass.find_existing(uniques, cols, starred, import_template, keepers, nil, row, klass, all, trim_prefix)
298
301
  end
299
302
  rescue ::DutyFree::NoUniqueColumnError
300
303
  sub_unique = nil
301
304
  end
302
- # binding.pry if sub_obj.is_a?(Employee) && sub_obj.first_name == 'Nancy' &&
303
- # sub_bt.is_a?(Employee)
305
+ # Self-referencing shouldn't build a new one if it couldn't find one
304
306
  # %%% Can criteria really ever be nil anymore?
305
- sub_bt ||= klass.new(criteria || {})
307
+ unless klass == sub_obj.class && criteria.empty?
308
+ sub_bt ||= klass.new(criteria || {})
309
+ end
306
310
  sub_obj.send("#{path_part}=", sub_bt)
307
- sub_bt # unless klass == sub_obj.class # Don't go further if it's self-referencing
311
+ sub_bt
308
312
  end
309
313
  end
310
314
  # Look for possible missing polymorphic detail
@@ -328,6 +332,7 @@ module DutyFree
328
332
  sub_hm, criteria = klass.find_existing(uniques, cols, starred, import_template, keepers, assoc.inverse_of, row, sub_next, all, trim_prefix)
329
333
 
330
334
  # If still not found then create a new related object using this has_many collection
335
+ # (criteria.empty? ? nil : sub_next.new(criteria))
331
336
  sub_next = sub_hm || sub_next.new(criteria)
332
337
  end
333
338
  unless sub_next.nil?
@@ -342,8 +347,10 @@ module DutyFree
342
347
  sub_objects[this_path] = sub_next if this_path.present?
343
348
  end
344
349
  end
345
- sub_obj = sub_next unless sub_next.nil?
350
+ # binding.pry if sub_obj.reports_to
351
+ sub_obj = sub_next #if sub_next
346
352
  end
353
+ # binding.pry if sub_obj.nil?
347
354
  next if sub_obj.nil?
348
355
 
349
356
  sym = "#{v.name}=".to_sym
@@ -366,6 +373,7 @@ module DutyFree
366
373
  row_errors[v.name] << "Boolean value \"#{row[key]}\" in column #{key + 1} not recognized"
367
374
  end
368
375
  end
376
+ # binding.pry if v.name.to_s == 'first_name' && sub_obj.first_name == 'Nancy'
369
377
  sub_obj.send(sym, row[key])
370
378
  # else
371
379
  # puts " #{sub_class.name} doesn't respond to #{sym}"
@@ -475,48 +483,76 @@ module DutyFree
475
483
  # Might have to do a deferred save kind of thing, and also make sure the product stuff came first
476
484
  # before the other stuff
477
485
 
486
+ # Find by all corresponding columns
487
+
478
488
  # Add in any foreign key stuff we can find from other belongs_to associations
479
489
  # %%% This is starting to look like the other BelongsToAssociation code above around line
480
490
  # 697, so it really needs to be turned into something recursive instead of this two-layer
481
491
  # thick thing at best.
492
+
493
+ # First check the belongs_tos
494
+ criteria = {}
495
+ bt_criteria = {}
496
+ bt_criteria_all_nil = true
497
+ bt_col_indexes = []
498
+ only_valid_uniques = (train_we_came_in_here_on == false)
482
499
  bts = reflect_on_all_associations.each_with_object([]) do |sn_assoc, s|
483
500
  if sn_assoc.is_a?(ActiveRecord::Reflection::BelongsToReflection) &&
484
- (!train_we_came_in_here_on || sn_assoc != train_we_came_in_here_on) &&
485
- sn_assoc.klass != self # Omit stuff pointing to us (like self-referencing stuff)
501
+ (!train_we_came_in_here_on || sn_assoc != train_we_came_in_here_on) # &&
502
+ # sn_assoc.klass != self # Omit stuff pointing to us (like self-referencing stuff)
486
503
  # %%% Make sure there's a starred column we know about from this one
487
- ret[sn_assoc.foreign_key] = nil if train_we_came_in_here_on == false
504
+ ret[sn_assoc.foreign_key] = nil if only_valid_uniques
488
505
  s << sn_assoc
489
506
  end
490
507
  s
491
508
  end
492
-
493
- # Find by all corresponding columns
494
- criteria = {}
495
- if train_we_came_in_here_on != false
496
- criteria = ret.each_with_object({}) do |v, s|
497
- s[v.first.to_sym] = row[v.last]
498
- s
499
- end
500
- end
501
-
502
509
  bts.each do |sn_bt|
503
510
  # This search prefix becomes something like "Order Details Product "
511
+ # binding.pry
504
512
  cols.each_with_index do |bt_col, idx|
505
- next unless bt_col.start_with?(trim_prefix + "#{sn_bt.name.to_s.underscore.tr('_', ' ').titleize} ")
513
+ next if bt_col_indexes.include?(idx) ||
514
+ !bt_col&.start_with?(trim_prefix + "#{sn_bt.name.to_s.underscore.tr('_', ' ').titleize} ")
506
515
 
507
516
  fk_id = if row
508
517
  # Max ID so if there are multiple, only the most recent one is picked.
509
- # %%% Need to stack these up in case there are multiple (like first_name, last_name on a referenced employee)
510
- sn_bt.klass.where(keepers[idx].name => row[idx]).limit(1).pluck('MAX(id)').first
518
+ # %%% Need to stack these up in case there are multiple
519
+ # (like first_name, last_name on a referenced employee)
520
+ # binding.pry
521
+ sn_bt.klass.where(keepers[idx].name => row[idx]).limit(1).pluck(MAX_ID).first
511
522
  else
512
523
  [sn_bt.klass, keepers[idx].name, idx]
513
524
  end
514
- criteria[sn_bt.foreign_key] = fk_id
525
+ if fk_id
526
+ bt_col_indexes << idx
527
+ bt_criteria_all_nil = false
528
+ end
529
+ bt_criteria[(fk_name = sn_bt.foreign_key)] = fk_id
530
+ # Add to our criteria if this belongs_to is required
531
+ # %%% Rails older than 5.0 handles this stuff differently!
532
+ unless sn_bt.options[:optional] || !sn_bt.klass.belongs_to_required_by_default
533
+ criteria[fk_name] = fk_id
534
+ else # Should not have this fk as a requirement
535
+ ret.delete(fk_name) if only_valid_uniques && ret.include?(fk_name)
536
+ end
537
+ end
538
+ end
539
+
540
+ new_criteria_all_nil = bt_criteria_all_nil
541
+ if train_we_came_in_here_on != false
542
+ criteria = ret.each_with_object({}) do |v, s|
543
+ next if bt_col_indexes.include?(v.last)
544
+
545
+ new_criteria_all_nil = false if (s[v.first.to_sym] = row[v.last])
546
+ s
515
547
  end
516
548
  end
517
549
 
518
550
  # Short-circuiting this to only get back the valid_uniques?
519
- return ret.merge(criteria) if train_we_came_in_here_on == false
551
+ return ret.merge(criteria) if only_valid_uniques
552
+
553
+ # binding.pry if obj.is_a?(Order)
554
+ # If there's nothing to match upon then we're out
555
+ return [nil, {}] if new_criteria_all_nil
520
556
 
521
557
  # With this criteria, find any matching has_many row we can so we can update it
522
558
  sub_hm = obj.find do |hm_obj|
@@ -532,7 +568,7 @@ module DutyFree
532
568
  # Try looking it up through ActiveRecord
533
569
  # %%% Should we perhaps do this first before the more intensive find routine above?
534
570
  sub_hm = obj.find_by(criteria) if sub_hm.nil?
535
- [sub_hm, criteria]
571
+ [sub_hm, criteria.merge(bt_criteria)]
536
572
  end
537
573
 
538
574
  private
@@ -5,7 +5,7 @@ module DutyFree
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 2
8
+ TINY = 3
9
9
 
10
10
  # PRE is nil unless it's a pre-release (beta, RC, etc.)
11
11
  PRE = nil
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duty_free
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorin Thwaits
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-04 00:00:00.000000000 Z
11
+ date: 2020-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -45,19 +45,19 @@ dependencies:
45
45
  - !ruby/object:Gem::Version
46
46
  version: '2.2'
47
47
  - !ruby/object:Gem::Dependency
48
- name: byebug
48
+ name: pry-byebug
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - ">="
51
+ - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: 3.7.0
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
- - - ">="
58
+ - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '0'
60
+ version: 3.7.0
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: ffaker
63
63
  requirement: !ruby/object:Gem::Requirement