跳至内容 跳至搜索
方法
H
R
W

实例公共方法

has_rich_text(name, encrypted: false, strict_loading: strict_loading_by_default, store_if_blank: true)

提供对依赖的RichText模型的访问,该模型保存单个命名富文本属性的主体和附件。此依赖属性是延迟实例化的,并在发生更改时自动保存。示例

class Message < ActiveRecord::Base
  has_rich_text :content
end

message = Message.create!(content: "<h1>Funny times!</h1>")
message.content? #=> true
message.content.to_s # => "<h1>Funny times!</h1>"
message.content.to_plain_text # => "Funny times!"

依赖的RichText模型也将自动处理通过 Trix 支持的编辑器发送的附件链接。这些附件使用 Active Storage 与RichText模型关联。

如果您想预加载依赖的RichText模型,可以使用命名范围

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.
Message.all.with_all_rich_text # Loads all rich text associations.

选项

  • :encrypted - 传递 true 以加密富文本属性。加密将是非确定性的。请参阅ActiveRecord::Encryption::EncryptableRecord.encrypts。默认值:false。

  • :strict_loading - 传递 true 强制严格加载。省略时,strict_loading: 将设置为类属性 strict_loading_by_default 的值(默认值为 false)。

  • :store_if_blank - 传递 false 以不创建具有空值的RichText记录,如果提供了空值。默认值:true。

注意:Action Text 依赖于多态关联,这些关联反过来在数据库中存储类名。当重命名使用has_rich_text的类时,请确保还更新相应行的action_text_rich_texts.record_type 多态类型列中的类名。

# File actiontext/lib/action_text/attribute.rb, line 53
      def has_rich_text(name, encrypted: false, strict_loading: strict_loading_by_default, store_if_blank: true)
        class_eval <<-CODE, __FILE__, __LINE__ + 1
          def #{name}
            rich_text_#{name} || build_rich_text_#{name}
          end

          def #{name}?
            rich_text_#{name}.present?
          end
        CODE

        if store_if_blank
          class_eval <<-CODE, __FILE__, __LINE__ + 1
            def #{name}=(body)
              self.#{name}.body = body
            end
          CODE
        else
          class_eval <<-CODE, __FILE__, __LINE__ + 1
            def #{name}=(body)
              if body.present?
                self.#{name}.body = body
              else
                if #{name}?
                  self.#{name}.body = body
                  self.#{name}.mark_for_destruction
                end
              end
            end
          CODE
        end

        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

rich_text_association_names()

返回所有富文本关联的名称。

# File actiontext/lib/action_text/attribute.rb, line 100
def rich_text_association_names
  reflect_on_all_associations(:has_one).collect(&:name).select { |n| n.start_with?("rich_text_") }
end

with_all_rich_text()

批量加载所有依赖的RichText模型。

# File actiontext/lib/action_text/attribute.rb, line 95
def with_all_rich_text
  includes(rich_text_association_names)
end