diff options
author | Sam Stephenson <sam@37signals.com> | 2018-02-07 18:26:19 -0600 |
---|---|---|
committer | Sam Stephenson <sam@37signals.com> | 2018-02-07 18:26:19 -0600 |
commit | 68d350ddacedf604717f0d1074d7624fa57757c2 (patch) | |
tree | a01852130448e37368ed2fd4af7e930e6c2cd0f2 /lib/active_text/attachments/trix_conversion.rb | |
parent | e22ba227a694b8426e69dbce640c5b0e4f39f574 (diff) | |
download | rails-68d350ddacedf604717f0d1074d7624fa57757c2.tar.gz rails-68d350ddacedf604717f0d1074d7624fa57757c2.tar.bz2 rails-68d350ddacedf604717f0d1074d7624fa57757c2.zip |
Initial import from BC3 RichText
Diffstat (limited to 'lib/active_text/attachments/trix_conversion.rb')
-rw-r--r-- | lib/active_text/attachments/trix_conversion.rb | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/active_text/attachments/trix_conversion.rb b/lib/active_text/attachments/trix_conversion.rb new file mode 100644 index 0000000000..25eada6709 --- /dev/null +++ b/lib/active_text/attachments/trix_conversion.rb @@ -0,0 +1,32 @@ +module ActiveText + module Attachments + module TrixConversion + extend ActiveSupport::Concern + + class_methods do + def fragment_by_converting_trix_attachments(content) + Fragment.wrap(content).replace(TrixAttachment::SELECTOR) do |node| + from_trix_attachment(TrixAttachment.new(node)) + end + end + + def from_trix_attachment(trix_attachment) + from_attributes(trix_attachment.attributes) + end + end + + def to_trix_attachment(content = trix_attachment_content) + attributes = full_attributes.dup + attributes["content"] = content if content + TrixAttachment.from_attributes(attributes) + end + + private + def trix_attachment_content + if partial_path = attachable.try(:to_trix_content_attachment_partial_path) + ApplicationRenderer.render(partial: partial_path, object: self, as: model_name.element) + end + end + end + end +end |