Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Kaminari #2

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ group :development do
gem "minitest-reporters"

gem "attr_json"
gem "kaminari-activerecord"
gem "money-rails"
gem "paperclip"
gem "rails", "~> 7.2"
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ GEM
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.7.2)
kaminari-activerecord (1.2.2)
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
language_server-protocol (3.17.0.3)
logger (1.6.1)
loofah (2.22.0)
Expand Down Expand Up @@ -289,6 +293,7 @@ PLATFORMS
DEPENDENCIES
attr_json
boba!
kaminari-activerecord
minitest
minitest-hooks
minitest-reporters
Expand Down
85 changes: 85 additions & 0 deletions lib/tapioca/dsl/compilers/kaminari.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# typed: strict
# frozen_string_literal: true

return unless defined?(Kaminari)

require "tapioca/dsl/helpers/active_record_constants_helper"

module Tapioca
module Dsl
module Compilers
# `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
# ~~~
class Kaminari < Tapioca::Dsl::Compiler
extend T::Sig
include Helpers::ActiveRecordConstantsHelper

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

sig { override.void }
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

class << self
extend T::Sig

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
descendants_of(::ActiveRecord::Base).reject(&:abstract_class?)
end
end

private

sig { returns(T::Array[[String, String]]) }
def target_modules
if compiler_enabled?("ActiveRecordRelations")
[
[RelationMethodsModuleName, RelationClassName],
[AssociationRelationMethodsModuleName, AssociationRelationClassName],
]
else
[[RelationMethodsModuleName, "T.untyped"]]
end
end
end
end
end
end
30 changes: 30 additions & 0 deletions manual/compiler_kaminari.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Kaminari

`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
~~~
1 change: 1 addition & 0 deletions manual/compilers.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This list is an evergeen list of currently available compilers.
* [ActiveRecordAssociationsPersisted](compiler_activerecordassociationspersisted.md)
* [ActiveRecordColumnsPersisted](compiler_activerecordcolumnspersisted.md)
* [AttrJson](compiler_attrjson.md)
* [Kaminari](compiler_kaminari.md)
* [MoneyRails](compiler_moneyrails.md)
* [Paperclip](compiler_paperclip.md)
<!-- END_COMPILER_LIST -->
6 changes: 6 additions & 0 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 106 additions & 0 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading