module PaperCropper::ModelExtension::InstanceMethods

Instance methods #########################################################################

Public Instance Methods

attachment_changed?(attachment_name) click to toggle source
# File lib/paper_cropper/model_extension.rb, line 83
def attachment_changed?(attachment_name)
  ["#{attachment_name}_name", "#{attachment_name}_content_type",
    "#{attachment_name}_file_size", "#{attachment_name}_updated_at",
    "#{attachment_name}_crop_x", "#{attachment_name}_crop_y",
    "#{attachment_name}_crop_width", "#{attachment_name}_crop_height",
    "#{attachment_name}_crop_ratio"
  ].any? { |attr|
    method= "#{attr}_changed?".to_sym
    if respond_to?(method)
      self.send(method)
    end
  }
end
cropping?(attachment_name) click to toggle source

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

@return [Boolean]

# File lib/paper_cropper/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_width").blank? &&
    !self.send(:"#{attachment_name}_crop_height").blank?
end
method_missing(method, *args) click to toggle source

Uses method missing to responding the model callback for reprocess the image

Calls superclass method
# File lib/paper_cropper/model_extension.rb, line 98
def method_missing(method, *args)
  if method.to_s =~ PaperCropper::RegExp::CALLBACK
    reprocess_cropped_attachment(
      method.to_s.scan(PaperCropper::RegExp::CALLBACK).flatten.first.to_sym
    )
  else
    super
  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/paper_cropper/model_extension.rb, line 111
def reprocess_cropped_attachment(attachment_name)
  if cropping?(attachment_name)
    attachment_instance = send(attachment_name)
    attachment_instance.assign(attachment_instance)
    attachment_instance.save
  end
end