Class: Tapioca::Dsl::Compilers::Kaminari

Inherits:
Tapioca::Dsl::Compiler
  • Object
show all
Extended by:
T::Sig
Includes:
Helpers::ActiveRecordConstantsHelper
Defined in:
lib/tapioca/dsl/compilers/kaminari.rb

Overview

‘Tapioca::Dsl::Compilers::Kaminari` decorates RBI files for models using Kaminari.

For example, with Kaminari installed and the following ‘ActiveRecord::Base` subclass:

~~~rb class Post < ApplicationRecord end ~~~

This compiler will produce the RBI file ‘post.rbi` with the following content:

~~~rbi # post.rbi # typed: true class Post

extend GeneratedRelationMethods

module GeneratedRelationMethods
  sig do
    params(
      num: T.any(Integer, String)
    ).returns(T.all(PrivateRelation, Kaminari::PageScopeMethods, Kaminari::ActiveRecordRelationMethods))
  end
  def page(num = nil); end
end

end ~~~

Constant Summary collapse

ConstantType =
type_member { { fixed: T.class_of(::ActiveRecord::Base) } }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



64
65
66
# File 'lib/tapioca/dsl/compilers/kaminari.rb', line 64

def gather_constants
  descendants_of(::ActiveRecord::Base).reject(&:abstract_class?)
end

Instance Method Details

#decorateObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tapioca/dsl/compilers/kaminari.rb', line 46

def decorate
  root.create_path(constant) do |model|
    target_modules.each do |module_name, return_type|
      model.create_module(module_name).create_method(
        ::Kaminari.config.page_method_name.to_s,
        parameters: [create_opt_param("num", type: "T.any(Integer, String)", default: "nil")],
        return_type: "T.all(#{return_type}, Kaminari::PageScopeMethods, Kaminari::ActiveRecordRelationMethods)",
      )
    end

    model.create_extend(RelationMethodsModuleName)
  end
end