store_base_sti_class 1.2.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +5 -5
  2. data/Appraisals +8 -65
  3. data/Gemfile.lock +16 -18
  4. data/LICENSE.txt +1 -1
  5. data/README.md +20 -9
  6. data/gemfiles/rails_4.2.11.gemfile +8 -0
  7. data/gemfiles/rails_5.0.7.gemfile +8 -0
  8. data/gemfiles/rails_5.1.6.gemfile +8 -0
  9. data/gemfiles/rails_5.2.2.gemfile +8 -0
  10. data/gemfiles/rails_6.0.0.gemfile +8 -0
  11. data/lib/store_base_sti_class.rb +3 -5
  12. data/lib/store_base_sti_class/version.rb +1 -1
  13. data/lib/store_base_sti_class_for_5_0.rb +16 -0
  14. data/lib/store_base_sti_class_for_5_1.rb +16 -0
  15. data/lib/store_base_sti_class_for_6_0.rb +153 -0
  16. data/store_base_sti_class.gemspec +1 -2
  17. metadata +10 -85
  18. data/gemfiles/rails_4.0.0.gemfile +0 -7
  19. data/gemfiles/rails_4.0.1.gemfile +0 -7
  20. data/gemfiles/rails_4.0.10.gemfile +0 -7
  21. data/gemfiles/rails_4.0.11.1.gemfile +0 -7
  22. data/gemfiles/rails_4.0.11.gemfile +0 -7
  23. data/gemfiles/rails_4.0.12.gemfile +0 -7
  24. data/gemfiles/rails_4.0.13.gemfile +0 -7
  25. data/gemfiles/rails_4.0.2.gemfile +0 -7
  26. data/gemfiles/rails_4.0.3.gemfile +0 -7
  27. data/gemfiles/rails_4.0.4.gemfile +0 -7
  28. data/gemfiles/rails_4.0.5.gemfile +0 -7
  29. data/gemfiles/rails_4.0.6.gemfile +0 -7
  30. data/gemfiles/rails_4.0.7.gemfile +0 -7
  31. data/gemfiles/rails_4.0.8.gemfile +0 -7
  32. data/gemfiles/rails_4.0.9.gemfile +0 -7
  33. data/gemfiles/rails_4.1.0.gemfile +0 -7
  34. data/gemfiles/rails_4.1.1.gemfile +0 -7
  35. data/gemfiles/rails_4.1.10.gemfile +0 -7
  36. data/gemfiles/rails_4.1.11.gemfile +0 -7
  37. data/gemfiles/rails_4.1.12.gemfile +0 -7
  38. data/gemfiles/rails_4.1.13.gemfile +0 -7
  39. data/gemfiles/rails_4.1.14.1.gemfile +0 -7
  40. data/gemfiles/rails_4.1.14.2.gemfile +0 -7
  41. data/gemfiles/rails_4.1.14.gemfile +0 -7
  42. data/gemfiles/rails_4.1.15.gemfile +0 -7
  43. data/gemfiles/rails_4.1.2.gemfile +0 -7
  44. data/gemfiles/rails_4.1.3.gemfile +0 -7
  45. data/gemfiles/rails_4.1.4.gemfile +0 -7
  46. data/gemfiles/rails_4.1.5.gemfile +0 -7
  47. data/gemfiles/rails_4.1.6.gemfile +0 -7
  48. data/gemfiles/rails_4.1.7.1.gemfile +0 -7
  49. data/gemfiles/rails_4.1.7.gemfile +0 -7
  50. data/gemfiles/rails_4.1.8.gemfile +0 -7
  51. data/gemfiles/rails_4.1.9.gemfile +0 -7
  52. data/gemfiles/rails_4.2.0.gemfile +0 -7
  53. data/gemfiles/rails_4.2.1.gemfile +0 -7
  54. data/gemfiles/rails_4.2.10.gemfile +0 -7
  55. data/gemfiles/rails_4.2.2.gemfile +0 -7
  56. data/gemfiles/rails_4.2.3.gemfile +0 -7
  57. data/gemfiles/rails_4.2.4.gemfile +0 -7
  58. data/gemfiles/rails_4.2.5.1.gemfile +0 -7
  59. data/gemfiles/rails_4.2.5.2.gemfile +0 -7
  60. data/gemfiles/rails_4.2.5.gemfile +0 -7
  61. data/gemfiles/rails_4.2.6.gemfile +0 -7
  62. data/gemfiles/rails_4.2.7.1.gemfile +0 -7
  63. data/gemfiles/rails_4.2.7.gemfile +0 -7
  64. data/gemfiles/rails_4.2.8.gemfile +0 -7
  65. data/gemfiles/rails_4.2.9.gemfile +0 -7
  66. data/gemfiles/rails_5.0.0.1.gemfile +0 -7
  67. data/gemfiles/rails_5.0.0.gemfile +0 -7
  68. data/gemfiles/rails_5.0.1.gemfile +0 -7
  69. data/gemfiles/rails_5.0.2.gemfile +0 -7
  70. data/gemfiles/rails_5.0.3.gemfile +0 -7
  71. data/gemfiles/rails_5.0.4.gemfile +0 -7
  72. data/gemfiles/rails_5.0.5.gemfile +0 -7
  73. data/gemfiles/rails_5.0.6.gemfile +0 -7
  74. data/gemfiles/rails_5.1.0.gemfile +0 -7
  75. data/gemfiles/rails_5.1.1.gemfile +0 -7
  76. data/gemfiles/rails_5.1.2.gemfile +0 -7
  77. data/gemfiles/rails_5.1.3.gemfile +0 -7
  78. data/gemfiles/rails_5.1.4.gemfile +0 -7
  79. data/gemfiles/rails_5.1.5.gemfile +0 -7
  80. data/gemfiles/rails_5.2.0.gemfile +0 -7
  81. data/lib/store_base_sti_class_for_4_0.rb +0 -337
  82. data/lib/store_base_sti_class_for_4_1.rb +0 -360
  83. data/store_base_sti_class-1.1.0.gem +0 -0
