Class: Releaf::Content::Node::Copy

Inherits:
Object
  • Object
show all
Includes:
Service
Defined in:
app/services/releaf/content/node/copy.rb

Instance Method Summary collapse

Methods included from Service

#add_error_and_raise

Instance Method Details

#callObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/services/releaf/content/node/copy.rb', line 7

def call
  prevent_infinite_copy_loop
  begin
    new_node = nil
    node.class.transaction do
      new_node = make_copy
    end
    new_node
  rescue ActiveRecord::RecordInvalid
    add_error_and_raise 'descendant invalid'
  else
    node.update_settings_timestamp
  end
end

#cloned_content_attributesObject



50
51
52
53
# File 'app/services/releaf/content/node/copy.rb', line 50

def cloned_content_attributes
  skippable_attribute_names = ["id"] + content_dragonfly_attributes
  node.content.attributes.except(*skippable_attribute_names)
end

#content_dragonfly_attributesObject



73
74
75
76
77
# File 'app/services/releaf/content/node/copy.rb', line 73

def content_dragonfly_attributes
  node.content.class.attribute_names.select do |attribute_name|
    Releaf::Builders::Utilities::ResolveAttributeFieldMethodName.new(object: node.content, attribute_name: attribute_name).file?
  end
end

#duplicate_contentObject



40
41
42
43
44
45
46
47
48
# File 'app/services/releaf/content/node/copy.rb', line 40

def duplicate_content
  return if node.content_id.blank?

  new_content = node.content.class.new(cloned_content_attributes)
  duplicate_content_dragonfly_attributes(new_content)

  new_content.save!
  new_content
end

#duplicate_content_dragonfly_attributes(new_content) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/releaf/content/node/copy.rb', line 55

def duplicate_content_dragonfly_attributes(new_content)
  content_dragonfly_attributes.each do |attribute_name|
    accessor_name = attribute_name.gsub("_uid", "")
    dragonfly_attachment = node.content.send(accessor_name)

    if dragonfly_attachment.present?
      begin
        dragonfly_attachment.path  # verify that the file exists
      rescue Dragonfly::Job::Fetch::NotFound
        dragonfly_attachment = nil
      end
    end

    new_content.send("#{attribute_name}=", nil)
    new_content.send("#{accessor_name}=", dragonfly_attachment)
  end
end

#duplicate_underObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/services/releaf/content/node/copy.rb', line 27

def duplicate_under
  new_node = nil
  node.class.transaction do
    new_node = node.class.new
    new_node.assign_attributes_from(node)
    new_node.content_id = duplicate_content.try(:id)
    new_node.prevent_auto_update_settings_timestamp do
      Releaf::Content::Node::SaveUnderParent.call(node: new_node, parent_id: parent_id)
    end
  end
  new_node
end

#make_copyObject



79
80
81
82
83
84
85
86
87
# File 'app/services/releaf/content/node/copy.rb', line 79

def make_copy
  new_node = duplicate_under

  node.children.each do |child|
    self.class.new(node: child, parent_id: new_node.id).make_copy
  end

  new_node
end

#prevent_infinite_copy_loopObject



22
23
24
25
# File 'app/services/releaf/content/node/copy.rb', line 22

def prevent_infinite_copy_loop
  return if node.self_and_descendants.find_by_id(parent_id).blank?
  add_error_and_raise("source or descendant node can't be parent of new node")
end