aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2007-01-11 18:19:23 +0000
committerJamis Buck <jamis@37signals.com>2007-01-11 18:19:23 +0000
commit8dd08f98c7f8d78c4b75e4d4c231d7341915f797 (patch)
tree0968e679b05553576028e3edb4ed1fc06536fc76 /activerecord
parent0d34c28199839882076e95a20bcae0be44345a1c (diff)
downloadrails-8dd08f98c7f8d78c4b75e4d4c231d7341915f797.tar.gz
rails-8dd08f98c7f8d78c4b75e4d4c231d7341915f797.tar.bz2
rails-8dd08f98c7f8d78c4b75e4d4c231d7341915f797.zip
dry up some duplicated code
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5883 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb58
1 files changed, 21 insertions, 37 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index 757895b6b4..b18169ab67 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -7,47 +7,15 @@ module ActiveRecord
end
def create(attrs = {}, replace_existing = true)
- # make sure we load the target first, if we plan on replacing the existing
- # instance. Otherwise, if the target has not previously been loaded
- # elsewhere, the instance we create will get orphaned.
- load_target if replace_existing
- record = @reflection.klass.with_scope(:create => construct_scope[:create]) { @reflection.klass.create(attrs) }
-
- if replace_existing
- replace(record, true)
- else
- record[@reflection.primary_key_name] = @owner.id unless @owner.new_record?
- self.target = record
- end
-
- record
+ new_record(replace_existing) { |klass| klass.create(attrs) }
end
def create!(attrs = {}, replace_existing = true)
- load_target if replace_existing
- record = @reflection.klass.with_scope(:create => construct_scope[:create]) { @reflection.klass.create!(attrs) }
-
- if replace_existing
- replace(record, true)
- else
- record[@reflection.primary_key_name] = @owner.id unless @owner.new_record?
- self.target = record
- end
-
- record
+ new_record(replace_existing) { |klass| klass.create!(attrs) }
end
- def build(attributes = {}, replace_existing = true)
- record = @reflection.klass.new(attributes)
-
- if replace_existing
- replace(record, true)
- else
- record[@reflection.primary_key_name] = @owner.id unless @owner.new_record?
- self.target = record
- end
-
- record
+ def build(attrs = {}, replace_existing = true)
+ new_record(replace_existing) { |klass| klass.new(attrs) }
end
def replace(obj, dont_save = false)
@@ -106,7 +74,23 @@ module ActiveRecord
set_belongs_to_association_for(create_scoping)
{ :create => create_scoping }
end
-
+
+ def new_record(replace_existing)
+ # make sure we load the target first, if we plan on replacing the existing
+ # instance. Otherwise, if the target has not previously been loaded
+ # elsewhere, the instance we create will get orphaned.
+ load_target if replace_existing
+ record = @reflection.klass.with_scope(:create => construct_scope[:create]) { yield @reflection.klass }
+
+ if replace_existing
+ replace(record, true)
+ else
+ record[@reflection.primary_key_name] = @owner.id unless @owner.new_record?
+ self.target = record
+ end
+
+ record
+ end
end
end
end