aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_text/attachments
diff options
context:
space:
mode:
Diffstat (limited to 'lib/active_text/attachments')
-rw-r--r--lib/active_text/attachments/caching.rb14
-rw-r--r--lib/active_text/attachments/minification.rb15
-rw-r--r--lib/active_text/attachments/trix_conversion.rb32
3 files changed, 61 insertions, 0 deletions
diff --git a/lib/active_text/attachments/caching.rb b/lib/active_text/attachments/caching.rb
new file mode 100644
index 0000000000..b0194170e1
--- /dev/null
+++ b/lib/active_text/attachments/caching.rb
@@ -0,0 +1,14 @@
+module ActiveText
+ module Attachments
+ module Caching
+ def cache_key(*args)
+ [self.class.name, cache_digest, *attachable.cache_key(*args)].join("/")
+ end
+
+ private
+ def cache_digest
+ Digest::SHA256.hexdigest(node.to_s)
+ end
+ end
+ end
+end
diff --git a/lib/active_text/attachments/minification.rb b/lib/active_text/attachments/minification.rb
new file mode 100644
index 0000000000..b33822862f
--- /dev/null
+++ b/lib/active_text/attachments/minification.rb
@@ -0,0 +1,15 @@
+module ActiveText
+ module Attachments
+ module Minification
+ extend ActiveSupport::Concern
+
+ class_methods do
+ def fragment_by_minifying_attachments(content)
+ Fragment.wrap(content).replace(ActiveText::Attachment::SELECTOR) do |node|
+ node.tap { |node| node.inner_html = "" }
+ end
+ end
+ end
+ end
+ end
+end
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