This commit is contained in:
David Heinemeier Hansson 2018-04-13 16:47:33 -07:00
parent 421b185dab
commit 7daa8cc63e
2 changed files with 33 additions and 7 deletions

@ -0,0 +1,9 @@
class ActionText::RichText < ActiveRecord::Base
serialize :body, ActionText::Content
has_many_attached :embeds
after_save do
self.embeds_attachments_blobs = body.attachments.map(&:attachable)
end
end

@ -3,16 +3,33 @@ module Attribute
extend ActiveSupport::Concern extend ActiveSupport::Concern
class_methods do class_methods do
def has_rich_text(attribute_name) def has_rich_text(name)
serialize(attribute_name, ActionText::Content) class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{name}
rich_text_#{name}
end
has_many_attached "#{attribute_name}_attachments" def #{name}=(body)
#{name}.body = body
end
CODE
after_save do has_one :"rich_text_#{name}", -> { where(name: name) }, class_name: "ActionText::RichText", as: :record, inverse_of: :record, dependent: false
blobs = public_send(attribute_name).attachments.map(&:attachable)
public_send("#{attribute_name}_attachments_blobs=", blobs) scope :"with_rich_text_#{name}", -> { includes("rich_text_#{name}") }
end
end end
# def has_rich_text(attribute_name)
# serialize(attribute_name, ActionText::Content)
#
# has_many_attached "#{attribute_name}_attachments"
#
# after_save do
# blobs = public_send(attribute_name).attachments.map(&:attachable)
# public_send("#{attribute_name}_attachments_blobs=", blobs)
# end
# end
end end
end end
end end