Class: ActiveRecord::Reflection::AbstractReflection
- Inherits:
-
Object
- Object
- ActiveRecord::Reflection::AbstractReflection
show all
- Defined in:
- activerecord/lib/active_record/reflection.rb
Overview
Defined Under Namespace
Classes: JoinKeys
Instance Method Summary
collapse
Instance Method Details
#alias_candidate(name) ⇒ Object
294
295
296
|
# File 'activerecord/lib/active_record/reflection.rb', line 294
def alias_candidate(name)
"#{plural_name}_#{name}"
end
|
#build_association(attributes, &block) ⇒ Object
Returns a new, unsaved instance of the associated class. attributes
will be passed to the class’s constructor.
153
154
155
|
# File 'activerecord/lib/active_record/reflection.rb', line 153
def build_association(attributes, &block)
klass.new(attributes, &block)
end
|
#build_join_constraint(table, foreign_table) ⇒ Object
190
191
192
193
194
195
196
197
198
199
200
201
|
# File 'activerecord/lib/active_record/reflection.rb', line 190
def build_join_constraint(table, foreign_table)
key = join_keys.key
foreign_key = join_keys.foreign_key
constraint = table[key].eq(foreign_table[foreign_key])
if klass.finder_needs_type_condition?
table.create_and([constraint, klass.send(:type_condition, table)])
else
constraint
end
end
|
#build_scope(table, predicate_builder = predicate_builder(table)) ⇒ Object
306
307
308
|
# File 'activerecord/lib/active_record/reflection.rb', line 306
def build_scope(table, predicate_builder = predicate_builder(table))
Relation.create(klass, table, predicate_builder)
end
|
298
299
300
|
# File 'activerecord/lib/active_record/reflection.rb', line 298
def chain
collect_join_chain
end
|
#check_validity_of_inverse! ⇒ Object
250
251
252
253
254
255
256
|
# File 'activerecord/lib/active_record/reflection.rb', line 250
def check_validity_of_inverse!
unless polymorphic?
if has_inverse? && inverse_of.nil?
raise InverseOfAssociationNotFoundError.new(self)
end
end
end
|
#class_name ⇒ Object
Returns the class name for the macro.
composed_of :balance, class_name: 'Money'
returns 'Money'
has_many :clients
returns 'Client'
169
170
171
|
# File 'activerecord/lib/active_record/reflection.rb', line 169
def class_name
@class_name ||= (options[:class_name] || derive_class_name).to_s
end
|
#constraints ⇒ Object
228
229
230
|
# File 'activerecord/lib/active_record/reflection.rb', line 228
def constraints
chain.flat_map(&:scopes)
end
|
#counter_cache_column ⇒ Object
232
233
234
235
236
237
238
239
240
241
242
|
# File 'activerecord/lib/active_record/reflection.rb', line 232
def counter_cache_column
if belongs_to?
if options[:counter_cache] == true
"#{active_record.name.demodulize.underscore.pluralize}_count"
elsif options[:counter_cache]
options[:counter_cache].to_s
end
else
options[:counter_cache] ? options[:counter_cache].to_s : "#{name}_count"
end
end
|
#counter_must_be_updated_by_has_many? ⇒ Boolean
290
291
292
|
# File 'activerecord/lib/active_record/reflection.rb', line 290
def counter_must_be_updated_by_has_many?
!inverse_updates_counter_in_memory? && has_cached_counter?
end
|
#get_join_keys(association_klass) ⇒ Object
302
303
304
|
# File 'activerecord/lib/active_record/reflection.rb', line 302
def get_join_keys(association_klass)
JoinKeys.new(join_pk(association_klass), join_foreign_key)
end
|
#has_cached_counter? ⇒ Boolean
Returns whether a counter cache should be used for this association.
The counter_cache option must be given on either the owner or inverse association, and the column must be present on the owner.
284
285
286
287
288
|
# File 'activerecord/lib/active_record/reflection.rb', line 284
def has_cached_counter?
options[:counter_cache] ||
inverse_which_updates_counter_cache && inverse_which_updates_counter_cache.options[:counter_cache] &&
!!active_record.columns_hash[counter_cache_column]
end
|
#inverse_of ⇒ Object
244
245
246
247
248
|
# File 'activerecord/lib/active_record/reflection.rb', line 244
def inverse_of
return unless inverse_name
@inverse_of ||= klass._reflect_on_association inverse_name
end
|
#inverse_updates_counter_in_memory? ⇒ Boolean
276
277
278
|
# File 'activerecord/lib/active_record/reflection.rb', line 276
def inverse_updates_counter_in_memory?
inverse_of && inverse_which_updates_counter_cache == inverse_of
end
|
#inverse_which_updates_counter_cache ⇒ Object
Also known as:
inverse_updates_counter_cache?
This shit is nasty. We need to avoid the following situation:
* An associated record is deleted via record.destroy
* Hence the callbacks run, and they find a belongs_to on the record with a
:counter_cache options which points back at our owner. So they update the
counter cache.
* In which case, we must make sure to *not* update the counter cache, or else
it will be decremented twice.
Hence this method.
268
269
270
271
272
273
|
# File 'activerecord/lib/active_record/reflection.rb', line 268
def inverse_which_updates_counter_cache
return @inverse_which_updates_counter_cache if defined?(@inverse_which_updates_counter_cache)
@inverse_which_updates_counter_cache = klass.reflect_on_all_associations(:belongs_to).find do |inverse|
inverse.counter_cache_column == counter_cache_column
end
end
|
#join_foreign_key ⇒ Object
310
311
312
|
# File 'activerecord/lib/active_record/reflection.rb', line 310
def join_foreign_key
active_record_primary_key
end
|
#join_keys ⇒ Object
175
176
177
|
# File 'activerecord/lib/active_record/reflection.rb', line 175
def join_keys
@join_keys ||= get_join_keys(klass)
end
|
#join_scope(table, foreign_klass) ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
|
# File 'activerecord/lib/active_record/reflection.rb', line 203
def join_scope(table, foreign_klass)
predicate_builder = predicate_builder(table)
scope_chain_items = join_scopes(table, predicate_builder)
klass_scope = klass_join_scope(table, predicate_builder)
if type
klass_scope.where!(type => foreign_klass.base_class.sti_name)
end
scope_chain_items.inject(klass_scope, &:merge!)
end
|
#join_scopes(table, predicate_builder) ⇒ Object
215
216
217
218
219
220
221
|
# File 'activerecord/lib/active_record/reflection.rb', line 215
def join_scopes(table, predicate_builder) if scope
[build_scope(table, predicate_builder).instance_exec(&scope)]
else
[]
end
end
|
#klass_join_scope(table, predicate_builder) ⇒ Object
223
224
225
226
|
# File 'activerecord/lib/active_record/reflection.rb', line 223
def klass_join_scope(table, predicate_builder) relation = build_scope(table, predicate_builder)
klass.scope_for_association(relation)
end
|
#primary_key_type ⇒ Object
161
162
163
|
# File 'activerecord/lib/active_record/reflection.rb', line 161
def primary_key_type
klass.type_for_attribute(klass.primary_key)
end
|
#quoted_table_name ⇒ Object
157
158
159
|
# File 'activerecord/lib/active_record/reflection.rb', line 157
def quoted_table_name
klass.quoted_table_name
end
|
#scope_chain ⇒ Object
185
186
187
|
# File 'activerecord/lib/active_record/reflection.rb', line 185
def scope_chain
chain.map(&:scopes)
end
|
Returns a list of scopes that should be applied for this Reflection object when querying the database.
181
182
183
|
# File 'activerecord/lib/active_record/reflection.rb', line 181
def scopes
scope ? [scope] : []
end
|
#table_name ⇒ Object
147
148
149
|
# File 'activerecord/lib/active_record/reflection.rb', line 147
def table_name
klass.table_name
end
|
#through_reflection? ⇒ Boolean
143
144
145
|
# File 'activerecord/lib/active_record/reflection.rb', line 143
def through_reflection?
false
end
|