Module: CheckForDuplicateAttachedFile::ClassMethods

Defined in:
lib/paperclip_duplicate_check.rb

Overview

Methods added to the class.

Instance Method Summary collapse

Instance Method Details

#check_for_duplicate_attached_file(name, ...) ⇒ Object

Marks one or more attachments as performing duplicate checking.

Parameters:

  • name (Symbol)

    An attachment name.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/paperclip_duplicate_check.rb', line 21

def check_for_duplicate_attached_file(*names)
  names.each do |name|
    define_method :#{name}_with_dup_check=" do |file|
      attachment = send(name)
      old_fingerprint = attachment.fingerprint
      send :#{name}_without_dup_check=", file
      if attachment.fingerprint == old_fingerprint then
        # restore to saved state
        attachment.instance_variable_set :@queued_for_delete, []
        attachment.instance_variable_set :@queued_for_write, {}
        attachment.instance_variable_set :@errors, {}
        attachment.instance_variable_set :@dirty, false
      end
    end
    alias_method_chain :#{name}=", :dup_check
  end
end