arin 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/arin/check.rb +60 -48
- data/lib/arin/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 262fd640382d4faae63d8548a3cc0caecd920a3d
|
4
|
+
data.tar.gz: a6fdc63adb0bd46302e87e39eb38bbff8f37ee83
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbe9e8550bb72cf7e552a826750b710c42b4bae839eed1e11c225e041fe680c728ea27c19bc9ade711acfaa36563f354c7cf5eae90faee5b758f263b1b185414
|
7
|
+
data.tar.gz: d69f2f7fc7ad348fc7a5afdad4d82381537fd9e2f623a3c31343387d9c9b4195d2c782a36997b575471fa6c3aaee1f2e95a2e06c816da5439dcf0ef1790f4c30
|
data/lib/arin/check.rb
CHANGED
@@ -33,92 +33,104 @@ module Arin
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def query
|
36
|
-
queries.join <<-SQL
|
36
|
+
queries.compact.join <<-SQL
|
37
37
|
UNION ALL
|
38
38
|
SQL
|
39
39
|
end
|
40
40
|
|
41
41
|
def queries
|
42
|
-
[]
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
42
|
+
classes.reduce([]) do |qs, klass|
|
43
|
+
qs.push(*class_queries(klass))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def class_queries klass
|
48
|
+
associations_for(klass).reduce([]) do |qs, assoc|
|
49
|
+
qs.push(*association_queries(assoc, klass))
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def association_queries assoc, klass
|
54
|
+
if is_polymorphic?(assoc)
|
55
|
+
polymorphic_association_queries(assoc, klass)
|
56
|
+
else
|
57
|
+
association_query(assoc, klass) if processable?(assoc, klass)
|
58
|
+
end
|
59
|
+
rescue StandardError => e
|
60
|
+
handle_query_failure(assoc, klass, e)
|
61
|
+
end
|
62
|
+
|
63
|
+
def polymorphic_association_queries assoc, klass
|
64
|
+
polymorphics(assoc, klass).map do |poly_class_name|
|
65
|
+
poly_class = poly_class_name.safe_constantize
|
66
|
+
if poly_class
|
67
|
+
polymorphic_association_query(assoc, klass, poly_class)
|
68
|
+
else
|
69
|
+
broken_polymorchic_class_query(assoc, klass, poly_class_name)
|
62
70
|
end
|
63
71
|
end
|
64
72
|
end
|
65
73
|
|
66
|
-
def
|
74
|
+
def associations_for klass
|
75
|
+
klass.reflect_on_all_associations(:belongs_to)
|
76
|
+
end
|
77
|
+
|
78
|
+
def processable?(assoc, klass)
|
67
79
|
klass.table_exists? &&
|
68
80
|
klass.primary_key &&
|
69
|
-
klass.column_names.include?(
|
81
|
+
klass.column_names.include?(assoc.foreign_key)
|
70
82
|
end
|
71
83
|
|
72
|
-
def
|
84
|
+
def association_query(assoc, klass)
|
73
85
|
<<-SQL
|
74
86
|
SELECT "#{klass.name}" AS class_name,
|
75
87
|
t.#{klass.primary_key} AS id,
|
76
|
-
"#{
|
77
|
-
t.#{
|
88
|
+
"#{assoc.class_name}" AS relation_class,
|
89
|
+
t.#{assoc.foreign_key} AS relation_id
|
78
90
|
FROM #{klass.table_name} AS t
|
79
|
-
LEFT JOIN #{
|
80
|
-
ON t.#{
|
81
|
-
WHERE r.#{
|
82
|
-
AND t.#{
|
91
|
+
LEFT JOIN #{assoc.table_name} AS r
|
92
|
+
ON t.#{assoc.foreign_key} = r.#{assoc.association_primary_key}
|
93
|
+
WHERE r.#{assoc.association_primary_key} IS NULL
|
94
|
+
AND t.#{assoc.foreign_key} IS NOT NULL
|
83
95
|
SQL
|
84
96
|
end
|
85
97
|
|
86
|
-
def
|
98
|
+
def polymorphic_association_query(assoc, klass, assoc_class)
|
87
99
|
<<-SQL
|
88
100
|
SELECT "#{klass.name}" AS class_name,
|
89
101
|
t.#{klass.primary_key} AS id,
|
90
|
-
"#{
|
91
|
-
t.#{
|
102
|
+
"#{assoc_class}" AS relation_class,
|
103
|
+
t.#{assoc.foreign_key} AS relation_id
|
92
104
|
FROM #{klass.table_name} AS t
|
93
|
-
LEFT JOIN #{
|
94
|
-
ON t.#{
|
95
|
-
AND t.#{
|
96
|
-
WHERE r.#{
|
97
|
-
AND t.#{
|
105
|
+
LEFT JOIN #{assoc_class.table_name} AS r
|
106
|
+
ON t.#{assoc.foreign_key} = r.#{assoc_class.primary_key}
|
107
|
+
AND t.#{assoc.foreign_type} = "#{assoc_class}"
|
108
|
+
WHERE r.#{assoc_class.primary_key} IS NULL
|
109
|
+
AND t.#{assoc.foreign_key} IS NOT NULL
|
98
110
|
SQL
|
99
111
|
end
|
100
112
|
|
101
|
-
def broken_polymorphic_class_query(
|
113
|
+
def broken_polymorphic_class_query(assoc, klass, assoc_class_name)
|
102
114
|
<<-SQL
|
103
115
|
SELECT "#{klass.name}" AS class_name,
|
104
116
|
t.#{klass.primary_key} AS id,
|
105
|
-
"#{
|
106
|
-
t.#{
|
117
|
+
"#{assoc_class}" AS relation_class,
|
118
|
+
t.#{assoc.foreign_key} AS relation_id
|
107
119
|
FROM #{klass.table_name} AS t
|
108
|
-
WHERE t.#{
|
120
|
+
WHERE t.#{assoc.foreign_type} = "#{assoc_class_name}"
|
109
121
|
SQL
|
110
122
|
end
|
111
123
|
|
112
|
-
def polymorphics(
|
113
|
-
klass.pluck(
|
124
|
+
def polymorphics(assoc, klass)
|
125
|
+
klass.pluck(assoc.foreign_type).uniq.compact
|
114
126
|
end
|
115
127
|
|
116
|
-
def is_polymorphic?(
|
117
|
-
|
128
|
+
def is_polymorphic?(assoc)
|
129
|
+
assoc.options[:polymorphic]
|
118
130
|
end
|
119
131
|
|
120
|
-
def handle_query_failure(
|
121
|
-
warn("Cannot process #{
|
132
|
+
def handle_query_failure(assoc, klass, e)
|
133
|
+
warn("Cannot process #{assoc.name} relation for #{klass}: #{e.message}")
|
122
134
|
end
|
123
135
|
end
|
124
136
|
end
|
data/lib/arin/version.rb
CHANGED