Class: TrixEmbed::Attachment

Inherits:
Object
  • Object
show all
Includes:
ActionText::Attachable, ActiveModel::Model, GlobalID::Identification
Defined in:
app/models/trix_embed/attachment.rb

Constant Summary collapse

ALLOWED_TAGS =
ActionText::ContentHelper.allowed_tags + %w[iframe]
ALLOWED_ATTRIBUTES =
(
ActionText::ContentHelper.allowed_attributes + %w[
  allow
  allowfullscreen
  allowpaymentrequest
  credentialless
  csp
  data-trix-embed
  data-trix-embed-error
  data-trix-embed-prohibited
  data-trix-embed-warning
  loading
  referrerpolicy
  sandbox
  srcdoc
]) - %w[class style]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Attachment

Returns a new instance of Attachment.



81
82
83
# File 'app/models/trix_embed/attachment.rb', line 81

def initialize(attributes = {})
  super @attributes = attributes.with_indifferent_access.slice(:content_type, :content)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



78
79
80
# File 'app/models/trix_embed/attachment.rb', line 78

def attributes
  @attributes
end

#contentObject

Returns the value of attribute content.



79
80
81
# File 'app/models/trix_embed/attachment.rb', line 79

def content
  @content
end

#content_typeObject

Returns the value of attribute content_type.



79
80
81
# File 'app/models/trix_embed/attachment.rb', line 79

def content_type
  @content_type
end

Class Method Details

.find(id) ⇒ Object



73
74
75
# File 'app/models/trix_embed/attachment.rb', line 73

def find(id)
  new JSON.parse(id)
end

.rewrite_for_display(content) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/trix_embed/attachment.rb', line 28

def rewrite_for_display(content)
  fragment = Nokogiri::HTML.fragment(content)
  matches = fragment.css("#{ActionText::Attachment.tag_name}[sgid][content-type='#{Mime::Type.lookup_by_extension(:trix_embed_attachment)}']")

  matches.each do |match|
    attachment = ActionText::Attachment.from_node(match)
    attachable = attachment.attachable

    html = ActionText::Content.render(
      partial: attachable.to_action_text_content_partial_path,
      locals: {attachable: attachable}
    )

    html = ActionText::ContentHelper.sanitizer.sanitize(
      html,
      tags: ALLOWED_TAGS,
      attributes: ALLOWED_ATTRIBUTES,
      scrubber: nil
    )

    match.replace html
  end

  fragment.to_html.html_safe
end

.rewrite_for_storage(trix_html) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/trix_embed/attachment.rb', line 54

def rewrite_for_storage(trix_html)
  fragment = Nokogiri::HTML.fragment(trix_html)
  matches = fragment.css("[data-trix-attachment][data-trix-content-type='#{Mime::Type.lookup_by_extension(:trix_embed_attachment)}']")

  matches.each do |match|
    data = JSON.parse(match["data-trix-attachment"]).deep_transform_keys(&:underscore)

    attachable = TrixEmbed::Attachment.new(data)
    attachment = ActionText::Attachment.from_attachable(attachable)

    match.replace ActionText::Content.render(
      partial: attachable.to_partial_path,
      locals: {attachment: attachment}
    )
  end

  fragment.to_html
end

Instance Method Details

#idObject



85
86
87
# File 'app/models/trix_embed/attachment.rb', line 85

def id
  attributes.to_json
end

#persisted?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/models/trix_embed/attachment.rb', line 89

def persisted?
  true
end

#to_action_text_content_partial_pathObject

What gets presented in the browser (show view)



99
100
101
# File 'app/models/trix_embed/attachment.rb', line 99

def to_action_text_content_partial_path
  "trix_embed/action_text_content_show"
end

#to_partial_pathObject

What gets saved to the database



94
95
96
# File 'app/models/trix_embed/attachment.rb', line 94

def to_partial_path
  "trix_embed/action_text_attachment"
end

#to_trix_content_attachment_partial_pathObject

What gets presented in the browser (edit view)



104
105
106
# File 'app/models/trix_embed/attachment.rb', line 104

def to_trix_content_attachment_partial_path
  "trix_embed/action_text_content_edit"
end