Module: Papercrop::ModelExtension::InstanceMethods

Defined in:
lib/papercrop/model_extension.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Uses method missing to responding the model callback



100
101
102
103
104
105
106
107
108
# 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

Instance Method Details

#cropping?(attachment_name) ⇒ Boolean

Asks if the attachment received a crop process

Parameters:

  • attachment_name (Symbol)

Returns:

  • (Boolean)


76
77
78
79
80
81
# 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) ⇒ Paperclip::Geometry

Returns a Paperclip::Geometry object of a given attachment

Parameters:

  • attachment_name (Symbol)
  • style (defaults to: :original)

    :original [Symbol] attachment style

Returns:

  • (Paperclip::Geometry)


89
90
91
92
93
94
95
96
# 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

#reset_crop_attributes_of(attachment_name) ⇒ Object

Sets all cropping attributes to nil

Parameters:

  • attachment_name (Symbol)


113
114
115
116
117
# 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