-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c632f4
commit 4f8f439
Showing
4 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
begin | ||
require "action_text" | ||
rescue LoadError | ||
return | ||
end | ||
|
||
module Tapioca | ||
module Dsl | ||
module Compilers | ||
# `Tapioca::Dsl::Compilers::ActionText` decorates RBI files for subclasses of | ||
# `ActiveRecord::Base` that declare [has_rich_text](https://edgeguides.rubyonrails.org/action_text_overview.html#creating-rich-text-content) | ||
# | ||
# For example, with the following `ActiveRecord::Base` subclass: | ||
# | ||
# ~~~rb | ||
# class Post < ApplicationRecord | ||
# has_rich_text :body | ||
# has_rich_text :title, encrypted: true | ||
# end | ||
# ~~~ | ||
# | ||
# this compiler will produce the RBI file `post.rbi` with the following content: | ||
# | ||
# ~~~rbi | ||
# # typed: strong | ||
# | ||
# class Post | ||
# sig { returns(ActionText::RichText) } | ||
# def body; end | ||
# | ||
# sig { params(value: T.nilable(T.any(ActionText::RichText, String))).returns(T.untyped) } | ||
# def body=(value); end | ||
# | ||
# sig { returns(T::Boolean) } | ||
# def body?; end | ||
# | ||
# sig { returns(ActionText::EncryptedRichText) } | ||
# def title; end | ||
# | ||
# sig { params(value: T.nilable(T.any(ActionText::EncryptedRichText, String))).returns(T.untyped) } | ||
# def title=(value); end | ||
# | ||
# sig { returns(T::Boolean) } | ||
# def title?; end | ||
# end | ||
# ~~~ | ||
class ActionText < Compiler | ||
extend T::Sig | ||
|
||
ConstantType = type_member { { fixed: T.class_of(::ActiveRecord::Base) } } | ||
|
||
sig { override.void } | ||
def decorate | ||
return if constant.rich_text_association_names.empty? | ||
|
||
root.create_path(constant) do |scope| | ||
constant.rich_text_association_names.each do |name| | ||
reflection = constant.reflections[name.to_s] | ||
type = reflection.options[:class_name] | ||
name = reflection.name.to_s.sub("rich_text_", "") | ||
scope.create_method( | ||
name, | ||
return_type: type, | ||
) | ||
scope.create_method( | ||
"#{name}?", | ||
return_type: "T::Boolean", | ||
) | ||
scope.create_method( | ||
"#{name}=", | ||
parameters: [create_param("value", type: "T.nilable(T.any(#{type}, String))")], | ||
return_type: "T.untyped", | ||
) | ||
end | ||
end | ||
end | ||
|
||
class << self | ||
extend T::Sig | ||
|
||
sig { override.returns(T::Enumerable[Module]) } | ||
def gather_constants | ||
descendants_of(::ActiveRecord::Base) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
## ActionText | ||
|
||
`Tapioca::Dsl::Compilers::ActionText` decorates RBI files for subclasses of | ||
`ActiveRecord::Base` that declare [has_rich_text](https://edgeguides.rubyonrails.org/action_text_overview.html#creating-rich-text-content) | ||
|
||
For example, with the following `ActiveRecord::Base` subclass: | ||
|
||
~~~rb | ||
class Post < ApplicationRecord | ||
has_rich_text :body | ||
has_rich_text :title, encrypted: true | ||
end | ||
~~~ | ||
|
||
this compiler will produce the RBI file `post.rbi` with the following content: | ||
|
||
~~~rbi | ||
# typed: strong | ||
|
||
class Post | ||
sig { returns(ActionText::RichText) } | ||
def body; end | ||
|
||
sig { params(value: T.nilable(T.any(ActionText::RichText, String))).returns(T.untyped) } | ||
def body=(value); end | ||
|
||
sig { returns(T::Boolean) } | ||
def body?; end | ||
|
||
sig { returns(ActionText::EncryptedRichText) } | ||
def title; end | ||
|
||
sig { params(value: T.nilable(T.any(ActionText::EncryptedRichText, String))).returns(T.untyped) } | ||
def title=(value); end | ||
|
||
sig { returns(T::Boolean) } | ||
def title?; end | ||
end | ||
~~~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module Tapioca | ||
module Dsl | ||
module Compilers | ||
class ActionTextSpec < ::DslSpec | ||
describe "Tapioca::Dsl::Compilers::ActionText" do | ||
before do | ||
add_ruby_file("require.rb", <<~RUBY) | ||
require "active_record" | ||
require "action_text" | ||
ActiveRecord::Base.include(ActionText::Attribute) | ||
ActiveRecord::Base.prepend(ActionText::Encryption) | ||
RUBY | ||
end | ||
|
||
describe "decorate" do | ||
it "generates an empty RBI file for ActiveRecord classes with no rich text" do | ||
add_ruby_file("post.rb", <<~RUBY) | ||
class Post < ActiveRecord::Base | ||
end | ||
RUBY | ||
|
||
expected = <<~RBI | ||
# typed: strong | ||
RBI | ||
|
||
assert_equal(expected, rbi_for(:Post)) | ||
end | ||
|
||
it "generates RBI file for ActiveRecord classes with a rich text" do | ||
add_ruby_file("post.rb", <<~RUBY) | ||
class Post < ActiveRecord::Base | ||
has_rich_text :body | ||
has_rich_text :title | ||
end | ||
RUBY | ||
|
||
expected = <<~RBI | ||
# typed: strong | ||
class Post | ||
sig { returns(ActionText::RichText) } | ||
def body; end | ||
sig { params(value: T.nilable(T.any(ActionText::RichText, String))).returns(T.untyped) } | ||
def body=(value); end | ||
sig { returns(T::Boolean) } | ||
def body?; end | ||
sig { returns(ActionText::RichText) } | ||
def title; end | ||
sig { params(value: T.nilable(T.any(ActionText::RichText, String))).returns(T.untyped) } | ||
def title=(value); end | ||
sig { returns(T::Boolean) } | ||
def title?; end | ||
end | ||
RBI | ||
|
||
assert_equal(expected, rbi_for(:Post)) | ||
end | ||
|
||
it "generates RBI file for ActiveRecord classes with encrypted rich text" do | ||
add_ruby_file("post.rb", <<~RUBY) | ||
class Post < ActiveRecord::Base | ||
has_rich_text :body, encrypted: true | ||
end | ||
RUBY | ||
|
||
expected = <<~RBI | ||
# typed: strong | ||
class Post | ||
sig { returns(ActionText::EncryptedRichText) } | ||
def body; end | ||
sig { params(value: T.nilable(T.any(ActionText::EncryptedRichText, String))).returns(T.untyped) } | ||
def body=(value); end | ||
sig { returns(T::Boolean) } | ||
def body?; end | ||
end | ||
RBI | ||
|
||
assert_equal(expected, rbi_for(:Post)) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |