如何在社区引擎中禁用附件 Fu 插件而不删除它?
所以情况是这样的:
Community Engine 中的照片模型正在使用附件 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
So here is the situation:
The Photo model in Community Engine is using attachment Fu. I, wish to use paperclip instead.
Now this works fine until I have to delete an attachment. That's when the attachment Fu is causing the problem. Here is what Photo.rb looks like (in /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
So my question is: Is there a way to disable this plugin? I don't want to change photo.rb and delete any lines, nor do I want to remove the plugin.
Any ideas here?
New Photo Model (in /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
New Photo Controller (in /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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您将插件安装到
vendor/
目录,则在plugins/
子目录下找到该插件,并注释掉init.rb
中的所有内容。这应该禁用该插件的所有功能,而无需将其从源代码树中删除。If you installed the plugin to your
vendor/
directory, then find the plugin under theplugins/
subdirectory and comment out everything in theinit.rb
. This should disable all functionality of the plugin without removing it from your source tree.当您没有为其初始化任何内容时,您究竟如何使用回形针?
您可以在初始化程序中打开该类并更改它以满足您的需求。
使用从配置文件设置的适配器模式添加附件代码可能会更好,这样您就可以创建一个模块并包含它。该模块根据您创建的某些配置以及从何处获取要初始化的参数来确定是否包含回形针或附件 fu。
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.