From 9574d0e820a377390b151e29f8407e6406bf9b6f Mon Sep 17 00:00:00 2001 From: Stephen Sugden Date: Sun, 14 May 2023 18:13:59 +0200 Subject: [PATCH] Update model spec to expect private relation types --- spec/solargraph-rails/model_spec.rb | 45 +++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/spec/solargraph-rails/model_spec.rb b/spec/solargraph-rails/model_spec.rb index d9bea48..9bb6c77 100644 --- a/spec/solargraph-rails/model_spec.rb +++ b/spec/solargraph-rails/model_spec.rb @@ -55,16 +55,16 @@ class Account < ActiveRecord::Base assert_public_instance_method( api_map, 'Account#transactions', - ['ActiveRecord::Associations::CollectionProxy'] + ['Transaction::ActiveRecord_Relation'] ) assert_public_instance_method( api_map, 'Account#things', - ['ActiveRecord::Associations::CollectionProxy'] + ['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 @@ -72,7 +72,23 @@ class Transaction < ActiveRecord::Base end RUBY - assert_class_method(api_map, 'Transaction.positive', ['Class']) + 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 @@ -87,7 +103,7 @@ class Person < ActiveRecord::Base assert_class_method( api_map, 'Person.taller_than', - ['Class'] + ['Person::ActiveRecord_Relation'] ) do |pin| expect(pin.parameters).not_to be_empty expect(pin.parameters.first.name).to eq('min_height') @@ -111,10 +127,27 @@ def some_method assert_class_method( api_map, 'Person.taller_than', - ['Class'] + ['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