class Tapioca::Dsl::Compilers::ActionText
~~~
end
def title?; end
sig { returns(T::Boolean) }
def title=(value); end
sig { params(value: T.nilable(T.any(ActionText::EncryptedRichText, String))).returns(T.untyped) }
def title; end
sig { returns(ActionText::EncryptedRichText) }
def body?; end
sig { returns(T::Boolean) }
def body=(value); end
sig { params(value: T.nilable(T.any(ActionText::RichText, String))).returns(T.untyped) }
def body; end
sig { returns(ActionText::RichText) }
class Post
# typed: strong
~~~rbi
this compiler will produce the RBI file ‘post.rbi` with the following content:
~~~
end
has_rich_text :title, encrypted: true
has_rich_text :body
class Post < ApplicationRecord
~~~rb
For example, with the following `ActiveRecord::Base` subclass:
`ActiveRecord::Base` that declare [has_rich_text](edgeguides.rubyonrails.org/action_text_overview.html#creating-rich-text-content)
`Tapioca::Dsl::Compilers::ActionText` decorates RBI files for subclasses of
def action_text_associations(constant)
def action_text_associations(constant) # Implementation copied from https://github.com/rails/rails/blob/31052d0e518b9da103eea2f79d250242ed1e3705/actiontext/lib/action_text/attribute.rb#L66 constant.reflect_on_all_associations(:has_one) .map(&:name).map(&:to_s) .select { |n| n.start_with?("rich_text_") } end
def decorate
def decorate root.create_path(constant) do |scope| self.class.action_text_associations(constant).each do |name| reflection = constant.reflections.fetch(name) type = reflection.options.fetch(: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
def gather_constants
def gather_constants descendants_of(::ActiveRecord::Base) .reject(&:abstract_class?) .select { |c| action_text_associations(c).any? } end