From f1d74871e7f00e8bbde3501a759487ac8cc4c3fc Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 13 Apr 2018 16:23:04 -0700 Subject: Rename from Active Text to Action Text This is more like Action View than Active Model. --- lib/action_text/attachables/content_attachment.rb | 36 +++++++++++++++++++ lib/action_text/attachables/missing_attachable.rb | 11 ++++++ lib/action_text/attachables/remote_image.rb | 44 +++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 lib/action_text/attachables/content_attachment.rb create mode 100644 lib/action_text/attachables/missing_attachable.rb create mode 100644 lib/action_text/attachables/remote_image.rb (limited to 'lib/action_text/attachables') diff --git a/lib/action_text/attachables/content_attachment.rb b/lib/action_text/attachables/content_attachment.rb new file mode 100644 index 0000000000..3ebd734786 --- /dev/null +++ b/lib/action_text/attachables/content_attachment.rb @@ -0,0 +1,36 @@ +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/lib/action_text/attachables/missing_attachable.rb b/lib/action_text/attachables/missing_attachable.rb new file mode 100644 index 0000000000..54b36d3cce --- /dev/null +++ b/lib/action_text/attachables/missing_attachable.rb @@ -0,0 +1,11 @@ +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/lib/action_text/attachables/remote_image.rb b/lib/action_text/attachables/remote_image.rb new file mode 100644 index 0000000000..2333427371 --- /dev/null +++ b/lib/action_text/attachables/remote_image.rb @@ -0,0 +1,44 @@ +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 -- cgit v1.2.3