Skip to content

Commit

Permalink
Merge pull request #1453 from Shopify/fix-active-record-scope-compile…
Browse files Browse the repository at this point in the history
…r-duplicate-methods

Fix ActiveRecordScope Compiler to not duplicate relation methods of superclass
  • Loading branch information
Shinomix authored Apr 12, 2023
2 parents d2fb474 + 43476f8 commit eb4ab4d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/tapioca/dsl/compilers/active_record_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def scope_method_names
constant = T.cast(superclass, T.class_of(ActiveRecord::Base))
end

scope_methods
scope_methods.uniq
end

sig do
Expand Down
42 changes: 42 additions & 0 deletions spec/tapioca/dsl/compilers/active_record_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,48 @@ def post_scope(*args, &blk); end

assert_equal(expected, rbi_for(:Post))
end

it "does not duplicate relation includes from abstract parent models" do
add_ruby_file("post.rb", <<~RUBY)
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
default_scope { max_execution_time(100) }
end
class ConfigurationRecord < ApplicationRecord
self.abstract_class = true
scope :post_scope, -> { where.not(kind: 'private') }
default_scope { max_execution_time(200) }
end
class Post < ConfigurationRecord
scope :post_scope, -> { where.not(kind: 'private') }
end
RUBY

expected = <<~RBI
# typed: strong
class Post
extend GeneratedRelationMethods
module GeneratedAssociationRelationMethods
sig { params(args: T.untyped, blk: T.untyped).returns(PrivateAssociationRelation) }
def post_scope(*args, &blk); end
end
module GeneratedRelationMethods
sig { params(args: T.untyped, blk: T.untyped).returns(PrivateRelation) }
def post_scope(*args, &blk); end
end
end
RBI

assert_equal(expected, rbi_for(:Post))
end
end

describe "without relations enabled" do
Expand Down

0 comments on commit eb4ab4d

Please sign in to comment.