@@ -1,360 +0,0 @@
1
- if ActiveRecord::VERSION::STRING =~ /^4\.1/
2
- module ActiveRecord
3
-
4
- class Base
5
- class_attribute :store_base_sti_class
6
- self.store_base_sti_class = true
7
- end
8
-
9
- module Associations
10
- class Association
11
-
12
- def creation_attributes
13
- attributes = {}
14
-
15
- if (reflection.macro == :has_one || reflection.macro == :has_many) && !options[:through]
16
- attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key]
17
-
18
- if reflection.options[:as]
19
- # START PATCH
20
- # original:
21
- # attributes[reflection.type] = owner.class.base_class.name
22
-
23
- attributes[reflection.type] = ActiveRecord::Base.store_base_sti_class ? owner.class.base_class.name : owner.class.name
24
-
25
- # END PATCH
26
- end
27
- end
28
-
29
- attributes
30
- end
31
- end
32
-
33
- class JoinDependency
34
- class JoinAssociation
35
- def join_constraints(foreign_table, foreign_klass, node, join_type, tables, scope_chain, chain)
36
- joins = []
37
- tables = tables.reverse
38
-
39
- scope_chain_index = 0
40
- scope_chain = scope_chain.reverse
41
-
42
- # The chain starts with the target table, but we want to end with it here (makes
43
- # more sense in this context), so we reverse
44
- chain.reverse_each do |reflection|
45
- table = tables.shift
46
- klass = reflection.klass
47
-
48
- case reflection.source_macro
49
- when :belongs_to
50
- key = reflection.association_primary_key
51
- foreign_key = reflection.foreign_key
52
- else
53
- key = reflection.foreign_key
54
- foreign_key = reflection.active_record_primary_key
55
- end
56
-
57
- constraint = build_constraint(klass, table, key, foreign_table, foreign_key)
58
-
59
- scope_chain_items = scope_chain[scope_chain_index].map do |item|
60
- if item.is_a?(Relation)
61
- item
62
- else
63
- ActiveRecord::Relation.create(klass, table).instance_exec(node, &item)
64
- end
65
- end
66
- scope_chain_index += 1
67
-
68
- default_scope = if klass.method(:build_default_scope).arity == 0
69
- klass.send(:build_default_scope)
70
- else
71
- klass.send(:build_default_scope, ActiveRecord::Relation.create(klass, table))
72
- end
73
- scope_chain_items.concat [default_scope].compact
74
-
75
- rel = scope_chain_items.inject(scope_chain_items.shift) do |left, right|
76
- left.merge right
77
- end
78
-
79
- if reflection.type
80
- # START PATCH
81
- # original:
82
- # constraint = constraint.and table[reflection.type].eq foreign_klass.base_class.name
83
-
84
- sti_class_name = ActiveRecord::Base.store_base_sti_class ? foreign_klass.base_class.name : foreign_klass.name
85
- constraint = constraint.and table[reflection.type].eq sti_class_name
86
-
87
- # END PATCH
88
- end
89
-
90
- if rel && !rel.arel.constraints.empty?
91
- constraint = constraint.and rel.arel.constraints
92
- end
93
-
94
- joins << table.create_join(table, table.create_on(constraint), join_type)
95
-
96
- # The current table in this iteration becomes the foreign table in the next
97
- foreign_table, foreign_klass = table, klass
98
- end
99
-
100
- joins
101
- end
102
- end
103
- end
104
-
105
- class BelongsToPolymorphicAssociation
106
- private
107
-
108
- def replace_keys(record)
109
- super
110
-
111
- # START PATCH
112
- # original:
113
- # owner[reflection.foreign_type] = record.class.base_class.name
114
-
115
- owner[reflection.foreign_type] = ActiveRecord::Base.store_base_sti_class ? record.class.base_class.name : record.class.name
116
-
117
- # END PATCH
118
- end
119
- end
120
-
121
- class Preloader
122
- class Association
123
- def build_scope
124
- scope = klass.unscoped
125
-
126
- values = reflection_scope.values
127
- preload_values = preload_scope.values
128
-
129
- scope.where_values = Array(values[:where]) + Array(preload_values[:where])
130
- scope.references_values = Array(values[:references]) + Array(preload_values[:references])
131
-
132
- select_method = scope.respond_to?(:select!) ? :select! : :_select!
133
- scope.send select_method, preload_values[:select] || values[:select] || table[Arel.star]
134
- scope.includes! preload_values[:includes] || values[:includes]
135
-
136
- if preload_values.key? :order
137
- scope.order! preload_values[:order]
138
- else
139
- if values.key? :order
140
- scope.order! values[:order]
141
- end
142
- end
143
-
144
- if options[:as]
145
- # START PATCH
146
- # original:
147
- # scope.where!(klass.table_name => { reflection.type => model.base_class.sti_name })
148
-
149
- scope.where!(klass.table_name => { reflection.type => ActiveRecord::Base.store_base_sti_class ? model.base_class.sti_name : model.sti_name })
150
-
151
- # END PATCH
152
- end
153
-
154
- scope.unscope_values = Array(values[:unscope])
155
- klass.default_scoped.merge(scope)
156
- end
157
- end
158
-
159
- module ThroughAssociation
160
- private
161
-
162
- def through_scope
163
- scope = through_reflection.klass.unscoped
164
-
165
- if options[:source_type]
166
- # BEGIN PATCH
167
- # original: scope.where! reflection.foreign_type => options[:source_type]
168
-
169
- adjusted_foreign_type = if ActiveRecord::Base.store_base_sti_class
170
- options[:source_type]
171
- else
172
- ([options[:source_type].constantize] + options[:source_type].constantize.descendants).map(&:to_s)
173
- end
174
-
175
- scope.where! reflection.foreign_type => adjusted_foreign_type
176
-
177
- # END PATCH
178
- else
179
- unless reflection_scope.where_values.empty?
180
- scope.includes_values = Array(reflection_scope.values[:includes] || options[:source])
181
- scope.where_values = reflection_scope.values[:where]
182
- end
183
-
184
- scope.references! reflection_scope.values[:references]
185
- scope.order! reflection_scope.values[:order] if scope.eager_loading?
186
- end
187
-
188
- scope
189
- end
190
- end
191
- end
192
-
193
- class AssociationScope
194
- def add_constraints(scope, owner, assoc_klass, refl, tracker)
195
- chain = refl.chain
196
- scope_chain = refl.scope_chain
197
-
198
- tables = construct_tables(chain, assoc_klass, refl, tracker)
199
-
200
- chain.each_with_index do |reflection, i|
201
- table, foreign_table = tables.shift, tables.first
202
-
203
- if reflection.source_macro == :belongs_to
204
- if reflection.options[:polymorphic]
205
- key = reflection.association_primary_key(assoc_klass)
206
- else
207
- key = reflection.association_primary_key
208
- end
209
-
210
- foreign_key = reflection.foreign_key
211
- else
212
- key = reflection.foreign_key
213
- foreign_key = reflection.active_record_primary_key
214
- end
215
-
216
- if reflection == chain.last
217
- bind_val = bind scope, table.table_name, key.to_s, owner[foreign_key], tracker
218
- scope = scope.where(table[key].eq(bind_val))
219
-
220
- if reflection.type
221
- # START PATCH
222
- # original: value = owner.class.base_class.name
223
-
224
- if ActiveRecord::Base.store_base_sti_class
225
- value = owner.class.base_class.name
226
- else
227
- value = owner.class.name
228
- end
229
-
230
- # END PATCH
231
-
232
- bind_val = bind scope, table.table_name, reflection.type.to_s, value, tracker
233
- scope = scope.where(table[reflection.type].eq(bind_val))
234
- end
235
- else
236
- constraint = table[key].eq(foreign_table[foreign_key])
237
-
238
- if reflection.type
239
- # START PATCH
240
- # original: type = chain[i + 1].klass.base_class.name
241
- # scope = scope.where(table[reflection.type].eq(bind_val))
242
-
243
- if ActiveRecord::Base.store_base_sti_class
244
- type = chain[i + 1].klass.base_class.name
245
- scope = scope.where(table[reflection.type].eq(type))
246
- else
247
- klass = chain[i + 1].klass
248
- scope = scope.where(table[reflection.type].in(([klass] + klass.descendants).map(&:name)))
249
- end
250
-
251
- # END PATCH
252
- end
253
-
254
- scope = scope.joins(join(foreign_table, constraint))
255
- end
256
-
257
- is_first_chain = i == 0
258
- klass = is_first_chain ? assoc_klass : reflection.klass
259
-
260
- # Exclude the scope of the association itself, because that
261
- # was already merged in the #scope method.
262
- scope_chain[i].each do |scope_chain_item|
263
- item = eval_scope(klass, scope_chain_item, owner)
264
-
265
- if scope_chain_item == refl.scope
266
- scope.merge! item.except(:where, :includes, :bind)
267
- end
268
-
269
- if is_first_chain
270
- scope.includes! item.includes_values
271
- end
272
-
273
- scope.where_values += item.where_values
274
- scope.order_values |= item.order_values
275
- end
276
- end
277
-
278
- scope
279
- end
280
- end
281
-
282
- module ThroughAssociation
283
- def construct_join_attributes(*records)
284
- ensure_mutable
285
-
286
- if source_reflection.association_primary_key(reflection.klass) == reflection.klass.primary_key
287
- join_attributes = { source_reflection.name => records }
288
- else
289
- join_attributes = {
290
- source_reflection.foreign_key =>
291
- records.map { |record|
292
- record.send(source_reflection.association_primary_key(reflection.klass))
293
- }
294
- }
295
- end
296
-
297
- if options[:source_type]
298
-
299
- # START PATCH
300
- # original:
301
- # join_attributes[source_reflection.foreign_type] =
302
- # records.map { |record| record.class.base_class.name }
303
-
304
- join_attributes[source_reflection.foreign_type] =
305
- records.map { |record| ActiveRecord::Base.store_base_sti_class ? record.class.base_class.name : record.class.name }
306
-
307
- # END PATCH
308
- end
309
-
310
- if records.count == 1
311
- Hash[join_attributes.map { |k, v| [k, v.first] }]
312
- else
313
- join_attributes
314
- end
315
- end
316
-
317
- end
318
-
319
- end
320
-
321
- module Reflection
322
- class ThroughReflection
323
- def scope_chain
324
- @scope_chain ||= begin
325
- scope_chain = source_reflection.scope_chain.map(&:dup)
326
-
327
- # Add to it the scope from this reflection (if any)
328
- scope_chain.first << scope if scope
329
-
330
- through_scope_chain = through_reflection.scope_chain.map(&:dup)
331
-
332
- if options[:source_type]
333
- type = foreign_type
334
-
335
- # START PATCH
336
- # original: source_type = options[:source_type]
337
-
338
- source_type = if ActiveRecord::Base.store_base_sti_class
339
- options[:source_type]
340
- else
341
- ([options[:source_type].constantize] + options[:source_type].constantize.descendants).map(&:to_s)
342
- end
343
-
344
- # END PATCH
345
-
346
- through_scope_chain.first << lambda { |object|
347
- where(type => source_type)
348
- }
349
- end
350
-
351
- # Recursively fill out the rest of the array from the through reflection
352
- scope_chain + through_scope_chain
353
- end
354
- end
355
- end
356
-
357
- end
358
- end
359
-
360
- end