diff options
author | George Claghorn <george@basecamp.com> | 2019-01-04 19:43:11 -0500 |
---|---|---|
committer | George Claghorn <george@basecamp.com> | 2019-01-04 22:22:49 -0500 |
commit | 0decd2ddc4a94cf522fc8ea8e6c73b9deedfdd93 (patch) | |
tree | 91b8ddcf86b15356cd8d3dee235bf53a1778572b /actiontext/lib/action_text/attachables | |
parent | 8a23a0e8c20c0cccf0073906d7dd7f809bfa836d (diff) | |
parent | cfe4674d3637c746cdb3c2b5131e2de498775529 (diff) | |
download | rails-0decd2ddc4a94cf522fc8ea8e6c73b9deedfdd93.tar.gz rails-0decd2ddc4a94cf522fc8ea8e6c73b9deedfdd93.tar.bz2 rails-0decd2ddc4a94cf522fc8ea8e6c73b9deedfdd93.zip |
Import Action Text
Diffstat (limited to 'actiontext/lib/action_text/attachables')
3 files changed, 97 insertions, 0 deletions
diff --git a/actiontext/lib/action_text/attachables/content_attachment.rb b/actiontext/lib/action_text/attachables/content_attachment.rb new file mode 100644 index 0000000000..804f74713f --- /dev/null +++ b/actiontext/lib/action_text/attachables/content_attachment.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module ActionText + module Attachables + class ContentAttachment + include ActiveModel::Model + + def self.from_node(node) + if node["content-type"] + if matches = node["content-type"].match(/vnd\.rubyonrails\.(.+)\.html/) + attachment = new(name: matches[1]) + attachment if attachment.valid? + end + end + end + + attr_accessor :name + validates_inclusion_of :name, in: %w( horizontal-rule ) + + def attachable_plain_text_representation(caption) + case name + when "horizontal-rule" + " ┄ " + else + " " + end + end + + def to_partial_path + "action_text/attachables/content_attachment" + end + + def to_trix_content_attachment_partial_path + "action_text/attachables/content_attachments/#{name.underscore}" + end + end + end +end diff --git a/actiontext/lib/action_text/attachables/missing_attachable.rb b/actiontext/lib/action_text/attachables/missing_attachable.rb new file mode 100644 index 0000000000..2f3bd40563 --- /dev/null +++ b/actiontext/lib/action_text/attachables/missing_attachable.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module ActionText + module Attachables + module MissingAttachable + extend ActiveModel::Naming + + def self.to_partial_path + "action_text/attachables/missing_attachable" + end + end + end +end diff --git a/actiontext/lib/action_text/attachables/remote_image.rb b/actiontext/lib/action_text/attachables/remote_image.rb new file mode 100644 index 0000000000..650b11862b --- /dev/null +++ b/actiontext/lib/action_text/attachables/remote_image.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +module ActionText + module Attachables + class RemoteImage + extend ActiveModel::Naming + + class << self + def from_node(node) + if node["url"] && content_type_is_image?(node["content-type"]) + new(attributes_from_node(node)) + end + end + + private + def content_type_is_image?(content_type) + content_type.to_s =~ /^image(\/.+|$)/ + end + + def attributes_from_node(node) + { url: node["url"], + content_type: node["content-type"], + width: node["width"], + height: node["height"] } + end + end + + attr_reader :url, :content_type, :width, :height + + def initialize(attributes = {}) + @url = attributes[:url] + @content_type = attributes[:content_type] + @width = attributes[:width] + @height = attributes[:height] + end + + def attachable_plain_text_representation(caption) + "[#{caption || "Image"}]" + end + + def to_partial_path + "action_text/attachables/remote_image" + end + end + end +end |