duty_free 1.0.1 → 1.0.6
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.rb +181 -0
- data/lib/duty_free/column.rb +5 -7
- data/lib/duty_free/extensions.rb +399 -220
- data/lib/duty_free/suggest_template.rb +23 -19
- data/lib/duty_free/util.rb +23 -9
- data/lib/duty_free/version_number.rb +1 -1
- metadata +15 -16
@@ -40,33 +40,32 @@ module DutyFree
|
|
40
40
|
assocs = {}
|
41
41
|
this_klass.reflect_on_all_associations.each do |assoc|
|
42
42
|
# PolymorphicReflection AggregateReflection RuntimeReflection
|
43
|
-
is_belongs_to = assoc.is_a?(ActiveRecord::Reflection::BelongsToReflection)
|
43
|
+
is_belongs_to = assoc.belongs_to? # is_a?(ActiveRecord::Reflection::BelongsToReflection)
|
44
44
|
# Figure out if it's belongs_to, has_many, or has_one
|
45
|
+
# HasAndBelongsToManyReflection
|
45
46
|
belongs_to_or_has_many =
|
46
47
|
if is_belongs_to
|
47
48
|
'belongs_to'
|
48
|
-
elsif (is_habtm = assoc.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection))
|
49
|
+
elsif (is_habtm = assoc.respond_to?(:macro) ? (assoc.macro == :has_and_belongs_to_many) : assoc.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection))
|
49
50
|
'has_and_belongs_to_many'
|
51
|
+
elsif assoc.respond_to?(:macro) ? (assoc.macro == :has_many) : assoc.is_a?(ActiveRecord::Reflection::HasManyReflection)
|
52
|
+
'has_many'
|
50
53
|
else
|
51
|
-
|
54
|
+
'has_one'
|
52
55
|
end
|
53
56
|
# Always process belongs_to, and also process has_one and has_many if do_has_many is chosen.
|
54
57
|
# Skip any HMT or HABTM. (Maybe break out HABTM into a combo HM and BT in the future.)
|
55
58
|
if is_habtm
|
56
|
-
unless ActiveRecord::Base.connection.table_exists?(assoc.join_table)
|
57
|
-
puts "* In the #{this_klass.name} model there's a problem with: \"has_and_belongs_to_many :#{assoc.name}\" because join table \"#{assoc.join_table}\" does not exist. You can create it with a create_join_table migration."
|
58
|
-
end
|
59
|
+
puts "* In the #{this_klass.name} model there's a problem with: \"has_and_belongs_to_many :#{assoc.name}\" because join table \"#{assoc.join_table}\" does not exist. You can create it with a create_join_table migration." unless ActiveRecord::Base.connection.table_exists?(assoc.join_table)
|
59
60
|
# %%% Search for other associative candidates to use instead of this HABTM contraption
|
60
|
-
if assoc.options.include?(:through)
|
61
|
-
puts "* In the #{this_klass.name} model there's a problem with: \"has_and_belongs_to_many :#{assoc.name}\" because it includes \"through: #{assoc.options[:through].inspect}\" which is pointless and should be removed."
|
62
|
-
end
|
61
|
+
puts "* In the #{this_klass.name} model there's a problem with: \"has_and_belongs_to_many :#{assoc.name}\" because it includes \"through: #{assoc.options[:through].inspect}\" which is pointless and should be removed." if assoc.options.include?(:through)
|
63
62
|
end
|
64
63
|
if (is_through = assoc.is_a?(ActiveRecord::Reflection::ThroughReflection)) && assoc.options.include?(:as)
|
65
64
|
puts "* In the #{this_klass.name} model there's a problem with: \"has_many :#{assoc.name} through: #{assoc.options[:through].inspect}\" because it also includes \"as: #{assoc.options[:as].inspect}\", so please choose either for this line to be a \"has_many :#{assoc.name} through:\" or to be a polymorphic \"has_many :#{assoc.name} as:\". It can't be both."
|
66
65
|
end
|
67
66
|
next if is_through || is_habtm || (!is_belongs_to && !do_has_many) || errored_assocs.include?(assoc)
|
68
67
|
|
69
|
-
if is_belongs_to && assoc.polymorphic
|
68
|
+
if is_belongs_to && assoc.options[:polymorphic] # Polymorphic belongs_to?
|
70
69
|
# Load all models
|
71
70
|
# %%% Note that this works in Rails 5.x, but may not work in Rails 6.0 and later, which uses the Zeitwerk loader by default:
|
72
71
|
Rails.configuration.eager_load_namespaces.select { |ns| ns < Rails::Application }.each(&:eager_load!)
|
@@ -90,7 +89,7 @@ module DutyFree
|
|
90
89
|
end
|
91
90
|
else
|
92
91
|
# Is it a polymorphic has_many, which is defined using as: :somethingable ?
|
93
|
-
is_polymorphic_hm = assoc.inverse_of&.polymorphic
|
92
|
+
is_polymorphic_hm = assoc.inverse_of&.options&.fetch(:polymorphic) { nil }
|
94
93
|
begin
|
95
94
|
# Standard has_one, or has_many, and belongs_to uses assoc.klass.
|
96
95
|
# Also polymorphic belongs_to uses assoc.klass.
|
@@ -106,9 +105,9 @@ module DutyFree
|
|
106
105
|
[[[fk], assoc.active_record], assoc_klass]
|
107
106
|
else # has_many or has_one
|
108
107
|
inverse_foreign_keys = is_polymorphic_hm ? [assoc.type, assoc.foreign_key] : [assoc.inverse_of&.foreign_key&.to_s]
|
109
|
-
puts "* Missing inverse foreign key for #{assoc.inspect}" if inverse_foreign_keys.first.nil?
|
110
108
|
missing_key_columns = inverse_foreign_keys - assoc_klass.columns.map(&:name)
|
111
109
|
if missing_key_columns.empty?
|
110
|
+
puts "* Missing inverse foreign key for #{this_klass.name} #{belongs_to_or_has_many} :#{assoc.name}" if inverse_foreign_keys.first.nil?
|
112
111
|
# puts "Has columns #{inverse_foreign_keys.inspect}"
|
113
112
|
[[inverse_foreign_keys, assoc_klass], assoc_klass]
|
114
113
|
else
|
@@ -182,10 +181,11 @@ module DutyFree
|
|
182
181
|
# Find belongs_tos for this model to one more more other klasses
|
183
182
|
def self._find_belongs_tos(klass, to_klass, errored_assocs)
|
184
183
|
klass.reflect_on_all_associations.each_with_object([]) do |bt_assoc, s|
|
185
|
-
|
184
|
+
# .is_a?(ActiveRecord::Reflection::BelongsToReflection)
|
185
|
+
next unless bt_assoc.belongs_to? && !errored_assocs.include?(bt_assoc)
|
186
186
|
|
187
187
|
begin
|
188
|
-
s << bt_assoc if !bt_assoc.polymorphic
|
188
|
+
s << bt_assoc if !bt_assoc.options[:polymorphic] && bt_assoc.klass == to_klass
|
189
189
|
rescue NameError
|
190
190
|
errored_assocs << bt_assoc
|
191
191
|
puts "* In the #{bt_assoc.active_record.name} model \"belongs_to :#{bt_assoc.name}\" could not find a model named #{bt_assoc.class_name}."
|
@@ -203,9 +203,7 @@ module DutyFree
|
|
203
203
|
|
204
204
|
# Requireds takes its cues from all attributes having a presence validator
|
205
205
|
requireds = _find_requireds(klass)
|
206
|
-
if priority_excluded_columns
|
207
|
-
klass_columns = klass_columns.reject { |col| priority_excluded_columns.include?(col.name) }
|
208
|
-
end
|
206
|
+
klass_columns = klass_columns.reject { |col| priority_excluded_columns.include?(col.name) } if priority_excluded_columns
|
209
207
|
excluded_columns = %w[created_at updated_at deleted_at]
|
210
208
|
unique = [(
|
211
209
|
# Find the first text field of a required if one exists
|
@@ -240,8 +238,14 @@ module DutyFree
|
|
240
238
|
if klass.columns.map(&:name).include?(attrib)
|
241
239
|
s << attrib
|
242
240
|
else
|
243
|
-
|
244
|
-
|
241
|
+
bt_names = klass.reflect_on_all_associations.each_with_object([]) do |assoc, names|
|
242
|
+
names << assoc.name.to_s if assoc.is_a?(ActiveRecord::Reflection::BelongsToReflection)
|
243
|
+
names
|
244
|
+
end
|
245
|
+
unless bt_names.include?(attrib)
|
246
|
+
puts "* In the #{klass.name} model \"validates_presence_of :#{attrib}\" should be removed as it does not refer to any existing column."
|
247
|
+
errored_columns << klass_col
|
248
|
+
end
|
245
249
|
end
|
246
250
|
end
|
247
251
|
end
|
data/lib/duty_free/util.rb
CHANGED
@@ -35,20 +35,34 @@ module DutyFree
|
|
35
35
|
end
|
36
36
|
|
37
37
|
# ActiveRecord AREL objects
|
38
|
-
elsif piece.is_a?(Arel::Nodes::JoinSource)
|
39
|
-
# The left side is the "FROM" table
|
40
|
-
# names += _recurse_arel(piece.left)
|
41
|
-
# The right side is an array of all JOINs
|
42
|
-
names += piece.right.inject([]) { |s, v| s + _recurse_arel(v) }
|
43
38
|
elsif piece.is_a?(Arel::Nodes::Join) # INNER or OUTER JOIN
|
44
|
-
#
|
45
|
-
|
46
|
-
|
39
|
+
# rubocop:disable Style/IdenticalConditionalBranches
|
40
|
+
if piece.right.is_a?(Arel::Table) # Came in from AR < 3.2?
|
41
|
+
# Arel 2.x and older is a little curious because these JOINs work "back to front".
|
42
|
+
# The left side here is either another earlier JOIN, or at the end of the whole tree, it is
|
43
|
+
# the first table.
|
44
|
+
names += _recurse_arel(piece.left)
|
45
|
+
# The right side here at the top is the very last table, and anywhere else down the tree it is
|
46
|
+
# the later "JOIN" table of this pair. (The table that comes after all the rest of the JOINs
|
47
|
+
# from the left side.)
|
48
|
+
names << (piece.right.table_alias || piece.right.name)
|
49
|
+
else # "Normal" setup, fed from a JoinSource which has an array of JOINs
|
50
|
+
# The left side is the "JOIN" table
|
51
|
+
names += _recurse_arel(piece.left)
|
52
|
+
# (The right side of these is the "ON" clause)
|
53
|
+
end
|
54
|
+
# rubocop:enable Style/IdenticalConditionalBranches
|
47
55
|
elsif piece.is_a?(Arel::Table) # Table
|
48
|
-
names << piece.name
|
56
|
+
names << (piece.table_alias || piece.name)
|
49
57
|
elsif piece.is_a?(Arel::Nodes::TableAlias) # Alias
|
50
58
|
# Can get the real table name from: self._recurse_arel(piece.left)
|
51
59
|
names << piece.right.to_s # This is simply a string; the alias name itself
|
60
|
+
elsif piece.is_a?(Arel::Nodes::JoinSource) # Leaving this until the end because AR < 3.2 doesn't know at all about JoinSource!
|
61
|
+
# The left side is the "FROM" table
|
62
|
+
# names += _recurse_arel(piece.left)
|
63
|
+
names << (piece.left.table_alias || piece.left.name)
|
64
|
+
# The right side is an array of all JOINs
|
65
|
+
names += piece.right.inject([]) { |s, v| s + _recurse_arel(v) }
|
52
66
|
end
|
53
67
|
names
|
54
68
|
end
|
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.
|
4
|
+
version: 1.0.6
|
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-
|
11
|
+
date: 2020-11-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,20 +16,20 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '3.0'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '6.
|
22
|
+
version: '6.1'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
29
|
+
version: '3.0'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '6.
|
32
|
+
version: '6.1'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: appraisal
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -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:
|
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:
|
60
|
+
version: 3.7.0
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: ffaker
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -204,10 +204,9 @@ dependencies:
|
|
204
204
|
- - "~>"
|
205
205
|
- !ruby/object:Gem::Version
|
206
206
|
version: '1.4'
|
207
|
-
description:
|
208
|
-
|
209
|
-
|
210
|
-
XLSX, ODT, HTML tables, or simple Ruby arrays.
|
207
|
+
description: 'Simplify data imports and exports with this slick ActiveRecord extension
|
208
|
+
|
209
|
+
'
|
211
210
|
email: lorint@gmail.com
|
212
211
|
executables: []
|
213
212
|
extensions: []
|
@@ -243,7 +242,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
243
242
|
requirements:
|
244
243
|
- - ">="
|
245
244
|
- !ruby/object:Gem::Version
|
246
|
-
version: 2.
|
245
|
+
version: 2.3.5
|
247
246
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
248
247
|
requirements:
|
249
248
|
- - ">="
|