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 DSL compiler for Kaminari #2100

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -58,6 +58,7 @@ group :development, :test do
gem "bcrypt"
gem "xpath"
gem "kredis"
gem "kaminari-activerecord"
end

group :test do
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ GEM
faraday (>= 1.10, < 3.0)
faraday-gzip (>= 1.0, < 3.0)
rack (>= 0.2)
kaminari-activerecord (1.2.2)
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
kramdown (2.5.1)
rexml (>= 3.3.9)
kredis (1.7.0)
Expand Down Expand Up @@ -384,6 +388,7 @@ DEPENDENCIES
identity_cache
irb
json_api_client
kaminari-activerecord
kramdown (~> 2.5)
kredis
minitest
Expand Down
83 changes: 83 additions & 0 deletions lib/tapioca/dsl/compilers/kaminari.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# 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 < 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
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 @@ -32,6 +32,7 @@ In the following section you will find all available DSL compilers:
* [GraphqlMutation](compiler_graphqlmutation.md)
* [IdentityCache](compiler_identitycache.md)
* [JsonApiClientResource](compiler_jsonapiclientresource.md)
* [Kaminari](compiler_kaminari.md)
* [Kredis](compiler_kredis.md)
* [MixedInClassAttributes](compiler_mixedinclassattributes.md)
* [Protobuf](compiler_protobuf.md)
Expand Down
106 changes: 106 additions & 0 deletions sorbet/rbi/gems/kaminari-activerecord@1.2.2.rbi

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

Loading
Loading