Module: Papercrop::ModelExtension::ClassMethods
- Defined in:
- lib/papercrop/model_extension.rb
Instance Method Summary collapse
-
#crop_attached_file(attachment_name, opts = {}) ⇒ Object
Initializes attachment cropping in your model.
Instance Method Details
#crop_attached_file(attachment_name, opts = {}) ⇒ Object
Initializes attachment cropping in your model
crop_attached_file :avatar
You can also define an aspect ratio for the crop and preview box through opts
crop_attached_file :avatar, :aspect => "4:3"
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/papercrop/model_extension.rb', line 16 def crop_attached_file(, opts = {}) [:crop_x, :crop_y, :crop_w, :crop_h, :original_w, :original_h, :box_w, :aspect, :cropped_geometries].each do |a| attr_accessor :"#{}_#{a}" end if opts[:aspect].kind_of?(String) && opts[:aspect] =~ Papercrop::RegExp::ASPECT opts[:aspect] = Range.new *opts[:aspect].split(':').map(&:to_i) end unless opts[:aspect].kind_of?(Range) opts[:aspect] = 1..1 end send :define_method, :"#{}_aspect" do opts[:aspect].first.to_f / opts[:aspect].last.to_f end if respond_to? :attachment_definitions # for Paperclip <= 3.4 definitions = else # for Paperclip >= 3.5 definitions = Paperclip::Tasks::Attachments.instance.definitions_for(self) end definitions[][:processors] ||= [] definitions[][:processors] << :cropper after_update :"reprocess_to_crop_#{}_attachment" end |