diff options
author | eileencodes <eileencodes@gmail.com> | 2015-09-26 16:07:24 -0400 |
---|---|---|
committer | eileencodes <eileencodes@gmail.com> | 2015-09-26 16:26:56 -0400 |
commit | ee824c8858f2a14aa3014877310971795348e460 (patch) | |
tree | a3baf298942d698cc87b149112f7c4dedbc5b30a /activerecord/test | |
parent | 7f18ea14c893cb5c9f04d4fda9661126758332b5 (diff) | |
download | rails-ee824c8858f2a14aa3014877310971795348e460.tar.gz rails-ee824c8858f2a14aa3014877310971795348e460.tar.bz2 rails-ee824c8858f2a14aa3014877310971795348e460.zip |
Fix regression in inverse_of on through associations
`inverse_of` on through associations was accidently removed/caused to
stop working in commit f8d2899 which was part of a refactoring on
`ThroughReflection`.
To fix we moved `inverse_of` and `check_validity_of_inverse!` to the
`AbstractReflection` so it's available to the `ThroughReflection`
without having to dup any methods. We then need to delegate `inverse_name`
method in `ThroughReflection`. `inverse_name` can't be moved to
`AbstractReflection` without moving methods that set the instance
variable `@automatic_inverse_of`.
This adds a test that ensures that `inverse_of` on a `ThroughReflection`
returns the correct class name, and the correct record for the inverse
relationship.
Fixes #21692
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/associations/inverse_associations_test.rb | 13 | ||||
-rw-r--r-- | activerecord/test/models/company.rb | 3 | ||||
-rw-r--r-- | activerecord/test/models/developer.rb | 3 | ||||
-rw-r--r-- | activerecord/test/models/project.rb | 2 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 2 |
5 files changed, 23 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb index 423b8238b1..ece4dab539 100644 --- a/activerecord/test/cases/associations/inverse_associations_test.rb +++ b/activerecord/test/cases/associations/inverse_associations_test.rb @@ -13,6 +13,9 @@ require 'models/mixed_case_monkey' require 'models/admin' require 'models/admin/account' require 'models/admin/user' +require 'models/developer' +require 'models/company' +require 'models/project' class AutomaticInverseFindingTests < ActiveRecord::TestCase fixtures :ratings, :comments, :cars @@ -198,6 +201,16 @@ class InverseAssociationTests < ActiveRecord::TestCase belongs_to_ref = Sponsor.reflect_on_association(:sponsor_club) assert_nil belongs_to_ref.inverse_of end + + def test_this_inverse_stuff + firm = Firm.create!(name: 'Adequate Holdings') + Project.create!(name: 'Project 1', firm: firm) + Developer.create!(name: 'Gorbypuff', firm: firm) + + new_project = Project.last + assert Project.reflect_on_association(:lead_developer).inverse_of.present?, "Expected inverse of to be present" + assert new_project.lead_developer.present?, "Expected lead developer to be present on the project" + end end class InverseHasOneTests < ActiveRecord::TestCase diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index 67936e8e5d..a96b8ef0f2 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -86,6 +86,9 @@ class Firm < Company has_many :association_with_references, -> { references(:foo) }, :class_name => 'Client' + has_one :lead_developer, class_name: "Developer" + has_many :projects + def log @log ||= [] end diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 7565e8f967..7c5941b1af 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -54,6 +54,9 @@ class Developer < ActiveRecord::Base has_many :ratings, through: :comments has_one :ship, dependent: :nullify + belongs_to :firm + has_many :contracted_projects, class_name: "Project" + scope :jamises, -> { where(:name => 'Jamis') } validates_inclusion_of :salary, :in => 50000..200000 diff --git a/activerecord/test/models/project.rb b/activerecord/test/models/project.rb index 7f42a4b1f8..5328330653 100644 --- a/activerecord/test/models/project.rb +++ b/activerecord/test/models/project.rb @@ -11,6 +11,8 @@ class Project < ActiveRecord::Base :before_remove => Proc.new {|o, r| o.developers_log << "before_removing#{r.id}"}, :after_remove => Proc.new {|o, r| o.developers_log << "after_removing#{r.id}"} has_and_belongs_to_many :well_payed_salary_groups, -> { group("developers.salary").having("SUM(salary) > 10000").select("SUM(salary) as salary") }, :class_name => "Developer" + belongs_to :firm + has_one :lead_developer, through: :firm, inverse_of: :contracted_projects attr_accessor :developers_log after_initialize :set_developers_log diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 4b7272f10a..d334a2740e 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -252,6 +252,7 @@ ActiveRecord::Schema.define do t.string :name t.string :first_name t.integer :salary, default: 70000 + t.integer :firm_id if subsecond_precision_supported? t.datetime :created_at, precision: 6 t.datetime :updated_at, precision: 6 @@ -658,6 +659,7 @@ ActiveRecord::Schema.define do create_table :projects, force: true do |t| t.string :name t.string :type + t.integer :firm_id end create_table :randomly_named_table1, force: true do |t| |