Skip to content
Newer
Older
100644 93 lines (74 sloc) 2.31 KB
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
1 module ActiveRecord
5c9d430 @libinenjoy BELONGS TO ASSOCIATION | Typo fix
libinenjoy authored
2 # = Active Record Belongs To Association
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
3 module Associations
ef79b91 @jonleighton Abstract common code from BelongsToAssociation and HasOneAssociation …
jonleighton authored
4 class BelongsToAssociation < SingularAssociation #:nodoc:
ea8181b @jonleighton move dependency logic out of generated methods
jonleighton authored
5
6 def handle_dependency
7 target.send(options[:dependent]) if load_target
8 end
9
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
10 def replace(record)
53b76c1 @wangjohn Adding a bang to method name of raise_on_type_mismatch.
wangjohn authored
11 raise_on_type_mismatch!(record) if record
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
12
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
13 update_counters(record)
14 replace_keys(record)
9f5c18c @jonleighton Refactor we_can_set_the_inverse_on_this? to use a less bizarre name a…
jonleighton authored
15 set_inverse_instance(record)
ccea983 @h-lame Providing support for :inverse_of as an option to associations.
h-lame authored
16
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
17 @updated = true if record
18
115eedb @jonleighton Use self.target= rather than @target= as the former automatically set…
jonleighton authored
19 self.target = record
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
20 end
2048556 @miloops Remove whitespace.
miloops authored
21
1e417d5 @kennyj Some refactor for association.
kennyj authored
22 def reset
23 super
24 @updated = false
25 end
26
5213a1f @jamis Fixed saving a record with two unsaved belongs_to associations pointi…
jamis authored
27 def updated?
28 @updated
29 end
2048556 @miloops Remove whitespace.
miloops authored
30
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
31 private
15601c5 @jonleighton Let's be less blasé about method visibility on association proxies
jonleighton authored
32
2175012 @georg Don't find belongs_to target when the foreign_key is NULL. Fixes #2828
georg authored
33 def find_target?
34 !loaded? && foreign_key_present? && klass
35 end
36
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
37 def update_counters(record)
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
38 counter_cache_name = reflection.counter_cache_column
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
39
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
40 if counter_cache_name && owner.persisted? && different_target?(record)
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
41 if record
3c40062 @jonleighton Support for :counter_cache on polymorphic belongs_to
jonleighton authored
42 record.class.increment_counter(counter_cache_name, record.id)
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
43 end
44
4e194ed @jonleighton Rename AssociationProxy#foreign_key_present to foreign_key_present?
jonleighton authored
45 if foreign_key_present?
a5274bb @jonleighton Rename target_klass to klass
jonleighton authored
46 klass.decrement_counter(counter_cache_name, target_id)
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
47 end
48 end
49 end
50
51 # Checks whether record is different to the current target, without loading it
52 def different_target?(record)
3543fde @acapilleri improved different_target conditions
acapilleri authored
53 if record.nil?
54 owner[reflection.foreign_key]
55 else
56 record.id != owner[reflection.foreign_key]
57 end
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
58 end
59
60 def replace_keys(record)
3b87c38 @jonleighton Fix belongs_to polymorphic with custom primary key on target.
jonleighton authored
61 if record
62 owner[reflection.foreign_key] = record[reflection.association_primary_key(record.class)]
63 else
64 owner[reflection.foreign_key] = nil
65 end
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
66 end
67
4e194ed @jonleighton Rename AssociationProxy#foreign_key_present to foreign_key_present?
jonleighton authored
68 def foreign_key_present?
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
69 owner[reflection.foreign_key]
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
70 end
ccea983 @h-lame Providing support for :inverse_of as an option to associations.
h-lame authored
71
72 # NOTE - for now, we're only supporting inverse setting from belongs_to back onto
73 # has_one associations.
9f5c18c @jonleighton Refactor we_can_set_the_inverse_on_this? to use a less bizarre name a…
jonleighton authored
74 def invertible_for?(record)
75 inverse = inverse_reflection_for(record)
76 inverse && inverse.macro == :has_one
ccea983 @h-lame Providing support for :inverse_of as an option to associations.
h-lame authored
77 end
1c11437 @szimek Add primary_key option to belongs_to association
szimek authored
78
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
79 def target_id
32eef69 @jonleighton Delegate Association#options to the reflection, and replace 'reflecti…
jonleighton authored
80 if options[:primary_key]
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
81 owner.send(reflection.name).try(:id)
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
82 else
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
83 owner[reflection.foreign_key]
bea4065 @jonleighton Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociatio…
jonleighton authored
84 end
1c11437 @szimek Add primary_key option to belongs_to association
szimek authored
85 end
a0be389 @jonleighton Allow assignment on has_one :through where the owner is a new record [#…
jonleighton authored
86
87 def stale_state
0f3901e @kennyj @stale_state should be nil when a model isn't saved.
kennyj authored
88 owner[reflection.foreign_key] && owner[reflection.foreign_key].to_s
a0be389 @jonleighton Allow assignment on has_one :through where the owner is a new record [#…
jonleighton authored
89 end
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
90 end
91 end
92 end
Something went wrong with that request. Please try again.