Module: JCropper::ClassMethods

Included in:
ActiveRecord::Base
Defined in:
lib/jcropper/jcropper.rb

Instance Method Summary collapse

Instance Method Details

#jcrop(attachment, style) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
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
46
47
48
49
50
# File 'lib/jcropper/jcropper.rb', line 3

def jcrop(attachment, style)
  raise "jcropper requires attachment to be of type Paperclip::Attachment" if self.attachment_definitions[attachment.to_sym].nil?
  
  class_exec(attachment, style) do |attachment, style|
    attr_reader :cropped_image
    write_inheritable_hash :jcropper_defs, {:attachment => attachment, :style => style} 
    class_inheritable_reader :jcropper_defs

    attr_accessor :jcropper_should_reprocess
    before_save :jcropper_normalize_crop, :jcropper_check_for_reprocess
    after_save :jcropper_reprocess
    
    ###CRZ - alias chain this
    def after_initialize
      @cropped_image = CroppedImage.new(self)
    end
    
    def jcropper_reprocess
      cropped_image.attachment.reprocess! if @jcropper_should_reprocess
    end
  end

  x, y, w, h = [:x, :y, :w, :h].map{|coord| JCropper.jattr(attachment, style, coord) }
  to_eval = <<-TO_EVAL
    def jcropper_check_for_reprocess
      @jcropper_should_reprocess ||= !(changed & %w(#{x} #{y} #{w} #{h})).empty?
      true
    end

    def jcropper_coords
      [#{x}, #{y}, #{w}, #{h}]
    end
    
    def jcropper_needs_crop?
      cropped_image and cropped_image.original_geometry and (#{w} == 0 or #{h} == 0)
    end

    def jcropper_normalize_crop
      self.#{x}, self.#{y}, self.#{w}, self.#{h} = *cropped_image.max_crop if jcropper_needs_crop?
      true
    end

    def jcropper_crop_string
      "-crop \#{#{w}}x\#{#{h}}+\#{#{x}}+\#{#{y}}"
    end
  TO_EVAL
  class_eval to_eval
end