store_base_sti_class 0.5.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/Appraisals +5 -57
  3. data/Gemfile.lock +6 -9
  4. data/README.md +8 -11
  5. data/debug.log +12652 -0
  6. data/gemfiles/rails_3.0.5.gemfile.lock +3 -3
  7. data/gemfiles/rails_3.0.6.gemfile.lock +3 -3
  8. data/gemfiles/rails_3.0.7.gemfile.lock +3 -3
  9. data/gemfiles/rails_3.2.14.gemfile.lock +1 -4
  10. data/gemfiles/rails_4.0.0.gemfile.lock +2 -5
  11. data/gemfiles/rails_4.0.1.gemfile.lock +2 -5
  12. data/gemfiles/rails_4.0.10.gemfile.lock +2 -5
  13. data/gemfiles/rails_4.0.11.1.gemfile.lock +2 -5
  14. data/gemfiles/rails_4.0.11.gemfile.lock +2 -5
  15. data/gemfiles/rails_4.0.12.gemfile.lock +2 -5
  16. data/gemfiles/rails_4.0.13.gemfile.lock +2 -5
  17. data/gemfiles/rails_4.0.2.gemfile.lock +2 -5
  18. data/gemfiles/rails_4.0.3.gemfile.lock +2 -5
  19. data/gemfiles/rails_4.0.4.gemfile.lock +2 -5
  20. data/gemfiles/rails_4.0.5.gemfile.lock +2 -5
  21. data/gemfiles/rails_4.0.6.gemfile.lock +2 -5
  22. data/gemfiles/rails_4.0.7.gemfile.lock +2 -5
  23. data/gemfiles/rails_4.0.8.gemfile.lock +2 -5
  24. data/gemfiles/rails_4.0.9.gemfile.lock +2 -5
  25. data/gemfiles/rails_4.1.0.gemfile.lock +2 -5
  26. data/gemfiles/rails_4.1.1.gemfile.lock +2 -5
  27. data/gemfiles/rails_4.1.10.gemfile.lock +2 -5
  28. data/gemfiles/rails_4.1.11.gemfile.lock +2 -5
  29. data/gemfiles/rails_4.1.12.gemfile.lock +2 -5
  30. data/gemfiles/rails_4.1.13.gemfile.lock +48 -0
  31. data/gemfiles/rails_4.1.14.1.gemfile.lock +48 -0
  32. data/gemfiles/rails_4.1.14.2.gemfile.lock +48 -0
  33. data/gemfiles/rails_4.1.14.gemfile.lock +48 -0
  34. data/gemfiles/rails_4.1.15.gemfile.lock +48 -0
  35. data/gemfiles/rails_4.1.2.gemfile.lock +2 -5
  36. data/gemfiles/rails_4.1.3.gemfile.lock +2 -5
  37. data/gemfiles/rails_4.1.4.gemfile.lock +2 -5
  38. data/gemfiles/rails_4.1.5.gemfile.lock +2 -5
  39. data/gemfiles/rails_4.1.6.gemfile.lock +2 -5
  40. data/gemfiles/rails_4.1.7.1.gemfile.lock +2 -5
  41. data/gemfiles/rails_4.1.7.gemfile.lock +2 -5
  42. data/gemfiles/rails_4.1.8.gemfile.lock +2 -5
  43. data/gemfiles/rails_4.1.9.gemfile.lock +2 -5
  44. data/gemfiles/rails_4.2.0.gemfile.lock +2 -5
  45. data/gemfiles/rails_4.2.1.gemfile.lock +2 -5
  46. data/gemfiles/rails_4.2.2.gemfile.lock +2 -5
  47. data/gemfiles/rails_4.2.3.gemfile.lock +2 -5
  48. data/gemfiles/rails_4.2.4.gemfile.lock +48 -0
  49. data/gemfiles/rails_4.2.5.1.gemfile.lock +48 -0
  50. data/gemfiles/rails_4.2.5.2.gemfile.lock +48 -0
  51. data/gemfiles/rails_4.2.5.gemfile.lock +48 -0
  52. data/gemfiles/rails_4.2.6.gemfile.lock +48 -0
  53. data/gemfiles/{rails_3.0.5.gemfile → rails_4.2.7.1.gemfile} +1 -2
  54. data/gemfiles/{rails_3.0.6.gemfile → rails_4.2.7.gemfile} +1 -2
  55. data/gemfiles/{rails_3.0.7.gemfile → rails_5.0.0.1.gemfile} +1 -2
  56. data/gemfiles/{rails_3.0.8.gemfile → rails_5.0.0.gemfile} +1 -2
  57. data/gemfiles/rails_5.0.1.gemfile +7 -0
  58. data/gemfiles/rails_5.0.1.gemfile.lock +48 -0
  59. data/lib/store_base_sti_class.rb +3 -5
  60. data/lib/store_base_sti_class/version.rb +1 -1
  61. data/lib/store_base_sti_class_for_5_0.rb +404 -0
  62. data/store_base_sti_class.gemspec +3 -3
  63. data/storebasestiname_unittest.sql +0 -0
  64. metadata +26 -64
  65. data/gemfiles/rails_3.0.10.gemfile +0 -8
  66. data/gemfiles/rails_3.0.11.gemfile +0 -8
  67. data/gemfiles/rails_3.0.12.gemfile +0 -8
  68. data/gemfiles/rails_3.0.13.gemfile +0 -8
  69. data/gemfiles/rails_3.0.14.gemfile +0 -8
  70. data/gemfiles/rails_3.0.15.gemfile +0 -8
  71. data/gemfiles/rails_3.0.16.gemfile +0 -8
  72. data/gemfiles/rails_3.0.17.gemfile +0 -8
  73. data/gemfiles/rails_3.0.18.gemfile +0 -8
  74. data/gemfiles/rails_3.0.19.gemfile +0 -8
  75. data/gemfiles/rails_3.0.20.gemfile +0 -8
  76. data/gemfiles/rails_3.0.9.gemfile +0 -8
  77. data/gemfiles/rails_3.1.0.gemfile +0 -8
  78. data/gemfiles/rails_3.1.10.gemfile +0 -8
  79. data/gemfiles/rails_3.1.11.gemfile +0 -8
  80. data/gemfiles/rails_3.1.12.gemfile +0 -8
  81. data/gemfiles/rails_3.1.2.gemfile +0 -8
  82. data/gemfiles/rails_3.1.3.gemfile +0 -8
  83. data/gemfiles/rails_3.1.4.gemfile +0 -8
  84. data/gemfiles/rails_3.1.5.gemfile +0 -8
  85. data/gemfiles/rails_3.1.6.gemfile +0 -8
  86. data/gemfiles/rails_3.1.7.gemfile +0 -8
  87. data/gemfiles/rails_3.1.8.gemfile +0 -8
  88. data/gemfiles/rails_3.1.9.gemfile +0 -8
  89. data/gemfiles/rails_3.2.0.gemfile +0 -8
  90. data/gemfiles/rails_3.2.1.gemfile +0 -8
  91. data/gemfiles/rails_3.2.10.gemfile +0 -8
  92. data/gemfiles/rails_3.2.11.gemfile +0 -8
  93. data/gemfiles/rails_3.2.12.gemfile +0 -8
  94. data/gemfiles/rails_3.2.13.gemfile +0 -8
  95. data/gemfiles/rails_3.2.14.gemfile +0 -8
  96. data/gemfiles/rails_3.2.15.gemfile +0 -8
  97. data/gemfiles/rails_3.2.16.gemfile +0 -8
  98. data/gemfiles/rails_3.2.17.gemfile +0 -8
  99. data/gemfiles/rails_3.2.18.gemfile +0 -8
  100. data/gemfiles/rails_3.2.19.gemfile +0 -8
  101. data/gemfiles/rails_3.2.2.gemfile +0 -8
  102. data/gemfiles/rails_3.2.20.gemfile +0 -8
  103. data/gemfiles/rails_3.2.21.gemfile +0 -8
  104. data/gemfiles/rails_3.2.22.1.gemfile +0 -8
  105. data/gemfiles/rails_3.2.22.2.gemfile +0 -8
  106. data/gemfiles/rails_3.2.22.gemfile +0 -8
  107. data/gemfiles/rails_3.2.3.gemfile +0 -8
  108. data/gemfiles/rails_3.2.4.gemfile +0 -8
  109. data/gemfiles/rails_3.2.5.gemfile +0 -8
  110. data/gemfiles/rails_3.2.6.gemfile +0 -8
  111. data/gemfiles/rails_3.2.7.gemfile +0 -8
  112. data/gemfiles/rails_3.2.8.gemfile +0 -8
  113. data/gemfiles/rails_3.2.9.gemfile +0 -8
  114. data/lib/store_base_sti_class_for_3_0.rb +0 -470
  115. data/lib/store_base_sti_class_for_3_1_and_above.rb +0 -304
