@@ -53,6 +53,46 @@ 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 : 4 , character : 12 } )
58+ # typed: false
59+
60+ class Organization < ActiveRecord::Base
61+ has_many :memberships
62+ has_many :users, through: :memberships
63+ end
64+ RUBY
65+
66+ assert_equal ( 1 , response . size )
67+
68+ assert_equal (
69+ URI ::Generic . from_path ( path : File . join ( dummy_root , "app" , "models" , "user.rb" ) ) . to_s ,
70+ response [ 0 ] . uri ,
71+ )
72+ assert_equal ( 2 , response [ 0 ] . range . start . line )
73+ assert_equal ( 2 , response [ 0 ] . range . end . line )
74+ end
75+
76+ test "recognizes through association on has_many :through association" do
77+ response = generate_definitions_for_source ( <<~RUBY , { line : 4 , character : 29 } )
78+ # typed: false
79+
80+ class Organization < ActiveRecord::Base
81+ has_many :memberships
82+ has_many :users, through: :memberships
83+ end
84+ RUBY
85+
86+ assert_equal ( 1 , response . size )
87+
88+ assert_equal (
89+ URI ::Generic . from_path ( path : File . join ( dummy_root , "app" , "models" , "membership.rb" ) ) . to_s ,
90+ response [ 0 ] . uri ,
91+ )
92+ assert_equal ( 2 , response [ 0 ] . range . start . line )
93+ assert_equal ( 2 , response [ 0 ] . range . end . line )
94+ end
95+
5696 test "recognizes belongs_to model associations" do
5797 response = generate_definitions_for_source ( <<~RUBY , { line : 3 , character : 14 } )
5898 # typed: false
@@ -91,6 +131,46 @@ class User < ActiveRecord::Base
91131 assert_equal ( 2 , response [ 0 ] . range . end . line )
92132 end
93133
134+ test "recognizes main association on has_one :through association" do
135+ response = generate_definitions_for_source ( <<~RUBY , { line : 4 , character : 19 } )
136+ # typed: false
137+
138+ class User < ActiveRecord::Base
139+ belongs_to :location, class_name: "Country"
140+ has_one :country_flag, through: :location, source: :flag
141+ end
142+ RUBY
143+
144+ assert_equal ( 1 , response . size )
145+
146+ assert_equal (
147+ URI ::Generic . from_path ( path : File . join ( dummy_root , "app" , "models" , "flag.rb" ) ) . to_s ,
148+ response [ 0 ] . uri ,
149+ )
150+ assert_equal ( 2 , response [ 0 ] . range . start . line )
151+ assert_equal ( 2 , response [ 0 ] . range . end . line )
152+ end
153+
154+ test "recognizes through association on has_one :through association" do
155+ response = generate_definitions_for_source ( <<~RUBY , { line : 4 , character : 35 } )
156+ # typed: false
157+
158+ class User < ActiveRecord::Base
159+ belongs_to :location, class_name: "Country"
160+ has_one :country_flag, through: :location, source: :flag
161+ end
162+ RUBY
163+
164+ assert_equal ( 1 , response . size )
165+
166+ assert_equal (
167+ URI ::Generic . from_path ( path : File . join ( dummy_root , "app" , "models" , "country.rb" ) ) . to_s ,
168+ response [ 0 ] . uri ,
169+ )
170+ assert_equal ( 2 , response [ 0 ] . range . start . line )
171+ assert_equal ( 2 , response [ 0 ] . range . end . line )
172+ end
173+
94174 test "recognizes has_and_belongs_to_many model associations" do
95175 response = generate_definitions_for_source ( <<~RUBY , { line : 3 , character : 27 } )
96176 # typed: false
@@ -111,11 +191,11 @@ class Profile < ActiveRecord::Base
111191 end
112192
113193 test "handles class_name argument for associations" do
114- response = generate_definitions_for_source ( <<~RUBY , { line : 3 , character : 11 } )
194+ response = generate_definitions_for_source ( <<~RUBY , { line : 3 , character : 14 } )
115195 # typed: false
116196
117197 class User < ActiveRecord::Base
118- has_one :location, class_name: "Country"
198+ belongs_to :location, class_name: "Country"
119199 end
120200 RUBY
121201
0 commit comments