duty_free 1.0.8 → 1.0.10
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 +4 -4
- data/lib/duty_free/extensions.rb +39 -30
- data/lib/duty_free/suggest_template.rb +378 -166
- data/lib/duty_free/util.rb +26 -12
- data/lib/duty_free/version_number.rb +1 -1
- data/lib/duty_free.rb +32 -7
- data/lib/generators/duty_free/model_generator.rb +349 -0
- data/lib/generators/duty_free/templates/create_versions.rb.erb +2 -2
- metadata +4 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa24c207ea3302cdd1cb04bf479c8140c63b4854a3ca628cb9a9f2b02ca9db19
|
4
|
+
data.tar.gz: 2d01e88512109b271209cb95f5b2b3e8f3b4222fda500e0d5f7b8ab06f6a0c11
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 025a2518dd7b88156e77be76697eeb0480b0a3fe7930fda9d4f2509b4d718e9d17744a6f5fb4ffdf8709ea7a80a0056fdb525d19ade89e3da1af084a9feb7a54
|
7
|
+
data.tar.gz: c1fd873299cb7bfca3f265d949c9e7bcc2788b5d2854a92c409ac4c5d7248827f07c4e21c2b353c10115d97201103b3e78dbbd119b2b61ae3b1a2f1659533649
|
data/lib/duty_free/extensions.rb
CHANGED
@@ -9,6 +9,7 @@ module DutyFree
|
|
9
9
|
# rubocop:disable Style/CommentedKeyword
|
10
10
|
module Extensions
|
11
11
|
MAX_ID = Arel.sql('MAX(id)')
|
12
|
+
IS_AMOEBA = Gem.loaded_specs['amoeba']
|
12
13
|
|
13
14
|
def self.included(base)
|
14
15
|
base.send :extend, ClassMethods
|
@@ -111,8 +112,8 @@ module DutyFree
|
|
111
112
|
rows
|
112
113
|
end
|
113
114
|
|
114
|
-
def df_import(data, import_template = nil)
|
115
|
-
::DutyFree::Extensions.import(self, data, import_template)
|
115
|
+
def df_import(data, import_template = nil, insert_only = false)
|
116
|
+
::DutyFree::Extensions.import(self, data, import_template, insert_only)
|
116
117
|
end
|
117
118
|
|
118
119
|
private
|
@@ -159,7 +160,7 @@ module DutyFree
|
|
159
160
|
# For use with importing, based on the provided column list calculate all valid combinations
|
160
161
|
# of unique columns. If there is no valid combination, throws an error.
|
161
162
|
# Returns an object found by this means, as well as the criteria that was used to find it.
|
162
|
-
def _find_existing(uniques, cols, starred, import_template, keepers, train_we_came_in_here_on,
|
163
|
+
def _find_existing(uniques, cols, starred, import_template, keepers, train_we_came_in_here_on, insert_only,
|
163
164
|
row = nil, klass_or_collection = nil, template_all = nil, trim_prefix = '',
|
164
165
|
assoc = nil, base_obj = nil)
|
165
166
|
unless trim_prefix.blank?
|
@@ -262,7 +263,8 @@ module DutyFree
|
|
262
263
|
# If we're processing a row then this list of foreign key column name entries, named such as
|
263
264
|
# "order_id" or "product_id" instead of column-specific stuff like "Order Date" and "Product Name",
|
264
265
|
# is kept until the last and then gets merged on top of the other criteria before being returned.
|
265
|
-
|
266
|
+
fk_name = sn_bt.foreign_key
|
267
|
+
bt_criteria[fk_name] = fk_id unless bt_criteria.include?(fk_name)
|
266
268
|
|
267
269
|
# Check to see if belongs_tos are generally required on this specific table
|
268
270
|
bt_req_by_default = sn_bt.klass.respond_to?(:belongs_to_required_by_default) &&
|
@@ -282,7 +284,7 @@ module DutyFree
|
|
282
284
|
(sn_bt.options[:optional] || !bt_req_by_default)
|
283
285
|
|
284
286
|
# Add to the criteria
|
285
|
-
criteria[fk_name] = fk_id
|
287
|
+
criteria[fk_name] = fk_id if insert_only && !criteria.include?(fk_name)
|
286
288
|
end
|
287
289
|
end
|
288
290
|
|
@@ -332,14 +334,15 @@ module DutyFree
|
|
332
334
|
# Find by all corresponding columns
|
333
335
|
if (row_value = row[v])
|
334
336
|
new_criteria_all_nil = false
|
335
|
-
criteria[k_sym] = row_value # The data
|
337
|
+
criteria[k_sym] = row_value # The data
|
336
338
|
end
|
337
339
|
end
|
338
340
|
end
|
341
|
+
# puts uniq_lookups.inspect
|
339
342
|
|
340
|
-
return uniq_lookups.merge(criteria) if only_valid_uniques
|
343
|
+
return [uniq_lookups.merge(criteria), bt_criteria] if only_valid_uniques
|
341
344
|
# If there's nothing to match upon then we're out
|
342
|
-
return [nil, {}] if new_criteria_all_nil
|
345
|
+
return [nil, {}, {}] if new_criteria_all_nil
|
343
346
|
|
344
347
|
# With this criteria, find any matching has_many row we can so we can update it.
|
345
348
|
# First try directly looking it up through ActiveRecord.
|
@@ -410,12 +413,12 @@ module DutyFree
|
|
410
413
|
# Standard criteria as well as foreign key column name detail with exact foreign keys
|
411
414
|
# that match up to a primary key so that if needed a new related object can be built,
|
412
415
|
# complete with all its association detail.
|
413
|
-
[found_object, criteria
|
416
|
+
[found_object, criteria, bt_criteria]
|
414
417
|
end # _find_existing
|
415
418
|
end # module ClassMethods
|
416
419
|
|
417
420
|
# With an array of incoming data, the first row having column names, perform the import
|
418
|
-
def self.import(obj_klass, data, import_template = nil)
|
421
|
+
def self.import(obj_klass, data, import_template = nil, insert_only)
|
419
422
|
instance_variable_set(:@defined_uniques, nil)
|
420
423
|
instance_variable_set(:@valid_uniques, nil)
|
421
424
|
|
@@ -559,7 +562,7 @@ module DutyFree
|
|
559
562
|
raise ::DutyFree::LessThanHalfAreMatchingColumnsError, I18n.t('import.altered_import_template_coumns') if keepers.length < (cols.length / 2) - 1
|
560
563
|
|
561
564
|
# Returns just the first valid unique lookup set if there are multiple
|
562
|
-
valid_unique = obj_klass.send(:_find_existing, uniques, cols, starred, import_template, keepers, false)
|
565
|
+
valid_unique, bt_criteria = obj_klass.send(:_find_existing, uniques, cols, starred, import_template, keepers, false, insert_only)
|
563
566
|
# Make a lookup from unique values to specific IDs
|
564
567
|
existing = obj_klass.pluck(*([:id] + valid_unique.keys)).each_with_object(existing) do |v, s|
|
565
568
|
s[v[1..-1].map(&:to_s)] = v.first
|
@@ -578,16 +581,17 @@ module DutyFree
|
|
578
581
|
to_be_saved = []
|
579
582
|
# Check to see if they want to preprocess anything
|
580
583
|
existing_unique = @before_process.call(valid_unique, existing_unique) if @before_process ||= import_template[:before_process]
|
581
|
-
if (criteria = existing[existing_unique])
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
584
|
+
obj = if (criteria = existing[existing_unique])
|
585
|
+
obj_klass.find(criteria)
|
586
|
+
else
|
587
|
+
is_insert = true
|
588
|
+
# unless build_tables.empty? # include?()
|
589
|
+
# binding.pry
|
590
|
+
# x = 5
|
591
|
+
# end
|
592
|
+
obj_klass.new
|
593
|
+
end
|
594
|
+
to_be_saved << [obj] unless criteria # || this one has any belongs_to that will be modified here
|
591
595
|
sub_obj = nil
|
592
596
|
polymorphics = []
|
593
597
|
sub_objects = {}
|
@@ -623,18 +627,21 @@ module DutyFree
|
|
623
627
|
start = 0
|
624
628
|
trim_prefix = v.titleize[start..-(v.name.length + 2)]
|
625
629
|
trim_prefix << ' ' unless trim_prefix.blank?
|
626
|
-
if
|
630
|
+
if assoc.belongs_to?
|
627
631
|
klass = Object.const_get(assoc&.class_name)
|
628
632
|
# Try to find a unique item if one is referenced
|
629
633
|
sub_next = nil
|
630
634
|
begin
|
631
|
-
sub_next, criteria = klass.send(:_find_existing, uniques, cols, starred, import_template, keepers, nil,
|
635
|
+
sub_next, criteria, bt_criteria = klass.send(:_find_existing, uniques, cols, starred, import_template, keepers, nil,
|
636
|
+
false, # insert_only
|
637
|
+
row, klass, all, trim_prefix, assoc)
|
632
638
|
rescue ::DutyFree::NoUniqueColumnError
|
633
639
|
end
|
634
|
-
# puts "#{v.path} #{criteria.inspect}"
|
635
640
|
bt_name = "#{path_part}="
|
636
|
-
|
637
|
-
|
641
|
+
# Not yet wired up to the right one, or going to the parent of a self-referencing model?
|
642
|
+
# puts "#{v.path} #{criteria.inspect}"
|
643
|
+
unless sub_next || (klass == sub_obj.class && (all_criteria = criteria.merge(bt_criteria)).empty?)
|
644
|
+
sub_next = klass.new(all_criteria || {})
|
638
645
|
to_be_saved << [sub_next, sub_obj, bt_name, sub_next]
|
639
646
|
end
|
640
647
|
# This wires it up in memory, but doesn't yet put the proper foreign key ID in
|
@@ -654,15 +661,17 @@ module DutyFree
|
|
654
661
|
# Rails 4.0 and later can do: sub_next.is_a?(ActiveRecord::Associations::CollectionProxy)
|
655
662
|
elsif [:has_many, :has_one, :has_and_belongs_to_many].include?(assoc.macro) # && !assoc.options[:through]
|
656
663
|
::DutyFree::Extensions._save_pending(to_be_saved)
|
664
|
+
sub_next = sub_obj.send(path_part)
|
657
665
|
# Try to find a unique item if one is referenced
|
658
666
|
# %%% There is possibility that when bringing in related classes using a nil
|
659
667
|
# in IMPORT_TEMPLATE[:all] that this will break. Need to test deeply nested things.
|
660
668
|
|
661
669
|
# assoc.inverse_of is the belongs_to side of the has_many train we came in here on.
|
662
|
-
sub_hm, criteria = assoc.klass.send(:_find_existing, uniques, cols, starred, import_template, keepers, assoc.inverse_of,
|
663
|
-
|
664
|
-
|
665
|
-
|
670
|
+
sub_hm, criteria, bt_criteria = assoc.klass.send(:_find_existing, uniques, cols, starred, import_template, keepers, assoc.inverse_of,
|
671
|
+
false, # insert_only
|
672
|
+
row, sub_next, all, trim_prefix, assoc,
|
673
|
+
# Just in case we're running Rails < 4.0 and this is a has_*
|
674
|
+
sub_obj)
|
666
675
|
# If still not found then create a new related object using this has_many collection
|
667
676
|
# (criteria.empty? ? nil : sub_next.new(criteria))
|
668
677
|
if sub_hm
|