Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 36 lines (29 sloc) 0.953 kB
57b7532 @dhh Work-in progress for providing better join model support and polymorp…
dhh authored
1 module ActiveRecord
fde9504 @rizwanreza Adds title to activerecord/lib/active_record/associations/*
rizwanreza authored
2 # = Active Record Belongs To Polymorphic Association
57b7532 @dhh Work-in progress for providing better join model support and polymorp…
dhh authored
3 module Associations
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
4 class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc:
7b56fb0 @jonleighton Fix private methods which are delegated to. This previously worked be…
jonleighton authored
5 def klass
6 type = owner[reflection.foreign_type]
7 type.presence && type.constantize
8 end
9
57b7532 @dhh Work-in progress for providing better join model support and polymorp…
dhh authored
10 private
81ca0cf @oggy Add inverse polymorphic association support. [#3520 state:resolved]
oggy authored
11
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
12 def replace_keys(record)
13 super
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
14 owner[reflection.foreign_type] = record && record.class.base_class.name
81ca0cf @oggy Add inverse polymorphic association support. [#3520 state:resolved]
oggy authored
15 end
16
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
17 def different_target?(record)
a5274bb @jonleighton Rename target_klass to klass
jonleighton authored
18 super || record.class != klass
81ca0cf @oggy Add inverse polymorphic association support. [#3520 state:resolved]
oggy authored
19 end
20
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
21 def inverse_reflection_for(record)
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
22 reflection.polymorphic_inverse_of(record.class)
57b7532 @dhh Work-in progress for providing better join model support and polymorp…
dhh authored
23 end
24
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
25 def raise_on_type_mismatch(record)
26 # A polymorphic association cannot have a type mismatch, by definition
57b7532 @dhh Work-in progress for providing better join model support and polymorp…
dhh authored
27 end
a0be389 @jonleighton Allow assignment on has_one :through where the owner is a new record …
jonleighton authored
28
29 def stale_state
0f3901e @kennyj @stale_state should be nil when a model isn't saved.
kennyj authored
30 foreign_key = super
31 foreign_key && [foreign_key.to_s, owner[reflection.foreign_type].to_s]
a0be389 @jonleighton Allow assignment on has_one :through where the owner is a new record …
jonleighton authored
32 end
57b7532 @dhh Work-in progress for providing better join model support and polymorp…
dhh authored
33 end
34 end
35 end
Something went wrong with that request. Please try again.