Module: Mongoid::PaperclipQueue

Defined in:
lib/mongoid_paperclip_queue.rb

Defined Under Namespace

Modules: InstanceMethods, Redis Classes: Queue

Instance Method Summary collapse

Instance Method Details

#has_queued_attached_file(field, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mongoid_paperclip_queue.rb', line 64

def has_queued_attached_file(field, options = {})


  # Include Paperclip and Paperclip::Glue for compatibility
  unless self.ancestors.include?(::Paperclip)
    include ::Paperclip
    include ::Paperclip::Glue
  end
  
  #send :include, InstanceMethods
  include InstanceMethods

  # Invoke Paperclip's #has_attached_file method and passes in the
  # arguments specified by the user that invoked Mongoid::Paperclip#has_mongoid_attached_file
  if options[:logger].nil? && Mongoid::Config.logger.present?
    options[:logger] = Mongoid::Config.logger
  end
  has_attached_file(field, options)
  
  # halt processing initially, but allow override for reprocess!
  self.send :"before_#{field}_post_process", :halt_processing
  
  define_method "#{field}_processing!" do 
    true
  end
  
  self.send :after_save do
    if self.changed.include? "#{field}_updated_at"
      # add a Redis key for the application to check if we're still processing
      # we don't need it for the processing, it's just a helpful tool
      Mongoid::PaperclipQueue::Redis.server.sadd(self.class.name, "#{field}:#{self.id.to_s}")

      # check if the document is embedded. if so, we need that to find it later
      if self.embedded?
        parents = []
        path = self
        associations = path.reflect_on_all_associations(:embedded_in)
        until associations.empty?
          # there should only be one :embedded_in per model, correct me if I'm wrong
          association = associations.first
          path = path.send(association.name.to_sym)
          parents << [association.class_name,association.name, path.id.to_s]
          associations = path.reflect_on_all_associations(:embedded_in)

        end
        # we need the relation name, not the class name
        args = [ self..name, field, self.id.to_s] + parents.reverse
      else 
        # or just use our default params like any other Paperclip model
        args = [self.class.name, field, self.id.to_s]
      end

      # then queue up our processing
      Mongoid::PaperclipQueue::Queue.enqueue(*args)
    end
  end
  
  ## 
  # Define the necessary collection fields in Mongoid for Paperclip
  field(:"#{field}_file_name", :type => String)
  field(:"#{field}_content_type", :type => String)
  field(:"#{field}_file_size", :type => Integer)
  field(:"#{field}_updated_at", :type => DateTime)
end