Module: Ksk::PaperclipCrop

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/ksk/paperclip_crop.rb

Instance Method Summary collapse

Instance Method Details

#cords_set?(image_name, style) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/models/concerns/ksk/paperclip_crop.rb', line 32

def cords_set?(image_name, style)
  !send("#{image_name}_#{style}_x").blank? && !send("#{image_name}_#{style}_y").blank? && !send("#{image_name}_#{style}_w").blank? && !send("#{image_name}_#{style}_h").blank?
end

#crop_thumbsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/concerns/ksk/paperclip_crop.rb', line 36

def crop_thumbs
  ksk_images_for_crop.each_pair do |image_name, style_names|
    image = send(image_name)
    image.styles.each_pair do |style, meta|
      if cords_set?(image_name, style)
        resize_banner style, [send("#{image_name}_#{style}_x"), send("#{image_name}_#{style}_y"), send("#{image_name}_#{style}_w"), send("#{image_name}_#{style}_h")], meta.attachment.options[:styles][style][0]
        send("#{image_name}_#{style}_x=", false)
      end
    end
    image.save
    image.instance.save
  end
end

#cropping?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/concerns/ksk/paperclip_crop.rb', line 20

def cropping?
  needs_crop = false
  ksk_images_for_crop.each_pair do |image_name, style_names|
    send(image_name).styles.each_pair do |style, meta|
      unless needs_crop
        needs_crop = cords_set?(image_name, style)
      end
    end
  end
  needs_crop
end

#resize_attr_accessorsObject



9
10
11
12
13
14
15
16
17
18
# File 'app/models/concerns/ksk/paperclip_crop.rb', line 9

def resize_attr_accessors
  ksk_images_for_crop.each_pair do |image_name, style_names|
    send(image_name).styles.each_pair do |style, meta|
      self.class.send(:attr_accessor, "#{image_name}_#{style}_x")
      self.class.send(:attr_accessor, "#{image_name}_#{style}_y")
      self.class.send(:attr_accessor, "#{image_name}_#{style}_w")
      self.class.send(:attr_accessor, "#{image_name}_#{style}_h")
    end
  end
end

#resize_banner(name, cords, resize) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'app/models/concerns/ksk/paperclip_crop.rb', line 50

def resize_banner(name, cords, resize)
  ksk_images_for_crop.each_pair do |image_name, style_names|
    image = send(image_name)
    image.queued_for_write[name] = Paperclip.processor(:ksk_crop).make(image, cords, image)
    style = Paperclip::Style.new(name, [resize, :jpg], image)
    image.queued_for_write[name] = Paperclip.processor(:thumbnail).make(image.queued_for_write[name], style.processor_options, image.queued_for_write[name])
    image.queued_for_write[name] = Paperclip.io_adapters.for(image.queued_for_write[name])
  end
end