Skip to content

Commit

Permalink
Add Action Text compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiculescu committed Sep 1, 2023
1 parent 0c632f4 commit 4f8f439
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 0 deletions.
92 changes: 92 additions & 0 deletions lib/tapioca/dsl/compilers/action_text.rb
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
39 changes: 39 additions & 0 deletions manual/compiler_actiontext.md
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
~~~
1 change: 1 addition & 0 deletions manual/compilers.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ In the following section you will find all available DSL compilers:
* [AASM](compiler_aasm.md)
* [ActionControllerHelpers](compiler_actioncontrollerhelpers.md)
* [ActionMailer](compiler_actionmailer.md)
* [ActionText](compiler_actiontext.md)
* [ActiveJob](compiler_activejob.md)
* [ActiveModelAttributes](compiler_activemodelattributes.md)
* [ActiveModelSecurePassword](compiler_activemodelsecurepassword.md)
Expand Down
98 changes: 98 additions & 0 deletions spec/tapioca/dsl/compilers/action_text_spec.rb
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

0 comments on commit 4f8f439

Please sign in to comment.