Skip to content

Commit

Permalink
Not need to pass join attributes to association build
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Mar 9, 2012
1 parent 7cd4063 commit f18c054
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
Expand Up @@ -73,7 +73,9 @@ def through_association
# association # association
def build_through_record(record) def build_through_record(record)
@through_records[record.object_id] ||= begin @through_records[record.object_id] ||= begin
through_record = through_association.build(construct_join_attributes(record)) ensure_mutable

through_record = through_association.build
through_record.send("#{source_reflection.name}=", record) through_record.send("#{source_reflection.name}=", record)
through_record through_record
end end
Expand Down
Expand Up @@ -37,9 +37,7 @@ def target_scope
# situation it is more natural for the user to just create or modify their join records # situation it is more natural for the user to just create or modify their join records
# directly as required. # directly as required.
def construct_join_attributes(*records) def construct_join_attributes(*records)
if source_reflection.macro != :belongs_to ensure_mutable
raise HasManyThroughCantAssociateThroughHasOneOrManyReflection.new(owner, reflection)
end


join_attributes = { join_attributes = {
source_reflection.foreign_key => source_reflection.foreign_key =>
Expand Down Expand Up @@ -73,6 +71,12 @@ def foreign_key_present?
!owner[through_reflection.foreign_key].nil? !owner[through_reflection.foreign_key].nil?
end end


def ensure_mutable
if source_reflection.macro != :belongs_to
raise HasManyThroughCantAssociateThroughHasOneOrManyReflection.new(owner, reflection)
end
end

def ensure_not_nested def ensure_not_nested
if reflection.nested? if reflection.nested?
raise HasManyThroughNestedAssociationsAreReadonly.new(owner, reflection) raise HasManyThroughNestedAssociationsAreReadonly.new(owner, reflection)
Expand Down
Expand Up @@ -4,7 +4,6 @@
require 'models/reference' require 'models/reference'
require 'models/job' require 'models/job'
require 'models/reader' require 'models/reader'
require 'models/secure_reader'
require 'models/comment' require 'models/comment'
require 'models/tag' require 'models/tag'
require 'models/tagging' require 'models/tagging'
Expand Down Expand Up @@ -60,7 +59,7 @@ def test_associate_existing
end end


def test_associate_existing_with_strict_mass_assignment_sanitizer def test_associate_existing_with_strict_mass_assignment_sanitizer
ActiveRecord::Base.mass_assignment_sanitizer = :strict SecureReader.mass_assignment_sanitizer = :strict


SecureReader.new SecureReader.new


Expand All @@ -71,7 +70,7 @@ def test_associate_existing_with_strict_mass_assignment_sanitizer
post.secure_people << person post.secure_people << person
end end
ensure ensure
ActiveRecord::Base.mass_assignment_sanitizer = :logger SecureReader.mass_assignment_sanitizer = :logger
end end


def test_associate_existing_record_twice_should_add_to_target_twice def test_associate_existing_record_twice_should_add_to_target_twice
Expand Down
9 changes: 9 additions & 0 deletions activerecord/test/models/reader.rb
Expand Up @@ -3,3 +3,12 @@ class Reader < ActiveRecord::Base
belongs_to :person, :inverse_of => :readers belongs_to :person, :inverse_of => :readers
belongs_to :single_person, :class_name => 'Person', :foreign_key => :person_id, :inverse_of => :reader belongs_to :single_person, :class_name => 'Person', :foreign_key => :person_id, :inverse_of => :reader
end end

class SecureReader < ActiveRecord::Base
self.table_name = "readers"

belongs_to :secure_post, :class_name => "Post", :foreign_key => "post_id"
belongs_to :secure_person, :inverse_of => :secure_readers, :class_name => "Person", :foreign_key => "person_id"

attr_accessible nil
end
9 changes: 0 additions & 9 deletions activerecord/test/models/secure_reader.rb

This file was deleted.

0 comments on commit f18c054

Please sign in to comment.