Module: Mongoid::Paperclip::ClassMethods

Defined in:
lib/mongoid-paperclip.rb

Instance Method Summary collapse

Instance Method Details

#after_commit(*args, &block) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/mongoid-paperclip.rb', line 108

def after_commit(*args, &block)
  options = args.pop if args.last.is_a? Hash
  if options
    case options[:on]
    when :create
      after_create(*args, &block)
    when :update
      after_update(*args, &block)
    when :destroy
      after_destroy(*args, &block)
    else
      after_save(*args, &block)
    end
  else
    after_save(*args, &block)
  end
end

#has_mongoid_attached_file(field_name, options = {}) ⇒ Object

Adds Mongoid::Paperclip’s “#has_mongoid_attached_file” class method to the model which includes Paperclip and Paperclip::Glue in to the model. Additionally it’ll also add the required fields for Paperclip since MongoDB is schemaless and doesn’t have migrations.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/mongoid-paperclip.rb', line 130

def has_mongoid_attached_file(field_name, options = {})
  # Include Paperclip and Paperclip::Glue for compatibility
  unless self.ancestors.include?(::Paperclip)
    include ::Paperclip
    include ::Paperclip::Glue
  end
  
  # Invoke Paperclip's #has_attached_file method and passes in the
  # arguments specified by the user that invoked Mongoid::Paperclip#has_mongoid_attached_file
  has_attached_file(field_name, options)

  # Define the necessary collection fields in Mongoid for Paperclip
  field :"#{field_name}_file_name",    type: String
  field :"#{field_name}_content_type", type: String
  field :"#{field_name}_file_size",    type: Integer
  field :"#{field_name}_updated_at",   type: Time
  field :"#{field_name}_fingerprint",  type: String
  
  # convenience attr (RailsAdmin uses this naming)
  attr_accessor :"delete_#{field_name}"
  before_validation do
     self.send(field_name).clear if self.send(:"delete_#{field_name}").present? && self.send(:"delete_#{field_name}") && self.send(:"delete_#{field_name}") != '0'
  end
end