Class: RicherText::Content

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Conversion, Rendering, Serialization
Defined in:
lib/richer_text/content.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Content

Returns a new instance of Content.



7
8
9
10
11
# File 'lib/richer_text/content.rb', line 7

def initialize(body)
  @body = body

  @fragment = RicherText::Fragment.parse(body)
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



51
52
53
# File 'lib/richer_text/content.rb', line 51

def body
  @body
end

#fragmentObject (readonly)

Returns the value of attribute fragment.



50
51
52
# File 'lib/richer_text/content.rb', line 50

def fragment
  @fragment
end

Instance Method Details

#image_blob_idsObject



41
42
43
44
45
46
47
48
# File 'lib/richer_text/content.rb', line 41

def image_blob_ids
  images.map do |image|
    # Ensure old images from ActionText which used the proxy url are supported
    image = image.gsub("/proxy/", "/redirect/")
    # Fetch the signed_blob_id from the image src
    image.split("/rails/active_storage/blobs/redirect/").last.split("/").first
  end
end

#image_blobsObject



37
38
39
# File 'lib/richer_text/content.rb', line 37

def image_blobs
  image_blob_ids.map { |id| ActiveStorage::Blob.find_signed(id) }
end

#imagesObject



25
26
27
28
29
# File 'lib/richer_text/content.rb', line 25

def images
  @images ||= fragment.find_all("img").map do |img|
    img.attributes["src"].value
  end.uniq
end

#inspectObject



13
14
15
# File 'lib/richer_text/content.rb', line 13

def inspect
  "#<#{self.class.name} #{to_html.truncate(25).inspect}>"
end

#mentionees_global_idsObject



31
32
33
34
35
# File 'lib/richer_text/content.rb', line 31

def mentionees_global_ids
  global_ids = fragment.find_all("span[data-type=mention]").map do |span|
    span.attributes["data-id"].value
  end.uniq
end

#to_htmlObject



21
22
23
# File 'lib/richer_text/content.rb', line 21

def to_html
  body
end

#to_sObject



17
18
19
# File 'lib/richer_text/content.rb', line 17

def to_s
  render partial: to_partial_path, layout: false, locals: { content: self }
end