aboutsummaryrefslogtreecommitdiffstats
path: root/actiontext/lib/action_text/attachables/content_attachment.rb
blob: 804f74713fa12d6199711e50e4913d8f373641fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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