module ActionText::Attribute

def has_rich_text(name, encrypted: false, strict_loading: strict_loading_by_default)

corresponding rows.
`action_text_rich_texts.record_type` polymorphic type column of the
make sure to also update the class names in the
class names in the database. When renaming classes that use `has_rich_text`,
Note: Action Text relies on polymorphic associations, which in turn store


`strict_loading_by_default` class attribute (false by default).
`strict_loading:` will be set to the value of the
* `:strict_loading` - Pass true to force strict loading. When omitted,

`ActiveRecord::Encryption::EncryptableRecord.encrypts`. Default: false.
encryption will be non-deterministic. See
* `:encrypted` - Pass true to encrypt the rich text attribute. The

#### Options

Message.all.with_all_rich_text # Loads all rich text associations.
Message.all.with_rich_text_content_and_embeds # Avoids N+1 queries when you just want the body and attachments.
Message.all.with_rich_text_content # Avoids N+1 queries when you just want the body, not the attachments.

scope:
If you wish to preload the dependent RichText model, you can use the named

RichText model using Active Storage.
as sent via the Trix-powered editor. These attachments are associated with the
The dependent RichText model will also automatically process attachments links

message.content.to_plain_text # => "Funny times!"
message.content.to_s # => "

Funny times!

"
message.content? #=> true
message = Message.create!(content: "

Funny times!

")

end
has_rich_text :content
class Message < ActiveRecord::Base

is lazily instantiated and will be auto-saved when it's been changed. Example:
attachments for a single named rich text attribute. This dependent attribute
Provides access to a dependent RichText model that holds the body and
def has_rich_text(name, encrypted: false, strict_loading: strict_loading_by_default)
  class_eval <<-CODE, __FILE__, __LINE__ + 1
    def #{name}
      rich_text_#{name} || build_rich_text_#{name}
    end
    def #{name}?
      rich_text_#{name}.present?
    end
    def #{name}=(body)
      self.#{name}.body = body
    end
  CODE
  rich_text_class_name = encrypted ? "ActionText::EncryptedRichText" : "ActionText::RichText"
  has_one :"rich_text_#{name}", -> { where(name: name) },
    class_name: rich_text_class_name, as: :record, inverse_of: :record, autosave: true, dependent: :destroy,
    strict_loading: strict_loading
  scope :"with_rich_text_#{name}", -> { includes("rich_text_#{name}") }
  scope :"with_rich_text_#{name}_and_embeds", -> { includes("rich_text_#{name}": { embeds_attachments: :blob }) }
end

def rich_text_association_names

Returns the names of all rich text associations.
def rich_text_association_names
  reflect_on_all_associations(:has_one).collect(&:name).select { |n| n.start_with?("rich_text_") }
end

def with_all_rich_text

Eager load all dependent RichText models in bulk.
def with_all_rich_text
  includes(rich_text_association_names)
end