Skip to content

Commit

Permalink
Scopes must include class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rzane committed Dec 7, 2024
1 parent cc54939 commit 7258764
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/tapioca/dsl/compilers/active_record_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,18 @@ class ActiveRecordScope < Compiler

sig { override.void }
def decorate
method_names = scope_method_names
scope_method_names = gather_scope_method_names
class_method_names = constant.public_methods(false) - scope_method_names

return if method_names.empty?
return if scope_method_names.empty? && class_method_names.empty?

root.create_path(constant) do |model|
relations_enabled = compiler_enabled?("ActiveRecordRelations")

relation_methods_module = model.create_module(RelationMethodsModuleName)
assoc_relation_methods_mod = model.create_module(AssociationRelationMethodsModuleName) if relations_enabled

method_names.each do |scope_method|
scope_method_names.each do |scope_method|
generate_scope_method(
relation_methods_module,
scope_method.to_s,
Expand All @@ -72,6 +73,12 @@ def decorate
)
end

class_method_names.each do |class_method|
method = constant.method(class_method)
create_method_from_def(relation_methods_module, method)
create_method_from_def(assoc_relation_methods_mod, method) if relations_enabled
end

model.create_extend(RelationMethodsModuleName)
end
end
Expand All @@ -86,7 +93,7 @@ def gather_constants
private

sig { returns(T::Array[Symbol]) }
def scope_method_names
def gather_scope_method_names
scope_methods = T.let([], T::Array[Symbol])
constant = self.constant

Expand Down

0 comments on commit 7258764

Please sign in to comment.