@@ -1,304 +0,0 @@
1
- require 'active_record'
2
-
3
- if ActiveRecord::VERSION::STRING =~ /^3\.(1|2)/
4
- module ActiveRecord
5
-
6
- class Base
7
- class_attribute :store_base_sti_class
8
- self.store_base_sti_class = true
9
- end
10
-
11
- module Associations
12
- class Association
13
-
14
- def creation_attributes
15
- attributes = {}
16
-
17
- if reflection.macro.in?([:has_one, :has_many]) && !options[:through]
18
- attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key]
19
-
20
- if reflection.options[:as]
21
- # START PATCH
22
- # original:
23
- # attributes[reflection.type] = owner.class.base_class.name
24
-
25
- attributes[reflection.type] = ActiveRecord::Base.store_base_sti_class ? owner.class.base_class.name : owner.class.name
26
-
27
- # END PATCH
28
- end
29
- end
30
-
31
- attributes
32
- end
33
-
34
- end
35
-
36
- class JoinDependency # :nodoc:
37
- class JoinAssociation < JoinPart # :nodoc:
38
- def join_to(relation)
39
-
40
- tables = @tables.dup
41
- foreign_table = parent_table
42
- foreign_klass = parent.active_record
43
-
44
- # The chain starts with the target table, but we want to end with it here (makes
45
- # more sense in this context), so we reverse
46
- chain.reverse.each_with_index do |reflection, i|
47
- table = tables.shift
48
-
49
- case reflection.source_macro
50
- when :belongs_to
51
- key = reflection.association_primary_key
52
- foreign_key = reflection.foreign_key
53
- when :has_and_belongs_to_many
54
- # Join the join table first...
55
- relation.from(join(
56
- table,
57
- table[reflection.foreign_key].
58
- eq(foreign_table[reflection.active_record_primary_key])
59
- ))
60
-
61
- foreign_table, table = table, tables.shift
62
-
63
- key = reflection.association_primary_key
64
- foreign_key = reflection.association_foreign_key
65
- else
66
- key = reflection.foreign_key
67
- foreign_key = reflection.active_record_primary_key
68
- end
69
-
70
- constraint = build_constraint(reflection, table, key, foreign_table, foreign_key)
71
-
72
- conditions = self.conditions[i].dup
73
-
74
- # START PATCH
75
- # original:
76
- # conditions << { reflection.type => foreign_klass.base_class.name } if reflection.type
77
-
78
- if ActiveRecord::Base.store_base_sti_class
79
- conditions << { reflection.type => foreign_klass.base_class.name } if reflection.type
80
- else
81
- conditions << { reflection.type => ([foreign_klass] + foreign_klass.descendants).map(&:name) } if reflection.type
82
- end
83
-
84
- # END PATCH
85
-
86
- unless conditions.empty?
87
- constraint = constraint.and(sanitize(conditions, table))
88
- end
89
-
90
- relation.from(join(table, constraint))
91
-
92
- # The current table in this iteration becomes the foreign table in the next
93
- foreign_table, foreign_klass = table, reflection.klass
94
- end
95
-
96
- relation
97
- end
98
- end
99
- end
100
-
101
-
102
- class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc:
103
-
104
- private
105
-
106
- def replace_keys(record)
107
- super
108
- # START PATCH
109
- # original: owner[reflection.foreign_type] = record && record.class.base_class.name
110
- unless ActiveRecord::Base.store_base_sti_class
111
- owner[reflection.foreign_type] = record && record.class.sti_name
112
- else
113
- owner[reflection.foreign_type] = record && record.class.base_class.name
114
- end
115
- #END PATCH
116
- end
117
- end
118
- end
119
- module Associations
120
- class Preloader
121
- class Association
122
- private
123
- def build_scope
124
-
125
- scope = klass.scoped
126
-
127
- scope = scope.where(process_conditions(options[:conditions]))
128
- scope = scope.where(process_conditions(preload_options[:conditions]))
129
-
130
- scope = scope.select(preload_options[:select] || options[:select] || table[Arel.star])
131
- scope = scope.includes(preload_options[:include] || options[:include])
132
-
133
-
134
-
135
- if options[:as]
136
- scope = scope.where(
137
- klass.table_name => {
138
- #START PATCH
139
- #original: reflection.type => model.base_class.sti_name
140
- reflection.type => ActiveRecord::Base.store_base_sti_class ? model.base_class.sti_name : model.sti_name
141
- #END PATCH
142
-
143
- }
144
- )
145
- end
146
-
147
- scope
148
- end
149
- end
150
-
151
- module ThroughAssociation
152
- def through_options
153
- through_options = {}
154
- if options[:source_type]
155
- #START PATCH
156
- #original: through_options[:conditions] = { reflection.foreign_type => options[:source_type] }
157
- through_options[:conditions] = { reflection.foreign_type => ([options[:source_type].constantize] + options[:source_type].constantize.descendants).map(&:to_s) }
158
- #END PATCH
159
- else
160
- if options[:conditions]
161
- through_options[:include] = options[:include] || options[:source]
162
- through_options[:conditions] = options[:conditions]
163
- end
164
-
165
- through_options[:order] = options[:order]
166
- end
167
- through_options
168
- end
169
- end
170
- end
171
-
172
- class AssociationScope
173
- def add_constraints(scope)
174
-
175
- tables = construct_tables
176
-
177
- chain.each_with_index do |reflection, i|
178
- table, foreign_table = tables.shift, tables.first
179
-
180
- if reflection.source_macro == :has_and_belongs_to_many
181
- join_table = tables.shift
182
-
183
- scope = scope.joins(join(
184
- join_table,
185
- table[reflection.association_primary_key].
186
- eq(join_table[reflection.association_foreign_key])
187
- ))
188
-
189
- table, foreign_table = join_table, tables.first
190
- end
191
-
192
- if reflection.source_macro == :belongs_to
193
- if reflection.options[:polymorphic]
194
- # START PATCH
195
- # This line exists to support multiple versions of AR 3.1
196
- # original in 3.1.3: key = reflection.association_primary_key
197
-
198
- key = (reflection.method(:association_primary_key).arity == 0) ? reflection.association_primary_key : reflection.association_primary_key(klass)
199
- # END PATCH
200
- else
201
- key = reflection.association_primary_key
202
- end
203
-
204
- foreign_key = reflection.foreign_key
205
- else
206
- key = reflection.foreign_key
207
- foreign_key = reflection.active_record_primary_key
208
- end
209
-
210
- conditions = self.conditions[i]
211
-
212
- if reflection == chain.last
213
- scope = scope.where(table[key].eq(owner[foreign_key]))
214
-
215
- if reflection.type
216
- # START PATCH
217
- # original: scope = scope.where(table[reflection.type].eq(owner.class.base_class.name))
218
-
219
- unless ActiveRecord::Base.store_base_sti_class
220
- scope = scope.where(table[reflection.type].eq(owner.class.name))
221
- else
222
- scope = scope.where(table[reflection.type].eq(owner.class.base_class.name))
223
- end
224
-
225
- # END PATCH
226
- end
227
-
228
- conditions.each do |condition|
229
- if options[:through] && condition.is_a?(Hash)
230
- condition = { table.name => condition }
231
- end
232
-
233
- scope = scope.where(interpolate(condition))
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
- # constraint = constraint.and(table[reflection.type].eq(type))
242
-
243
- if ActiveRecord::Base.store_base_sti_class
244
- type = chain[i + 1].klass.base_class.name
245
- constraint = constraint.and(table[reflection.type].eq(type))
246
- else
247
- klass = chain[i + 1].klass
248
- constraint = constraint.and(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
-
256
- unless conditions.empty?
257
- scope = scope.where(sanitize(conditions, table))
258
- end
259
- end
260
- end
261
-
262
- scope
263
- end
264
-
265
- end
266
- end
267
- module Reflection
268
- class ThroughReflection < AssociationReflection
269
-
270
- def conditions
271
- @conditions ||= begin
272
- conditions = source_reflection.conditions.map { |c| c.dup }
273
-
274
- # Add to it the conditions from this reflection if necessary.
275
- conditions.first << options[:conditions] if options[:conditions]
276
-
277
- through_conditions = through_reflection.conditions
278
-
279
- if options[:source_type]
280
- # START PATCH
281
- # original: through_conditions.first << { foreign_type => options[:source_type] }
282
-
283
- unless ActiveRecord::Base.store_base_sti_class
284
- through_conditions.first << { foreign_type => ([options[:source_type].constantize] + options[:source_type].constantize.descendants).map(&:to_s) }
285
- else
286
- through_conditions.first << { foreign_type => options[:source_type] }
287
- end
288
-
289
- # END PATCH
290
- end
291
-
292
- # Recursively fill out the rest of the array from the through reflection
293
- conditions += through_conditions
294
-
295
- # And return
296
- conditions
297
- end
298
- end
299
- end
300
- end
301
-
302
- end
303
-
304
- end