Created
June 17, 2012 19:52
-
-
Save ralphos/2945549 to your computer and use it in GitHub Desktop.
Cropping question
This file contains 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
################ tattoo.rb ################# | |
class Tattoo < ActiveRecord::Base | |
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h | |
attr_accessible :name, :image, :user_id | |
has_many :comments | |
belongs_to :user | |
has_attached_file :image, styles: { :thumb => "200x150>", :normal => "600x450>", :large => "600x600>" }, :processors => [:cropper] | |
after_update :reprocess_image, if: :cropping? | |
def self.recent | |
order('created_at desc') | |
end | |
def self.search(keywords) | |
tattoos = recent | |
tattoos = tattoos.where("name like ?", "%#{keywords}%") if keywords.present? | |
tattoos | |
end | |
def cropping? | |
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank? | |
end | |
def image_geometry(style = :original) | |
@geometry ||= {} | |
@geometry[style] ||= Paperclip::Geometry.from_file(image.path(style)) | |
end | |
private | |
def reprocess_image | |
image.reprocess! | |
end | |
end | |
############ tattoos_controller.rb ############### | |
class TattoosController < ApplicationController | |
def index | |
@tattoos = Tattoo.search(params[:search]) | |
end | |
def new | |
@tattoo = Tattoo.new | |
end | |
def create | |
@tattoo = Tattoo.new(params[:tattoo]) | |
if @tattoo.save | |
current_user.tattoos << @tattoo | |
render :crop | |
else | |
flash.now.alert = 'Sorry there was an error uploading your Tattu' | |
render 'new' | |
end | |
end | |
def show | |
@tattoo = Tattoo.find(params[:id]) | |
@comment = Comment.new | |
@comments = @tattoo.comments | |
end | |
def update | |
@tattoo = Tattoo.find(params[:id]) | |
if @tattoo.update_attributes(params[:tattoo]) | |
if params[:tattoo][:image].present? | |
render :crop | |
else | |
redirect_to @tattoo, notice: 'Successfully updated Tattu' | |
end | |
else | |
render 'new' | |
end | |
end | |
end | |
############## crop.html.erb ############### | |
<h2>Crop Tattoo</h2> | |
<%= image_tag @tattoo.image.url(:large), id: "cropbox" %> | |
<%= form_for @tattoo do |f| %> | |
<% %w[x y w h].each do |attribute| %> | |
<%= f.hidden_field "crop_#{attribute}" %><br> | |
<% end %> | |
<div class="actions"> | |
<%= f.submit "Crop Image" %> | |
</div> | |
<% end %> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment