diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2018-06-27 16:20:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-27 16:20:58 +0200 |
commit | b430ba4754f59934acb4806c83d10dee5af8de9e (patch) | |
tree | 1d7482d313f05a800f63a3a490293c9593b2a6a2 /lib/action_text/attribute.rb | |
parent | a4af9580d604221ec36d2cd5671eb7f256ae5173 (diff) | |
parent | 60050ee41275eda25601beaa453e26841511d492 (diff) | |
download | rails-b430ba4754f59934acb4806c83d10dee5af8de9e.tar.gz rails-b430ba4754f59934acb4806c83d10dee5af8de9e.tar.bz2 rails-b430ba4754f59934acb4806c83d10dee5af8de9e.zip |
Merge pull request #1 from basecamp/separate-rich-text-record
Separate Rich Text record
Diffstat (limited to 'lib/action_text/attribute.rb')
-rw-r--r-- | lib/action_text/attribute.rb | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/lib/action_text/attribute.rb b/lib/action_text/attribute.rb index 32dd1d0e3f..299c26b29f 100644 --- a/lib/action_text/attribute.rb +++ b/lib/action_text/attribute.rb @@ -3,15 +3,41 @@ module ActionText extend ActiveSupport::Concern class_methods do - def has_rich_text(attribute_name) - serialize(attribute_name, ActionText::Content) + # 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.body.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_emebds # 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} + self.rich_text_#{name} ||= ActionText::RichText.new(name: "#{name}", record: self) + end - has_many_attached "#{attribute_name}_attachments" + def #{name}=(body) + #{name}.body = body + end + CODE - after_save do - blobs = public_send(attribute_name).attachments.map(&:attachable) - public_send("#{attribute_name}_attachments_blobs=", blobs) - end + has_one :"rich_text_#{name}", -> { where(name: name) }, class_name: "ActionText::RichText", as: :record, inverse_of: :record, 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 }) } + + after_save { public_send(name).save if public_send(name).changed? } end end end |