From ef79b917848f07b61e3243027f5e2ce4bc006d78 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 16 Jan 2011 19:34:08 +0000 Subject: Abstract common code from BelongsToAssociation and HasOneAssociation into SingularAssociation --- .../associations/singular_association.rb | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 activerecord/lib/active_record/associations/singular_association.rb (limited to 'activerecord/lib/active_record/associations/singular_association.rb') diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb new file mode 100644 index 0000000000..023f7caf68 --- /dev/null +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -0,0 +1,31 @@ +module ActiveRecord + module Associations + class SingularAssociation < AssociationProxy #:nodoc: + def create(attributes = {}) + record = scoped.scoping { @reflection.create_association(attributes) } + set_new_record(record) + record + end + + def create!(attributes = {}) + build(attributes).tap { |record| record.save! } + end + + def build(attributes = {}) + record = scoped.scoping { @reflection.build_association(attributes) } + set_new_record(record) + record + end + + private + # Implemented by subclasses + def replace(record) + raise NotImplementedError + end + + def set_new_record(record) + replace(record) + end + end + end +end -- cgit v1.2.3 From f1a15c2197d5da8e0c38bd59aa19973c9cfc0a01 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 16 Jan 2011 19:47:58 +0000 Subject: Abstract a bit more into SingularAssociation --- activerecord/lib/active_record/associations/singular_association.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activerecord/lib/active_record/associations/singular_association.rb') diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index 023f7caf68..4b457bd881 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -26,6 +26,12 @@ module ActiveRecord def set_new_record(record) replace(record) end + + def check_record(record) + record = record.target if AssociationProxy === record + raise_on_type_mismatch(record) if record + record + end end end end -- cgit v1.2.3 From b7594a075637f2d3039b066c282acfcb32126cdf Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 16 Jan 2011 19:49:32 +0000 Subject: find_target can also go into SingularAssociation --- activerecord/lib/active_record/associations/singular_association.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord/lib/active_record/associations/singular_association.rb') diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index 4b457bd881..b6f49c6f36 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -18,6 +18,10 @@ module ActiveRecord end private + def find_target + scoped.first.tap { |record| set_inverse_instance(record) } + end + # Implemented by subclasses def replace(record) raise NotImplementedError -- cgit v1.2.3 From 63c73dd0214188dc91442db538e141e30ec3b1b9 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 23 Jan 2011 21:29:36 +0000 Subject: We shouldn't be using scoped.scoping { ... } to build associated records, as this can affect validations/callbacks/etc inside the record itself [#6252 state:resolved] --- .../active_record/associations/singular_association.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record/associations/singular_association.rb') diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index b6f49c6f36..b43b52fc52 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -2,9 +2,7 @@ module ActiveRecord module Associations class SingularAssociation < AssociationProxy #:nodoc: def create(attributes = {}) - record = scoped.scoping { @reflection.create_association(attributes) } - set_new_record(record) - record + new_record(:create, attributes) end def create!(attributes = {}) @@ -12,9 +10,7 @@ module ActiveRecord end def build(attributes = {}) - record = scoped.scoping { @reflection.build_association(attributes) } - set_new_record(record) - record + new_record(:build, attributes) end private @@ -36,6 +32,13 @@ module ActiveRecord raise_on_type_mismatch(record) if record record end + + def new_record(method, attributes) + attributes = scoped.scope_for_create.merge(attributes || {}) + record = @reflection.send("#{method}_association", attributes) + set_new_record(record) + record + end end end end -- cgit v1.2.3 From 15601c52e7c7094a6b7b54ef8acfc8299a4d6724 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Mon, 24 Jan 2011 19:28:53 +0000 Subject: =?UTF-8?q?Let's=20be=20less=20blas=C3=A9=20about=20method=20visib?= =?UTF-8?q?ility=20on=20association=20proxies?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- activerecord/lib/active_record/associations/singular_association.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/lib/active_record/associations/singular_association.rb') diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index b43b52fc52..7f92d9712a 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -14,6 +14,7 @@ module ActiveRecord end private + def find_target scoped.first.tap { |record| set_inverse_instance(record) } end -- cgit v1.2.3