Evm*_*rov 12 ruby-on-rails carrierwave
我有类似RailsCasts中描述的模型:
应用程序/模型/ resident.rb:
class Resident < ActiveRecord::Base
include PhotoConcern
end
Run Code Online (Sandbox Code Playgroud)
应用程序/模型/ employee.rb:
class Employee < ActiveRecord::Base
include PhotoConcern
end
Run Code Online (Sandbox Code Playgroud)
应用程序/模型/忧虑/ photo_concern.rb:
module PhotoConcern
extend ActiveSupport::Concern
included do
mount_uploader :photo, PhotoUploader
attr_accessor :photo_crop_x, :photo_crop_y, :photo_crop_w, :photo_crop_h
after_save :crop_photo
def crop_photo
photo.recreate_versions! if photo_crop_x.present?
end
end
end
Run Code Online (Sandbox Code Playgroud)
应用程序/上传/ photo_uploader.rb:
class PhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :cropped do
process :crop
end
version :thumb, from_version: :cropped do
process resize_to_fill: [100, 100]
end
version :avatar, from_version: :cropped do
process resize_to_fill: [200, 200]
end
def crop
return if model.photo_crop_x.blank?
resize_to_limit(500, nil)
resize_to_fit(500, nil)
manipulate! do |img|
size = model.photo_crop_w << 'x' << model.photo_crop_h
offset = '+' << model.photo_crop_x << '+' << model.photo_crop_y
img.crop("#{size}#{offset}")
img
end
end
end
Run Code Online (Sandbox Code Playgroud)
应用程序/视图/员工/ show.slim
= image_tag (@employee.photo.present? ? @employee.photo.url(:avatar) : "client_#{@employee.sex}.png"), class: 'img-circle img-responsive'
Run Code Online (Sandbox Code Playgroud)
我想在裁剪后重命名版本文件,这样我的用户就不会为缓存而烦恼.它在CarrierWave wiki中描述了如何重命名文件,但也写了 "为了保存新生成的文件名,你必须在recreate_versions之后调用模型上的save!".
如何重命名版本文件?我无法再次调用save!
我的员工after_save
,因为有更多的钩子不应该被调用两次.此外,PhotoConcern包含在另一个类中.
相关维基文章:
为了保存新生成的文件名,您必须调用save!在recreate_versions之后的模型!
所以我相信你怀疑的答案包含在Carrierwave rubydocumentation中
recreate_versions!(*versions) ? Object
重新创建版本并重新处理它们.如果它们的参数以某种方式发生了变化,这可用于重新创建版本.
*versions
如果不省略它们,则此方法将存储,否则cached file
将存储.
# File 'lib/carrierwave/uploader/versions.rb', line 216
def recreate_versions!(*versions)
# Some files could possibly not be stored on the local disk. This
# doesn't play nicely with processing. Make sure that we're only
# processing a cached file
#
# The call to store! will trigger the necessary callbacks to both
# process this version and all sub-versions
if versions.any?
file = sanitized_file if !cached?
# the file will be stored
store_versions!(file, versions)
else
cache! if !cached?
# If new_file is omitted, a previously cached file will be stored.
store!
end
Run Code Online (Sandbox Code Playgroud)
怎么store!
办?
store!(new_file = nil) ? Object
通过将文件传递给此Uploader的存储引擎来存储文件.如果省略new_file,则将存储先前缓存的文件
此方法包含在您的中class PhotoUploader < CarrierWave::Uploader::Base
,它用于with_callbacks
使用回调存储您的文件:store
.回调会触发此方法.
# File 'lib/carrierwave/uploader/store.rb', line 53
def store!(new_file=nil)
cache!(new_file) if new_file && ((@cache_id != parent_cache_id) || @cache_id.nil?)
if !cache_only and @file and @cache_id
with_callbacks(:store, new_file) do
new_file = storage.store!(@file)
if delete_tmp_file_after_storage
@file.delete unless move_to_store
cache_storage.delete_dir!(cache_path(nil))
end
@file = new_file
@cache_id = nil
end
end
end
Run Code Online (Sandbox Code Playgroud)
是什么store_versions!
方法呢?
def store_versions!(new_file, versions=nil)
if versions
active = Hash[active_versions]
versions.each { |v| active[v].try(:store!, new_file) } unless active.empty?
else
active_versions.each { |name, v| v.store!(new_file) }
end
end
Run Code Online (Sandbox Code Playgroud)
什么是Carrierwave回调以及如何使用它们?
after :store, :store_versions!
Run Code Online (Sandbox Code Playgroud)
关于SO的这个问题解释了并且wiki解释了回调如何工作,通过after :store, :my_method
在version :low do
块内部执行,您将my_method
仅在after :store
回调时执行(仅适用于该版本).
该:store
回调对应的执行store!
.
什么@filename
属性?如何recreate_versions!
编码文件名?
@filename
与方法定义filename
在lib/carrierwave/uploader/store.rb
##
# Override this in your Uploader to change the filename.
#
# Be careful using record ids as filenames. If the filename is stored in the database
# the record id will be nil when the filename is set. Don't use record ids unless you
# understand this limitation.
#
# Do not use the version_name in the filename, as it will prevent versions from being
# loaded correctly.
#
# === Returns
#
# [String] a filename
#
def filename
@filename
end
Run Code Online (Sandbox Code Playgroud)
来自carrierwave的指南建议在def filename
重新创建版本时使用重新创建唯一的文件名recreate_version!
.
此方法不保存到数据库,保存到数据库,您需要save!
在appropriata Carrierwave
回调上调用,而不会破坏您的Carrierwave
GEM
我没有这个问题的解决方案,但没有相关的文档,我们应该开始构建它.
归档时间: |
|
查看次数: |
690 次 |
最近记录: |