Skip to content

Commit

Permalink
Update model spec to expect private relation types
Browse files Browse the repository at this point in the history
  • Loading branch information
grncdr committed May 20, 2023
1 parent 9f1d4e7 commit 9574d0e
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions spec/solargraph-rails/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,40 @@ class Account < ActiveRecord::Base
assert_public_instance_method(
api_map,
'Account#transactions',
['ActiveRecord::Associations::CollectionProxy<Transaction>']
['Transaction::ActiveRecord_Relation']
)
assert_public_instance_method(
api_map,
'Account#things',
['ActiveRecord::Associations::CollectionProxy<Thing>']
['Thing::ActiveRecord_Relation']
)
end

it 'generates methods for scope' do
it 'exposes scopes as class methods' do
load_string 'app/models/transaction.rb',
<<-RUBY
class Transaction < ActiveRecord::Base
scope :positive, ->(arg) { where(foo: 'bar') }
end
RUBY

assert_class_method(api_map, 'Transaction.positive', ['Class<Transaction>'])
assert_class_method(api_map, 'Transaction.positive', ['Transaction::ActiveRecord_Relation'])
assert_public_instance_method(api_map, 'Transaction::ActiveRecord_Relation#positive', ['Transaction::ActiveRecord_Relation'])
end

it 'exposes scopes as relation instance methods' do
load_string 'app/models/person.rb',
<<~RUBY
class Person < ActiveRecord::Base
scope :taller_than, ->(h) { where(height: h..) }
end
RUBY

assert_public_instance_method(
api_map,
'Person::ActiveRecord_Relation#taller_than',
['Person::ActiveRecord_Relation']
)
end

it 'generates scope methods with parameters' do
Expand All @@ -87,7 +103,7 @@ class Person < ActiveRecord::Base
assert_class_method(
api_map,
'Person.taller_than',
['Class<Person>']
['Person::ActiveRecord_Relation']
) do |pin|
expect(pin.parameters).not_to be_empty
expect(pin.parameters.first.name).to eq('min_height')
Expand All @@ -111,10 +127,27 @@ def some_method
assert_class_method(
api_map,
'Person.taller_than',
['Class<Person>']
['Person::ActiveRecord_Relation']
) do |pin|
expect(pin.parameters).not_to be_empty
expect(pin.parameters.first.name).to eq('min_height')
end
end

it 'exposes class methods as instance methods on relations', if: Solargraph::Rails::Delegate.supported? do
load_string 'app/models/person.rb',
<<~RUBY
class Person < ActiveRecord::Base
def self.taller_than(h)
where(height: h..)
end
end
RUBY

assert_public_instance_method(
api_map,
'Person::ActiveRecord_Relation#taller_than',
['Person::ActiveRecord_Relation']
)
end
end

0 comments on commit 9574d0e

Please sign in to comment.