2012-09-20 3 views
1

저는 이미지를 저장하는 새 프로젝트에서 Paperclip gem을 사용하고 있습니다. 또한 내 모델의 가상 속성에 저장된 자르기 매개 변수에 따라 이미지를 자르기 위해 작성한 간단한 사용자 정의 Paperclip 프로세서가 있습니다.가상 속성에 값이 오기 전에 Paperclip의 후 처리가 시작됩니까?

그래서 클라이언트는 모델의 실제 db 필드와 crop_x, crop_y, crop_w, crop_h 속성을 사용하여 양식을 전송합니다. 컨트롤러는 "params"를 기반으로 새 인스턴스를 만듭니다. 잘 살펴 보았지만 확인했는데 이상한 것들이 있습니다. 일어나고.

디버거 내 사용자 지정 프로세서에 배치

class Jewel < ActiveRecord::Base 
has_many :line_items 
belongs_to :material 

******* some code ********* 

before_save :debugmeBS 

validates :category_id, :presence => true 

attr_accessor :crop_x, :crop_y, :crop_w, :crop_h 
attr_accessible :name, :category_id, :price, :crop_x, :crop_y, 
:crop_w, :crop_h, :image 

after_update :reprocess_image, :if => :need_to_crop? 

has_attached_file :image, :styles => { :full => "900x", :smallthumb => "80x80>" }, 
:processors => [:cropper, :thumbnail] 

before_post_process :debugmePP 

def need_to_crop? 
    # debugger 
    !crop_x.blank? && !crop_y.blank? &&!crop_w.blank? &&!crop_h.blank? 
end 

private 
    def debugmeBS 
     debugger 
     x=2 

    end 

    def debugmePP 
     debugger 
     x=3 

    end 
end 
이 crop_x, crop_y 등 가상 속성이 비어있는 것을 보여줍니다 (호출하는 방법 "need_to_crop을?") 한편 다른 가상없는 사람이 제대로 설정 : 여기

내 모델의 . 추적 오류에 나는 before_save와 Paperclip의 before_post_process라는 두 개의 "before _"이벤트를 배치했습니다. before_post_process가 before_save 전에 호출되고, 모든 "crop_"속성은 nil이지만, before_save가 호출되는 순간에는 올바르게 설정됩니다.
그래서 질문은 왜입니까? Paperclip 프로세서가 액세스 할 가상 속성을 설정하는 방법은 무엇입니까? 고마워요

답변

3

나는 똑같은 것을 쳤다. Paperclip (lib/paperclip/아래의 callbacks.rb, attachment.rb 및 paperclip.rb 참조) 코드를 보면 파일이 속성에 할당 될 때 before_post_process 및 before__post_process 콜백이 호출 된 것처럼 보입니다. 내 생각 엔이 모델의 다른 특성이 설정되기 전에이 작업이 수행되고있는 것입니다.

업데이트 : 나는 Paperclip 프로젝트의 GitHub Issues에 대해 물었고, 위의 내용을 확인했다. 처리 시간은 속성이 할당 된시기에 따라 다릅니다. 속성은의 이름에 따라 사전 순으로 지정되므로 다른 속성보다 먼저 지정할 수 있습니다.

해결을 위해

, 당신은 할 수 있습니다 :

def create 
    avatar = params[:resource].delete(:avatar) 
    resource = Resource.new(params[:resource] 
    resource.avatar = avatar 
    resource save 
end 

소스 : https://github.com/thoughtbot/paperclip/issues/1279

+0

해킹,하지만 좋은 해킹 :) –