module Papercrop::ModelExtension::InstanceMethods

Public Instance Methods

cropping?(attachment_name) click to toggle source

Asks if the attachment received a crop process @param attachment_name [Symbol]

@return [Boolean]

# File lib/papercrop/model_extension.rb, line 76
def cropping?(attachment_name)
  !self.send(:"#{attachment_name}_crop_x").blank? &&
  !self.send(:"#{attachment_name}_crop_y").blank? &&
  !self.send(:"#{attachment_name}_crop_w").blank? &&
  !self.send(:"#{attachment_name}_crop_h").blank?
end
image_geometry(attachment_name, style = :original) click to toggle source

Returns a Paperclip::Geometry object of a given attachment

@param attachment_name [Symbol] @param style = :original [Symbol] attachment style @return [Paperclip::Geometry]

# File lib/papercrop/model_extension.rb, line 89
def image_geometry(attachment_name, style = :original)
  @geometry                  ||= {}
  @geometry[attachment_name] ||= {}
  
  path = (self.send(attachment_name).options[:storage] == :filesystem) ? self.send(attachment_name).path(style) : self.send(attachment_name).url(style)
  
  @geometry[attachment_name][style] ||= Paperclip::Geometry.from_file(path)
end
method_missing(method, *args) click to toggle source

Uses method missing to responding the model callback

Calls superclass method
# File lib/papercrop/model_extension.rb, line 100
def method_missing(method, *args)
  if method.to_s =~ Papercrop::RegExp::CALLBACK
    reprocess_cropped_attachment(
      method.to_s.scan(Papercrop::RegExp::CALLBACK).flatten.first.to_sym
    )
  else
    super
  end
end
reset_crop_attributes_of(attachment_name) click to toggle source

Sets all cropping attributes to nil @param attachment_name [Symbol]

# File lib/papercrop/model_extension.rb, line 113
def reset_crop_attributes_of(attachment_name)
  [:crop_x, :crop_y, :crop_w, :crop_h].each do |a|
    self.send :"#{attachment_name}_#{a}=", nil
  end
end

Private Instance Methods

reprocess_cropped_attachment(attachment_name) click to toggle source

Saves the attachment if the crop attributes are present @param attachment_name [Symbol]

# File lib/papercrop/model_extension.rb, line 123
def reprocess_cropped_attachment(attachment_name)
  if cropping?(attachment_name)
    attachment_instance = send(attachment_name)
    attachment_instance.assign(attachment_instance)
    attachment_instance.save

    reset_crop_attributes_of(attachment_name)
  end
end