Created
October 9, 2009 16:19
-
-
Save amasses/206145 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GalleryImage < ActiveRecord::Base | |
belongs_to :property | |
has_attached_file :image, :styles => {:small => "300x300#", | |
:medium => "575x420>", | |
:small_thumb => "95x95#", | |
:thumb => "100x100#", | |
:large => "550x550>", | |
:featured => "296x477!"}, | |
:default_style => :small, | |
:processors => [:rotator] | |
attr_accessor :rotation_degrees, :rotate | |
before_create :set_defaults | |
def rotate!(degrees = 90) | |
self.rotation += degrees | |
self.rotation -= 360 if self.rotation >= 360 | |
self.rotation += 360 if self.rotation <= -360 | |
self.rotate = true | |
self.image.reprocess! | |
self.save | |
end | |
def rotating? | |
!self.rotation.nil? and self.rotate | |
end | |
private | |
def set_defaults | |
self.rotation ||= 0 | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GalleryImagesController < ApplicationController | |
def rotate | |
@image = GalleryImage.find(params[:id]) | |
rotation = params[:deg].to_f | |
rotation ||= 90 # Optional, otherwise, check for nil! | |
@image.rotate!(rotation) | |
flash[:notice] = "The image has been rotated" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Paperclip | |
class Rotator < Thumbnail | |
def transformation_command | |
if rotate_command | |
super + rotate_command | |
else | |
super | |
end | |
end | |
def rotate_command | |
target = @attachment.instance | |
if target.rotating? | |
" -rotate #{target.rotation}" | |
end | |
end | |
end | |
end |
Because rotation_degree
should be initialized to 0 with the set_default
callback.
You can update existing records with something like GalleryImage.update_all(:rotation_degree => 0)
is anyone with this lib yet? My image is not updating, yet it gives no error... And the image gets broken...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey it is not working can u help me!!!
I changed rotation into rotation_degrees now i got this error
NoMethodError (undefined method `+' for nil:NilClass):