Class: Para::Cloneable::AttachmentsCloner
- Inherits:
-
Object
- Object
- Para::Cloneable::AttachmentsCloner
- Defined in:
- lib/para/cloneable/attachments_cloner.rb
Constant Summary collapse
- ATTACHMENTS_RELATION_REGEX =
Handle both one and many attachment relations
/_attachments?\z/
Instance Attribute Summary collapse
-
#clone ⇒ Object
readonly
Returns the value of attribute clone.
-
#dictionary ⇒ Object
readonly
Returns the value of attribute dictionary.
-
#original ⇒ Object
readonly
Returns the value of attribute original.
Instance Method Summary collapse
- #clone! ⇒ Object
- #clone_attachment(name, original_attachment) ⇒ Object
- #clone_attachment_if_needed(name, attachment) ⇒ Object
-
#find_cloned_attachment(attachment_target, original_blob) ⇒ Object
Seemlessly handle one and many attachment relations return values and fetch the attachment that we just cloned by comparing blobs checksum, as depending which ActiveStorage version we’re on (Rails 5.2 or 6), the ‘#attach` method doesn’t always return the same, so for now we still handle the Rails 5.2 case.
-
#initialize(original, clone, dictionary) ⇒ AttachmentsCloner
constructor
A new instance of AttachmentsCloner.
-
#store_cloned(source, clone) ⇒ Object
This stores the source and clone resources into the deep_clone dictionary, which simulates what the deep_cloneable gem does when it clones a resource.
- #store_key_for(attachment) ⇒ Object
Constructor Details
#initialize(original, clone, dictionary) ⇒ AttachmentsCloner
Returns a new instance of AttachmentsCloner.
9 10 11 12 13 |
# File 'lib/para/cloneable/attachments_cloner.rb', line 9 def initialize(original, clone, dictionary) @original = original @clone = clone @dictionary = dictionary end |
Instance Attribute Details
#clone ⇒ Object (readonly)
Returns the value of attribute clone.
4 5 6 |
# File 'lib/para/cloneable/attachments_cloner.rb', line 4 def clone @clone end |
#dictionary ⇒ Object (readonly)
Returns the value of attribute dictionary.
4 5 6 |
# File 'lib/para/cloneable/attachments_cloner.rb', line 4 def dictionary @dictionary end |
#original ⇒ Object (readonly)
Returns the value of attribute original.
4 5 6 |
# File 'lib/para/cloneable/attachments_cloner.rb', line 4 def original @original end |
Instance Method Details
#clone! ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/para/cloneable/attachments_cloner.rb', line 15 def clone! return unless defined?(ActiveStorage) = original.class.reflections.select do |name, reflection| name.to_s.match(ATTACHMENTS_RELATION_REGEX) && reflection.[:class_name] == 'ActiveStorage::Attachment' end .each do |name, reflection| association_target = original.send(name) next unless association_target if reflection.collection? association_target.each do || (name, ) end else (name, association_target) end end end |
#clone_attachment(name, original_attachment) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/para/cloneable/attachments_cloner.rb', line 48 def (name, ) association_name = name.gsub(ATTACHMENTS_RELATION_REGEX, '') original_blob = .blob # Handle missing file in storage service by bypassing the attachment cloning return unless ActiveStorage::Blob.service.exist?(original_blob&.key) Para::ActiveStorageDownloader.new().download_blob_to_tempfile do |tempfile| = clone.send(association_name) cloned_blob = ActiveStorage::Blob.create_and_upload!( io: tempfile, filename: original_blob.filename, content_type: original_blob.content_type ) .attach(cloned_blob) = (, original_blob) # Store the cloned attachment and blob into the deep_cloneable dictionary used # by the `deep_clone` method to ensure that, if needed during the cloning # operation, they won't be cloned once more and are accessible for processing store_cloned(, ) store_cloned(original_blob, cloned_blob) end end |
#clone_attachment_if_needed(name, attachment) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/para/cloneable/attachments_cloner.rb', line 37 def (name, ) return if .nil? store_key = store_key_for() # If the attachment has already been cloned, we don't need to clone it again return if dictionary[store_key]&.key?() (name, ) end |
#find_cloned_attachment(attachment_target, original_blob) ⇒ Object
Seemlessly handle one and many attachment relations return values and fetch the attachment that we just cloned by comparing blobs checksum, as depending which ActiveStorage version we’re on (Rails 5.2 or 6), the ‘#attach` method doesn’t always return the same, so for now we still handle the Rails 5.2 case.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/para/cloneable/attachments_cloner.rb', line 79 def (, original_blob) = if ..any? . else [.] end = .find do |att| att.blob.checksum == original_blob.checksum end end |
#store_cloned(source, clone) ⇒ Object
This stores the source and clone resources into the deep_clone dictionary, which simulates what the deep_cloneable gem does when it clones a resource
94 95 96 97 98 99 |
# File 'lib/para/cloneable/attachments_cloner.rb', line 94 def store_cloned(source, clone) store_key = store_key_for(source) dictionary[store_key] ||= {} dictionary[store_key][source] = clone end |
#store_key_for(attachment) ⇒ Object
101 102 103 |
# File 'lib/para/cloneable/attachments_cloner.rb', line 101 def store_key_for() .class.name.tableize.to_sym end |