Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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 "devise"
gem "kaminari-activerecord"
gem "money-rails"
gem "paperclip"
Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ GEM
attr_json (2.5.0)
activerecord (>= 6.0.0, < 8.1)
base64 (0.2.0)
bcrypt (3.1.20)
benchmark (0.4.0)
bigdecimal (3.1.8)
builder (3.3.0)
Expand All @@ -91,6 +92,12 @@ GEM
connection_pool (2.4.1)
crass (1.0.6)
date (3.3.4)
devise (4.9.4)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
drb (2.2.1)
erubi (1.13.1)
globalid (1.2.1)
Expand Down Expand Up @@ -157,6 +164,7 @@ GEM
nokogiri (1.16.7)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
orm_adapter (0.5.0)
paperclip (6.1.0)
activemodel (>= 4.2.0)
activesupport (>= 4.2.0)
Expand Down Expand Up @@ -218,6 +226,9 @@ GEM
regexp_parser (2.9.2)
reline (0.5.10)
io-console (~> 0.5)
responders (3.1.1)
actionpack (>= 5.2)
railties (>= 5.2)
rexml (3.3.9)
rubocop (1.65.1)
json (~> 2.3)
Expand Down Expand Up @@ -275,6 +286,8 @@ GEM
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
useragent (0.16.10)
warden (1.2.9)
rack (>= 2.0.9)
webrick (1.8.1)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
Expand All @@ -292,6 +305,7 @@ PLATFORMS
DEPENDENCIES
attr_json
boba!
devise
kaminari-activerecord
minitest
minitest-hooks
Expand Down
66 changes: 66 additions & 0 deletions lib/tapioca/dsl/compilers/devise.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# typed: true
# frozen_string_literal: true

return unless defined?(Devise)

module Tapioca
module Dsl
module Compilers
# `Tapioca::Dsl::Compilers::Devise` generates RBI files for `ApplicationController``
#
# For example, with the following routes configuration:
#
# ~~~rb
# Rails.application.routes.draw do
# devise_for :users
# end
# ~~~
#
# this compiler will produce the RBI file `user.rbi` with the following content:
#
# ~~~rbi
# # user.rbi
# # typed: true
# class ApplicationController
# sig { returns(T.nilable(T::Hash[T.untyped, T.untyped])) }
# def user_session; end
#
# sig { returns(T::Boolean) }
# def user_signed_in?; end
#
# sig { void }
# def authenticate_user!; end
#
# sig { returns(T.nilable(User)) }
# def current_user; end
# end
# ~~~
class Devise < Tapioca::Dsl::Compiler
extend T::Sig

ConstantType = type_member { { upper: ActiveRecord::Base } }

class << self
extend T::Sig

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
::Devise.mappings.values.map(&:class_name).map(&:constantize)
end
end

sig { override.void }
def decorate
::Devise.mappings.each do |key, mapping|
root.create_path(ApplicationController) do |klass|
klass.create_method("current_#{key}", return_type: "T.nilable(#{mapping.class_name})")
klass.create_method("authenticate_#{key}!", return_type: "void")
klass.create_method("#{key}_session", return_type: "T.nilable(T::Hash[T.untyped, T.untyped])")
klass.create_method("#{key}_signed_in?", return_type: "T::Boolean")
end
end
end
end
end
end
end
31 changes: 31 additions & 0 deletions manual/compiler_devise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## Devise

`Tapioca::Dsl::Compilers::Devise` generates RBI files for `ApplicationController``

For example, with the following routes configuration:

~~~rb
Rails.application.routes.draw do
devise_for :users
end
~~~

this compiler will produce the RBI file `user.rbi` with the following content:

~~~rbi
# user.rbi
# typed: true
class ApplicationController
sig { returns(T.nilable(T::Hash[T.untyped, T.untyped])) }
def user_session; end

sig { returns(T::Boolean) }
def user_signed_in?; end

sig { void }
def authenticate_user!; end

sig { returns(T.nilable(User)) }
def current_user; 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)
* [Devise](compiler_devise.md)
* [Kaminari](compiler_kaminari.md)
* [MoneyRails](compiler_moneyrails.md)
* [Paperclip](compiler_paperclip.md)
Expand Down
Loading
Loading