跳到內容 跳到搜尋
方法
H
R
W

實體公共方法

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

提供使用相依的 RichText 模型,來持有單一命名 rich text 屬性的本文和附件。此相依屬性會在需要時延遲實體化,當其有變動時會自動儲存。範例

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 來加密 rich text 屬性。加密會是非決定性的。請參閱 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()

傳回所有 rich text 關聯的名稱。

# 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