Sha256: de129d82377f107f64dae52aac9b60607aa51911979ef685cc881b5f00f5306a

Contents?: true

Size: 1.86 KB

Versions: 36

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module ActionText
  module Attribute
    extend ActiveSupport::Concern

    class_methods do
      # Provides access to a dependent RichText model that holds the body and attachments for a single named rich text attribute.
      # This dependent attribute is lazily instantiated and will be auto-saved when it's been changed. Example:
      #
      #   class Message < ActiveRecord::Base
      #     has_rich_text :content
      #   end
      #
      #   message = Message.create!(content: "<h1>Funny times!</h1>")
      #   message.content.to_s # => "<h1>Funny times!</h1>"
      #   message.content.to_plain_text # => "Funny times!"
      #
      # The dependent RichText model will also automatically process attachments links as sent via the Trix-powered editor.
      # These attachments are associated with the RichText model using Active Storage.
      #
      # If you wish to preload the dependent RichText model, you can use the named scope:
      #
      #   Message.all.with_rich_text_content # Avoids N+1 queries when you just want the body, not the attachments.
      #   Message.all.with_rich_text_content_and_embeds # Avoids N+1 queries when you just want the body and attachments.
      def has_rich_text(name)
        class_eval <<-CODE, __FILE__, __LINE__ + 1
          def #{name}
            rich_text_#{name} || build_rich_text_#{name}
          end

          def #{name}=(body)
            self.#{name}.body = body
          end
        CODE

        has_one :"rich_text_#{name}", -> { where(name: name) },
          class_name: "ActionText::RichText", as: :record, inverse_of: :record, autosave: true, dependent: :destroy

        scope :"with_rich_text_#{name}", -> { includes("rich_text_#{name}") }
        scope :"with_rich_text_#{name}_and_embeds", -> { includes("rich_text_#{name}": { embeds_attachments: :blob }) }
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 4 rubygems

Version Path
actiontext-6.0.6.1 lib/action_text/attribute.rb
actiontext-6.0.6 lib/action_text/attribute.rb
actiontext-6.0.5.1 lib/action_text/attribute.rb
actiontext-6.0.5 lib/action_text/attribute.rb
actiontext-6.0.4.8 lib/action_text/attribute.rb
actiontext-6.0.4.7 lib/action_text/attribute.rb
actiontext-6.0.4.6 lib/action_text/attribute.rb
actiontext-6.0.4.5 lib/action_text/attribute.rb
actiontext-6.0.4.4 lib/action_text/attribute.rb
actiontext-6.0.4.3 lib/action_text/attribute.rb
actiontext-6.0.4.2 lib/action_text/attribute.rb
actiontext-6.0.4.1 lib/action_text/attribute.rb
actiontext-6.0.4 lib/action_text/attribute.rb
actiontext-6.0.3.7 lib/action_text/attribute.rb
actiontext-6.0.3.6 lib/action_text/attribute.rb
actiontext-6.0.3.5 lib/action_text/attribute.rb
actiontext-6.0.3.4 lib/action_text/attribute.rb
actiontext-6.0.3.3 lib/action_text/attribute.rb
actiontext-6.0.3.2 lib/action_text/attribute.rb
actiontext-6.0.3.1 lib/action_text/attribute.rb