iron_trail 0.2.0 → 0.2.1
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/iron_trail/reifier.rb +21 -16
- data/lib/iron_trail/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d9d355d0b56b36b299f0eac1f43dea8ba843358119a8d2d02ddce34528f504f7
|
|
4
|
+
data.tar.gz: e0915938859c2263226ee04fac0ae56ab7f11782dab1e51f38cb745ded6edca2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6349be50c371976cc781d8f0921d4112e97230086c81f0cc6886e66ff35c128c7ddf9d5db67393a8b77d573ad39b81595e359071e7f6a0eb2e38f040d4e795a9
|
|
7
|
+
data.tar.gz: 96f17b9eaab3ed0ec8963a96b2f4d9231b43c0e5171db548ae29f59234282ee66ff6ae63a44ee7b807d9d30fc652a714157de5ab75cda68b2e7770fddf7ab2d3
|
data/lib/iron_trail/reifier.rb
CHANGED
|
@@ -25,27 +25,32 @@ module IronTrail
|
|
|
25
25
|
record
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
-
def self.model_from_table_name(table_name, sti_type=nil)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
val[0]
|
|
33
|
-
else
|
|
34
|
-
val.to_h { |k| [k.to_s, k] }
|
|
35
|
-
end
|
|
28
|
+
def self.model_from_table_name(table_name, sti_type = nil)
|
|
29
|
+
candidates = ActiveRecord::Base.descendants
|
|
30
|
+
.reject(&:abstract_class)
|
|
31
|
+
.select { |klass| klass.table_name == table_name }
|
|
36
32
|
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
raise "Cannot infer model from table named '#{table_name}'" if candidates.empty?
|
|
34
|
+
return candidates.first if candidates.one?
|
|
39
35
|
|
|
40
|
-
|
|
41
|
-
|
|
36
|
+
if sti_type.present?
|
|
37
|
+
klass = candidates.find { |c| c.name == sti_type }
|
|
38
|
+
return klass if klass
|
|
39
|
+
|
|
40
|
+
raise "Cannot infer STI model for table #{table_name} and type '#{sti_type}'"
|
|
41
|
+
end
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
# When sti_type is nil and multiple classes share the table,
|
|
44
|
+
# filter out STI subclasses to find the base class.
|
|
45
|
+
bases = candidates.reject { |c| candidates.any? { |other| other != c && c < other } }
|
|
46
|
+
return bases.first if bases.one?
|
|
45
47
|
|
|
46
|
-
|
|
48
|
+
# If multiple base classes remain, prefer the one that is an STI base
|
|
49
|
+
# (i.e., has subclasses among the original candidates).
|
|
50
|
+
sti_bases = bases.select { |c| candidates.any? { |other| other != c && other < c } }
|
|
51
|
+
return sti_bases.first if sti_bases.one?
|
|
47
52
|
|
|
48
|
-
raise "Cannot infer
|
|
53
|
+
raise "Cannot infer model from table named '#{table_name}'"
|
|
49
54
|
end
|
|
50
55
|
end
|
|
51
56
|
end
|
data/lib/iron_trail/version.rb
CHANGED