Class: Releaf::Content::Node::Copy
- Inherits:
-
Object
- Object
- Releaf::Content::Node::Copy
- Includes:
- Service
- Defined in:
- app/services/releaf/content/node/copy.rb
Instance Method Summary collapse
- #call ⇒ Object
- #cloned_content_attributes ⇒ Object
- #content_dragonfly_attributes ⇒ Object
- #duplicate_content ⇒ Object
- #duplicate_content_dragonfly_attributes(new_content) ⇒ Object
- #duplicate_under ⇒ Object
- #make_copy ⇒ Object
- #prevent_infinite_copy_loop ⇒ Object
Methods included from Service
Instance Method Details
#call ⇒ Object
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. end end |
#cloned_content_attributes ⇒ Object
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_attributes ⇒ Object
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_content ⇒ Object
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", "") = node.content.send(accessor_name) if .present? begin .path # verify that the file exists rescue Dragonfly::Job::Fetch::NotFound = nil end end new_content.send("#{attribute_name}=", nil) new_content.send("#{accessor_name}=", ) end end |
#duplicate_under ⇒ Object
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. do Releaf::Content::Node::SaveUnderParent.call(node: new_node, parent_id: parent_id) end end new_node end |
#make_copy ⇒ Object
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_loop ⇒ Object
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 |