module PaperCropper::ModelExtension::ClassMethods
Class methods ######################################################################### Initializes attachment cropping in your model
crop_attached_file :avatar
You can also define an initial aspect ratio for the crop and preview box through opts
crop_attached_file :avatar, :aspect => "4:3"
Or unlock it
crop_attached_file :avatar, :aspect => false
@param attachment_name [Symbol] Name of the desired attachment to crop @param opts [Hash] @option opts [Range, String, FalseClass] :aspect
Public Instance Methods
crop_attached_file(attachment_name, opts = {})
click to toggle source
# File lib/paper_cropper/model_extension.rb, line 21 def crop_attached_file(attachment_name, opts = {}) opts = opts.dup include PaperCropper::ModelExtension::InstanceMethods aspect = normalize_aspect opts[:aspect] send :define_method, :"#{attachment_name}_aspect" do aspect.first.to_f / aspect.last.to_f if aspect end if respond_to? :attachment_definitions # for Paperclip <= 3.4 definitions = attachment_definitions else # for Paperclip >= 3.5 definitions = Paperclip::Tasks::Attachments.instance.definitions_for(self) end processors = definitions[attachment_name][:processors] ||= [] unless processors.include? :paper_cropper processors << :paper_cropper end after_save :"reprocess_to_crop_#{attachment_name}_attachment" after_update { if send(:attachment_changed?, attachment_name) self.update_column(:"#{attachment_name}_updated_at", Time.now) end } end
normalize_aspect(aspect)
click to toggle source
Returns a valid and normalized value for aspect ratio It will return 1.. if aspect is nil or a invalid string @param aspect [Range, String, FalseClass]
@return [Range]
# File lib/paper_cropper/model_extension.rb, line 56 def normalize_aspect(aspect) if aspect.kind_of?(String) && aspect =~ PaperCropper::RegExp::ASPECT Range.new *aspect.split(':').map(&:to_i) elsif aspect.kind_of?(Range) return aspect.first.to_i..aspect.last.to_i elsif aspect == false return aspect else 1..1 end end