ruby on rails - In Carrierwave how to create a version only when needed? Not when uploading -
ruby on rails - In Carrierwave how to create a version only when needed? Not when uploading -
i have setup uploader upload bunch of photos album. allow user select 4 images album cover. after select it, need generate smaller version of photos. selected 4. how can this?
basically need way generate image versions after main upload. can utilize refile. problem is, need create versions while uploading!
building off @karlingen's answer:
i'm going assume there 'selected' boolean value on table image records.
# picture.rb belongs_to :my_model scope :chosen, -> { where(selected: true) } set version want display user select in carrierwave uploader, along versions want after album selected
include carrierwave::minimagick # original version version :thumb process resize_to_fill: [50, 50] end # processed later versions :large_album, if: :is_selected? process resize_to_fill: [500, 500] end versions :medium_album, if: :is_selected? process resize_to_fill: [400, 400] end versions :small_album, if: :is_selected? process resize_to_fill: [300, 300] end private # check if selected def is_selected?(picture) picture.uploader.model.selected end conditional versions
occasionally want restrict creation of versions on properties within model or based on image itself.
then when user selects album photos want utilize can phone call manual create versions phone call in controller on selected albums:
my_model.pictures.chosen.each |picture| picture.image.recreate_versions!(:large_album, :medium_album, :small_album) end recreating versions
you might come situation want retroactively alter version or add together new one. can utilize recreate_versions! method recreate versions base of operations file. uses naive approach re-upload , process specified version or versions, if none passed argument.
when generating random unique filenames have phone call save! on model after using recreate_versions!. necessary because recreate_versions! doesn't save new filename database. calling save! prevent database , file scheme running out of sync..... or on mounted uploader
you might consider tying after_update function.
ruby-on-rails carrierwave
Comments
Post a Comment