Class: ActionText::AttachmentGallery

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
actiontext/lib/action_text/attachment_gallery.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveModel::Model

#persisted?

Methods included from ActiveSupport::Concern

#append_features, #class_methods, extended, #included, #prepend_features, #prepended

Methods included from ActiveModel::Conversion

#to_key, #to_model, #to_param, #to_partial_path

Methods included from ActiveModel::Validations

#errors, #initialize_dup, #invalid?, #valid?, #validate!, #validates_with

Methods included from ActiveModel::AttributeAssignment

#assign_attributes

Constructor Details

#initialize(node) ⇒ AttachmentGallery

Returns a new instance of AttachmentGallery.


52
53
54
# File 'actiontext/lib/action_text/attachment_gallery.rb', line 52

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node


50
51
52
# File 'actiontext/lib/action_text/attachment_gallery.rb', line 50

def node
  @node
end

Class Method Details

.attachment_selectorObject


41
42
43
# File 'actiontext/lib/action_text/attachment_gallery.rb', line 41

def attachment_selector
  "#{ActionText::Attachment.tag_name}[presentation=gallery]"
end

25
26
27
28
29
30
31
32
33
34
35
# File 'actiontext/lib/action_text/attachment_gallery.rb', line 25

def find_attachment_gallery_nodes(content)
  Fragment.wrap(content).find_all(selector).select do |node|
    node.children.all? do |child|
      if child.text?
        /\A(\n|\ )*\z/.match?(child.text)
      else
        child.matches? attachment_selector
      end
    end
  end
end

.fragment_by_canonicalizing_attachment_galleries(content) ⇒ Object


11
12
13
14
15
# File 'actiontext/lib/action_text/attachment_gallery.rb', line 11

def fragment_by_canonicalizing_attachment_galleries(content)
  fragment_by_replacing_attachment_gallery_nodes(content) do |node|
    "<#{TAG_NAME}>#{node.inner_html}</#{TAG_NAME}>"
  end
end

17
18
19
20
21
22
23
# File 'actiontext/lib/action_text/attachment_gallery.rb', line 17

def fragment_by_replacing_attachment_gallery_nodes(content)
  Fragment.wrap(content).update do |source|
    find_attachment_gallery_nodes(source).each do |node|
      node.replace(yield(node).to_s)
    end
  end
end

.from_node(node) ⇒ Object


37
38
39
# File 'actiontext/lib/action_text/attachment_gallery.rb', line 37

def from_node(node)
  new(node)
end

.selectorObject


45
46
47
# File 'actiontext/lib/action_text/attachment_gallery.rb', line 45

def selector
  "#{TAG_NAME}:has(#{attachment_selector} + #{attachment_selector})"
end

Instance Method Details

#attachmentsObject


56
57
58
59
60
# File 'actiontext/lib/action_text/attachment_gallery.rb', line 56

def attachments
  @attachments ||= node.css(ActionText::AttachmentGallery.attachment_selector).map do |node|
    ActionText::Attachment.from_node(node).with_full_attributes
  end
end

#inspectObject


66
67
68
# File 'actiontext/lib/action_text/attachment_gallery.rb', line 66

def inspect
  "#<#{self.class.name} size=#{size.inspect}>"
end

#sizeObject


62
63
64
# File 'actiontext/lib/action_text/attachment_gallery.rb', line 62

def size
  attachments.size
end