Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 103 lines (86 sloc) 3.077 kb
a9f3c9d @sikachu Using Object#in? and Object#either? in various places
sikachu authored
1
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
2 module ActiveRecord
fde9504 @rizwanreza Adds title to activerecord/lib/active_record/associations/*
rizwanreza authored
3 # = Active Record Belongs To Has One Association
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
4 module Associations
ef79b91 @jonleighton Abstract common code from BelongsToAssociation and HasOneAssociation …
jonleighton authored
5 class HasOneAssociation < SingularAssociation #:nodoc:
ea8181b @jonleighton move dependency logic out of generated methods
jonleighton authored
6
7 def handle_dependency
8 case options[:dependent]
9 when :restrict, :restrict_with_exception
10 raise ActiveRecord::DeleteRestrictionError.new(reflection.name) if load_target
11
12 when :restrict_with_error
13 if load_target
14 record = klass.human_attribute_name(reflection.name).downcase
15 owner.errors.add(:base, :"restrict_dependent_destroy.one", record: record)
16 false
17 end
18
19 else
20 delete
21 end
22 end
23
82b0ce9 @jonleighton Refactor HasOneAssociation#replace
jonleighton authored
24 def replace(record, save = true)
53b76c1 @wangjohn Adding a bang to method name of raise_on_type_mismatch.
wangjohn authored
25 raise_on_type_mismatch!(record) if record
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
26 load_target
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
27
ebd7cc6 @cliochris Fix #8856 Ensure has_one association=(associate) triggers save.
cliochris authored
28 return self.target if !(target || record)
29 if (target != record) || record.changed?
3cb0f3f @bogdan Do not create useless database transaction when building `has_one` as…
bogdan authored
30 transaction_if(save) do
42dd5d9 @kennyj Fix #7191. Remove unnecessary transaction when assigning has_one asso…
kennyj authored
31 remove_target!(options[:dependent]) if target && !target.destroyed?
ec950cf @joliss Fix grammar
joliss authored
32
42dd5d9 @kennyj Fix #7191. Remove unnecessary transaction when assigning has_one asso…
kennyj authored
33 if record
34 set_owner_attributes(record)
35 set_inverse_instance(record)
ec950cf @joliss Fix grammar
joliss authored
36
42dd5d9 @kennyj Fix #7191. Remove unnecessary transaction when assigning has_one asso…
kennyj authored
37 if owner.persisted? && save && !record.save
38 nullify_owner_attributes(record)
39 set_owner_attributes(target) if target
40 raise RecordNotSaved, "Failed to save the new associated #{reflection.name}."
41 end
1d6e218 @jonleighton When assigning a has_one, if anything fails, the assignment should be…
jonleighton authored
42 end
43 end
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
44 end
45
115eedb @jonleighton Use self.target= rather than @target= as the former automatically set…
jonleighton authored
46 self.target = record
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
47 end
afa0c7f @bgreenlee Add support for :primary_key option to has_one as well as has_many so…
bgreenlee authored
48
52f8e4b @jonleighton Use proper objects to do the work to build the associations (adding m…
jonleighton authored
49 def delete(method = options[:dependent])
50 if load_target
51 case method
52 when :delete
53 target.delete
54 when :destroy
55 target.destroy
56 when :nullify
2d9e5a2 @rafaelfranca Deprecate update_column in favor of update_columns.
rafaelfranca authored
57 target.update_columns(reflection.foreign_key => nil)
52f8e4b @jonleighton Use proper objects to do the work to build the associations (adding m…
jonleighton authored
58 end
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
59 end
52f8e4b @jonleighton Use proper objects to do the work to build the associations (adding m…
jonleighton authored
60 end
61
15601c5 @jonleighton Let's be less blasé about method visibility on association proxies
jonleighton authored
62 private
63
552df9b @jonleighton Support for create_association! for has_one associations
jonleighton authored
64 # The reason that the save param for replace is false, if for create (not just build),
ef79b91 @jonleighton Abstract common code from BelongsToAssociation and HasOneAssociation …
jonleighton authored
65 # is because the setting of the foreign keys is actually handled by the scoping when
66 # the record is instantiated, and so they are set straight away and do not need to be
67 # updated within replace.
68 def set_new_record(record)
5b28e52 @jonleighton Don't not remove double negatives
jonleighton authored
69 replace(record, false)
8dd08f9 @jamis dry up some duplicated code
jamis authored
70 end
82b0ce9 @jonleighton Refactor HasOneAssociation#replace
jonleighton authored
71
1bc71ed @jonleighton When assigning a has_one, if the existing record fails to be removed …
jonleighton authored
72 def remove_target!(method)
447b6a4 @fxn removes usage of Object#in? from the code base (the method remains de…
fxn authored
73 case method
74 when :delete
75 target.delete
76 when :destroy
77 target.destroy
78 else
79 nullify_owner_attributes(target)
1bc71ed @jonleighton When assigning a has_one, if the existing record fails to be removed …
jonleighton authored
80
447b6a4 @fxn removes usage of Object#in? from the code base (the method remains de…
fxn authored
81 if target.persisted? && owner.persisted? && !target.save
82 set_owner_attributes(target)
83 raise RecordNotSaved, "Failed to remove the existing associated #{reflection.name}. " +
ec950cf @joliss Fix grammar
joliss authored
84 "The record failed to save after its foreign key was set to nil."
447b6a4 @fxn removes usage of Object#in? from the code base (the method remains de…
fxn authored
85 end
82b0ce9 @jonleighton Refactor HasOneAssociation#replace
jonleighton authored
86 end
87 end
1d6e218 @jonleighton When assigning a has_one, if anything fails, the assignment should be…
jonleighton authored
88
89 def nullify_owner_attributes(record)
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
90 record[reflection.foreign_key] = nil
1d6e218 @jonleighton When assigning a has_one, if anything fails, the assignment should be…
jonleighton authored
91 end
3cb0f3f @bogdan Do not create useless database transaction when building `has_one` as…
bogdan authored
92
93 def transaction_if(value)
94 if value
95 reflection.klass.transaction { yield }
96 else
97 yield
98 end
99 end
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
100 end
101 end
102 end
Something went wrong with that request. Please try again.