我想将文件上传限制为仅限图片,并自动将其转换为.png
。为此,我使用这个类:
class ImageAttachment < ActiveRecord::Base
attr_accessible :file, :file_file_name, :file_content_type, :file_file_size
validates_attachment :file,
:content_type => { :content_type => ["image/jpg", "image/tiff", "image/png"] }
has_attached_file :file,
:styles => { :original => ["100%", :png],
:large => ["500x500", :png],
:medium => ["150x150", :png],
:thumb => ["75x100", :png]
},
:default_url => "/system/missing_thumb.png"
end
据我了解,:styles => { :original => ["100%", :png], ...}
应将所有通过验证的上传文件转换为.png
个文件。因此,我希望在上传文件example.tiff
时发生以下情况:
.png
example.png
"image/png"
这是我使用的规范:
it "should convert all image types to .png" do
test_file = File.new(Rails.root + "spec/fixtures/images/test.tiff")
attachment = ImageAttachment.create :file => test_file
attachment.file.url.should == "some/paperclip/path/.../test.png"
attachment.file_file_name.should == "test.png"
attachment.file_content_type.should == "image/png"
end
第一个断言是真的,我也可以在终端看到ImageMagick输出,
但attachment.file_file_name
仍会返回example.tiff
,attachment.file_content_type
会返回"image/tiff"
。
我的假设是,回形针会自动更新file_file_name
和file_content_type
属性错误吗?
如果是这样,我最好如何自己做这件事?