@@ -53,6 +53,42 @@ class Organization < ActiveRecord::Base
5353 assert_equal ( 2 , response [ 0 ] . range . end . line )
5454 end
5555
56+ test "recognizes main association on has_many :through association" do
57+ response = generate_definitions_for_source ( <<~RUBY , { line : 2 , character : 13 } )
58+ class Organization < ActiveRecord::Base
59+ has_many :memberships
60+ has_many :users, through: :memberships
61+ end
62+ RUBY
63+
64+ assert_equal ( 1 , response . size )
65+
66+ assert_equal (
67+ URI ::Generic . from_path ( path : File . join ( dummy_root , "app" , "models" , "user.rb" ) ) . to_s ,
68+ response [ 0 ] . uri ,
69+ )
70+ assert_equal ( 2 , response [ 0 ] . range . start . line )
71+ assert_equal ( 2 , response [ 0 ] . range . end . line )
72+ end
73+
74+ test "recognizes through association on has_many :through association" do
75+ response = generate_definitions_for_source ( <<~RUBY , { line : 2 , character : 30 } )
76+ class Organization < ActiveRecord::Base
77+ has_many :memberships
78+ has_many :users, through: :memberships
79+ end
80+ RUBY
81+
82+ assert_equal ( 1 , response . size )
83+
84+ assert_equal (
85+ URI ::Generic . from_path ( path : File . join ( dummy_root , "app" , "models" , "membership.rb" ) ) . to_s ,
86+ response [ 0 ] . uri ,
87+ )
88+ assert_equal ( 2 , response [ 0 ] . range . start . line )
89+ assert_equal ( 2 , response [ 0 ] . range . end . line )
90+ end
91+
5692 test "recognizes belongs_to model associations" do
5793 response = generate_definitions_for_source ( <<~RUBY , { line : 3 , character : 14 } )
5894 # typed: false
@@ -91,6 +127,42 @@ class User < ActiveRecord::Base
91127 assert_equal ( 2 , response [ 0 ] . range . end . line )
92128 end
93129
130+ test "recognizes main association on has_one :through association" do
131+ response = generate_definitions_for_source ( <<~RUBY , { line : 2 , character : 12 } )
132+ class User < ActiveRecord::Base
133+ belongs_to :location, class_name: "Country"
134+ has_one :country_flag, through: :location, source: :flag
135+ end
136+ RUBY
137+
138+ assert_equal ( 1 , response . size )
139+
140+ assert_equal (
141+ URI ::Generic . from_path ( path : File . join ( dummy_root , "app" , "models" , "flag.rb" ) ) . to_s ,
142+ response [ 0 ] . uri ,
143+ )
144+ assert_equal ( 2 , response [ 0 ] . range . start . line )
145+ assert_equal ( 2 , response [ 0 ] . range . end . line )
146+ end
147+
148+ test "recognizes through association on has_one :through association" do
149+ response = generate_definitions_for_source ( <<~RUBY , { line : 2 , character : 36 } )
150+ class User < ActiveRecord::Base
151+ belongs_to :location, class_name: "Country"
152+ has_one :country_flag, through: :location, source: :flag
153+ end
154+ RUBY
155+
156+ assert_equal ( 1 , response . size )
157+
158+ assert_equal (
159+ URI ::Generic . from_path ( path : File . join ( dummy_root , "app" , "models" , "country.rb" ) ) . to_s ,
160+ response [ 0 ] . uri ,
161+ )
162+ assert_equal ( 2 , response [ 0 ] . range . start . line )
163+ assert_equal ( 2 , response [ 0 ] . range . end . line )
164+ end
165+
94166 test "recognizes has_and_belongs_to_many model associations" do
95167 response = generate_definitions_for_source ( <<~RUBY , { line : 3 , character : 27 } )
96168 # typed: false
@@ -111,11 +183,11 @@ class Profile < ActiveRecord::Base
111183 end
112184
113185 test "handles class_name argument for associations" do
114- response = generate_definitions_for_source ( <<~RUBY , { line : 3 , character : 11 } )
186+ response = generate_definitions_for_source ( <<~RUBY , { line : 3 , character : 14 } )
115187 # typed: false
116188
117189 class User < ActiveRecord::Base
118- has_one :location, class_name: "Country"
190+ belongs_to :location, class_name: "Country"
119191 end
120192 RUBY
121193
@@ -467,6 +539,48 @@ def name; end
467539 assert_equal ( 15 , response . range . end . character )
468540 end
469541
542+ test "recognizes string main association on has_many :through association" do
543+ response = generate_definitions_for_source ( <<~RUBY , { line : 2 , character : 14 } )
544+ class Organization < ApplicationRecord
545+ has_many :memberships
546+ has_many "users", through: :memberships
547+ end
548+
549+ class User < ApplicationRecord
550+ end
551+ RUBY
552+
553+ assert_equal ( 1 , response . size )
554+
555+ assert_equal (
556+ URI ::Generic . from_path ( path : File . join ( dummy_root , "app" , "models" , "user.rb" ) ) . to_s ,
557+ response [ 0 ] . uri ,
558+ )
559+ assert_equal ( 2 , response [ 0 ] . range . start . line )
560+ assert_equal ( 2 , response [ 0 ] . range . end . line )
561+ end
562+
563+ test "recognizes string through association on has_many :through association" do
564+ response = generate_definitions_for_source ( <<~RUBY , { line : 2 , character : 32 } )
565+ class Organization < ApplicationRecord
566+ has_many "memberships"
567+ has_many :users, through: "memberships"
568+ end
569+
570+ class Membership < ApplicationRecord
571+ end
572+ RUBY
573+
574+ assert_equal ( 1 , response . size )
575+
576+ assert_equal (
577+ URI ::Generic . from_path ( path : File . join ( dummy_root , "app" , "models" , "membership.rb" ) ) . to_s ,
578+ response [ 0 ] . uri ,
579+ )
580+ assert_equal ( 2 , response [ 0 ] . range . start . line )
581+ assert_equal ( 2 , response [ 0 ] . range . end . line )
582+ end
583+
470584 private
471585
472586 def generate_definitions_for_source ( source , position )
0 commit comments