方法
    
  
  
  
    
    
    
    
    
    
    
      实例公共方法
has_rich_text(name, encrypted: false, strict_loading: strict_loading_by_default, store_if_blank: true) 链接
提供对单个富文本属性的 body 和 attachments 的依赖的 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 驱动的编辑器发送的 attachments 链接。这些 attachments 使用 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 多态类型列中的类名。
源: 显示 | 在 GitHub 上
# 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() 链接
返回所有富文本关联的名称。
源: 显示 | 在 GitHub 上
# 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 模型。
源: 显示 | 在 GitHub 上
# File actiontext/lib/action_text/attribute.rb, line 95 def with_all_rich_text includes(rich_text_association_names) end