Module: PaperclipArchiveProcessor::ClassMethods

Defined in:
lib/paperclip_archive_processor/integration.rb

Class Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/paperclip_archive_processor/integration.rb', line 11

def self.extended(base)
  base.module_eval do

    private

    def create_callback(name, &block)
      class_eval do
        define_method(name, &block)
      end
    end

    def define_before_save_callback(variable_name, attachment_name)
      name = "before_save_filter_for_#{attachment_name}"
      create_callback(name) do
        instance_variable_set(variable_name, try(attachment_name).dirty?)
        true
      end
      send(:before_save, name)
    end

    def define_after_save_callback(variable_name, attachment_name)
      name = "after_save_filter_for_#{attachment_name}"
      create_callback(name) do
        if instance_variable_get(variable_name)
          PaperclipArchiveProcessor::Processor.extract(try(attachment_name))
          true
        end
      end
      send(:after_save, name)
    end

    def define_processor_callbacks(attachment_name)
      variable_name = "@_#{attachment_name}_has_changed"
      define_before_save_callback(variable_name, attachment_name)
      define_after_save_callback(variable_name, attachment_name)
    end

    public

    alias has_attached_file_original has_attached_file

    def has_attached_file(name, options = {})
      has_attached_file_original(name, options)

      if options.delete(:extract_archive)
        define_processor_callbacks(name)
      end
    end
  end

end