arin 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/arin/check.rb +60 -48
  3. data/lib/arin/version.rb +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4ebdd4498fb783ba99697abc74fe1843103a55c3
4
- data.tar.gz: 1d04dc64e80aa0f233b60b62766f7600a2ab814a
3
+ metadata.gz: 262fd640382d4faae63d8548a3cc0caecd920a3d
4
+ data.tar.gz: a6fdc63adb0bd46302e87e39eb38bbff8f37ee83
5
5
  SHA512:
6
- metadata.gz: b585aab9c5a817056b1f92638ce0a792e2f548d8e1e35542fe68f7bc76d84020cf660d403b497b614813efb16ae118837b50efcf4ffc0aba6ae54d625689335a
7
- data.tar.gz: 3ff2d9acbc5b9a2c2a9e34250631cd46490db7b75666b1628c78394aa9b3cb900fcabcd9ad951c0c1054f7119700081f74e6d62e581049a21e405089ed2b2e2b
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
- [].tap do |qs|
43
- classes.each do |klass|
44
- klass.reflect_on_all_associations(:belongs_to).each do |relation|
45
- begin
46
- if is_polymorphic?(relation)
47
- polymorphics(klass, relation).each do |poly|
48
- poly_class = poly.safe_constantize
49
- if poly_class
50
- qs << polymorphic_relation_query(klass, relation, poly_class)
51
- else
52
- qs << broken_polymorchic_class_query(klass, relation, poly)
53
- end
54
- end
55
- else
56
- qs << relation_query(klass, relation) if processable?(klass, relation)
57
- end
58
- rescue StandardError => e
59
- handle_query_failure(klass, relation, e)
60
- end
61
- end
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 processable?(klass, relation)
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?(relation.foreign_key)
81
+ klass.column_names.include?(assoc.foreign_key)
70
82
  end
71
83
 
72
- def relation_query(klass, relation)
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
- "#{relation.class_name}" AS relation_class,
77
- t.#{relation.foreign_key} AS relation_id
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 #{relation.table_name} AS r
80
- ON t.#{relation.foreign_key} = r.#{relation.association_primary_key}
81
- WHERE r.#{relation.association_primary_key} IS NULL
82
- AND t.#{relation.foreign_key} IS NOT NULL
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 polymorphic_relation_query(klass, relation, relation_class)
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
- "#{relation_class}" AS relation_class,
91
- t.#{relation.foreign_key} AS relation_id
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 #{relation_class.table_name} AS r
94
- ON t.#{relation.foreign_key} = r.#{relation_class.primary_key}
95
- AND t.#{relation.foreign_type} = "#{relation_class}"
96
- WHERE r.#{relation_class.primary_key} IS NULL
97
- AND t.#{relation.foreign_key} IS NOT NULL
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(klass, relation, relation_class)
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
- "#{relation_class}" AS relation_class,
106
- t.#{relation.foreign_key} AS relation_id
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.#{relation.foreign_type} = "#{relation_class}"
120
+ WHERE t.#{assoc.foreign_type} = "#{assoc_class_name}"
109
121
  SQL
110
122
  end
111
123
 
112
- def polymorphics(klass, relation)
113
- klass.pluck(relation.foreign_type).uniq.compact
124
+ def polymorphics(assoc, klass)
125
+ klass.pluck(assoc.foreign_type).uniq.compact
114
126
  end
115
127
 
116
- def is_polymorphic?(relation)
117
- relation.options[:polymorphic]
128
+ def is_polymorphic?(assoc)
129
+ assoc.options[:polymorphic]
118
130
  end
119
131
 
120
- def handle_query_failure(klass, relation, e)
121
- warn("Cannot process #{relation.name} relation for #{klass}: #{e.message}")
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
@@ -1,3 +1,3 @@
1
1
  module Arin
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - fmnoise@gmail.com