1

所以情况如下:

社区引擎中的照片模型正在使用附件 Fu。我想用回形针代替。

现在这工作正常,直到我必须删除附件。这就是附件 Fu 引起问题的时候。这是 Photo.rb 的样子(在 /vendor/plugins/community_engine/app/models 中):

class Photo < ActiveRecord::Base


  acts_as_commentable
  belongs_to :album

  has_attachment prepare_options_for_attachment_fu(AppConfig.photo['attachment_fu_options'])

  acts_as_taggable

  acts_as_activity :user, :if => Proc.new{|record| record.parent.nil? && record.album_id.nil?}

  validates_presence_of :size
  validates_presence_of :content_type
  validates_presence_of :filename
  validates_presence_of :user, :if => Proc.new{|record| record.parent.nil? }
  validates_inclusion_of :content_type, :in => attachment_options[:content_type], :message => "is not allowed", :allow_nil => true
  validates_inclusion_of :size, :in => attachment_options[:size], :message => " is too large", :allow_nil => true

...
...

end

所以我的问题是:有没有办法禁用这个插件?我不想更改 photo.rb 并删除任何行,也不想删除插件。

这里有什么想法吗?

新照片模型(在 /app/ 中):

require 'paperclip_processors/cropper'

class Photo < ActiveRecord::Base   

  attr_accessible :image
  has_attached_file :image,      
                    :path=>":class/:hash/:style.:extension",
                    :styles => {
                      :thumb => {:geometry => "100x100!", :crop_to => :crop_parameters},
                      :medium => {:geometry => "290x320!", :crop_to => :crop_parameters},
                      :large => {:geometry => "664>", :crop_to => :crop_parameters},
                      :uncropped => "630x472"
                    },
                    :convert_options=>'-quality 92',
                    :processors => [:cropper]

  def crop_parameters
    ActiveSupport::JSON.decode(read_attribute(:crop_parameters)) rescue nil
  end


# overrides to make paperclip appear as attachment_fu to existing pages          

  def size # in MB
    image_file_size
  end
  def filename
    image_file_name
  end
  def content_type
    image_content_type
  end


  def public_filename(size=:original)
    image.url(size) || ""
  end

end

新照片控制器(在 /app/ 中):

require 'pp'

class PhotosController < BaseController
  before_filter :use_paperclip, :only => [:create]

  def use_paperclip
    params[:photo][:image] = params[:photo][:uploaded_data]
    params[:photo].delete(:uploaded_data)     
  end

end
4

2 回答 2

0

How exactly are you using paperclip, when you haven't initialized anything for it?

You could maybe open up the class in an initializer and change it to meet your needs.

Might be better to add the attachment code using an adapter pattern which is set up from a config file, so you can create a module and include it. This module works out whether to include paperclip or attachement fu depending on some config you create, and where to get the parameters to initialize.

于 2011-10-24T15:42:05.327 回答
0

如果您将插件安装到您的vendor/目录,则在plugins/子目录下找到插件并注释掉init.rb. 这应该禁用插件的所有功能,而不会将其从源代码树中删除。

于 2011-10-24T15:35:45.550 回答