carrierwaveuploader/carrierwave
Rspec utilisant CarrierWave pour télécharger plusieurs images-Qiita
gem 'carrierwave', '~> 2.0'
Créer un uploader après l'installation du bundle
be rails g uploader Images
app / uploaders / images_uploader.rb est créé.
Contenu
class ImagesUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url(*args)
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png "].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png "].compact.join('_')
# end
# Process files as they are uploaded:
# process scale: [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process resize_to_fit: [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_whitelist
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg " if original_filename
# end
end
Ajout de la colonne d'images à la table Image. (Déjà lié à la table Post lors de la création de la table Image)
Lors du téléchargement de plusieurs fichiers, il semble être enregistré au format json, alors spécifiez-le.
be rails g migration add_images_to_image images:json
migrer le contenu
class AddImagesToImage < ActiveRecord::Migration[6.0]
def change
add_column :images, :images, :json
end
end
rails db: migrer pour ajouter des colonnes
models/image.rb
class Image < ApplicationRecord
belongs_to :post
validates :images, presence: true
mount_uploaders :images, ImagesUploader
end
spec/factories/images.rb
FactoryBot.define do
factory :image do
images { [ Rack::Test::UploadedFile.new(Rails.root.join('spec/fixtures/test.jpg'), 'spec/fixtures/test.jpg') ] }
post
end
end
Créez un répertoire fixtures sous le répertoire spec. J'ai créé test.jpg d'une manière pseudo.
spec/models/images.rb
require "rails_helper"
RSpec.describe Image, type: :model do
let(:pic_path) { Rails.root.join('spec/fixtures/test.jpg') }
let(:pic) { Rack::Test::UploadedFile.new(pic_path) }
context "Si une image est spécifiée" do
it "L'image est téléchargée" do
image = create(:image)
expect(image).to be_valid
end
it "image(Plusieurs)Mais téléchargé" do
image = create(:image, images: [pic, pic])
binding.pry
expect(image).to be_valid
end
end
context "Si aucune image n'est spécifiée" do
it "Obtenez une erreur" do
image = build(:image, images: nil)
expect(image).to be_invalid
expect(image.errors.messages[:images]).to eq ["can't be blank"]
end
end
end
Recommended Posts