Merge pull request #23221 from vipulnsward/23209-fix-missin_source_type

Add missing source_type if provided on hmt which belongs to an sti re…
This commit is contained in:
Rafael França 2016-01-24 13:08:32 -05:00
commit 5119171d39
6 changed files with 26 additions and 0 deletions

@ -66,6 +66,11 @@ def build_through_record(record)
through_record = through_association.build(*options_for_through_record)
through_record.send("#{source_reflection.name}=", record)
if options[:source_type]
through_record.send("#{source_reflection.foreign_type}=", options[:source_type])
end
through_record
end
end

@ -23,6 +23,7 @@
require 'models/department'
require 'models/cake_designer'
require 'models/drink_designer'
require 'models/mocktail_designer'
require 'models/recipe'
class ReflectionTest < ActiveRecord::TestCase
@ -278,6 +279,14 @@ def test_scope_chain_does_not_interfere_with_hmt_with_polymorphic_case
assert_equal 2, @hotel.chefs.size
end
def test_scope_chain_does_not_interfere_with_hmt_with_polymorphic_case_and_sti
@hotel = Hotel.create!
@hotel.mocktail_designers << MocktailDesigner.create!
assert_equal 1, @hotel.mocktail_designers.size
assert_equal 1, @hotel.chef_lists.size
end
def test_scope_chain_of_polymorphic_association_does_not_leak_into_other_hmt_associations
hotel = Hotel.create!
department = hotel.departments.create!

@ -2,3 +2,7 @@ class Chef < ActiveRecord::Base
belongs_to :employable, polymorphic: true
has_many :recipes
end
class ChefList < Chef
belongs_to :employable_list, polymorphic: true
end

@ -3,5 +3,9 @@ class Hotel < ActiveRecord::Base
has_many :chefs, through: :departments
has_many :cake_designers, source_type: 'CakeDesigner', source: :employable, through: :chefs
has_many :drink_designers, source_type: 'DrinkDesigner', source: :employable, through: :chefs
has_many :chef_lists, as: :employable_list
has_many :mocktail_designers, through: :chef_lists, source: :employable, :source_type => "MocktailDesigner"
has_many :recipes, through: :chefs
end

@ -0,0 +1,2 @@
class MocktailDesigner < DrinkDesigner
end

@ -976,6 +976,8 @@ def except(adapter_names_to_exclude)
t.integer :employable_id
t.string :employable_type
t.integer :department_id
t.string :employable_list_type
t.integer :employable_list_id
end
create_table :recipes, force: true do |t|
t.integer :chef_id