image = File.open("spec/fixtures/image.jpg")
it "should upload image" do
uploader = ImageUploader.new(:store)
uploaded_file = uploader.upload(image)
expect(uploaded_file).to be_present
end
image = File.open("spec/fixtures/image.jpg")
it "should process image" do
uploader = ImageUploader.new(:store)
processed = uploader.process(image, phase: :store)
expect(processed[:box]).to ...
expect(processed[:card]).to ...
endrequire "sidekiq/testing"
it "should process in a background job" do
photo = Photo.create(image: image)
expect(photo.reload.image).not_to be_a(Hash) # not processed
PromoteJob.drain
expect(photo.reload.image).to be_a(Hash) # processed
endrequire 'image_processing/mini_magick'
class ImageUploader < Shrine include ImageProcessing::MiniMagick
plugin :determine_mime_type plugin :remove_attachment plugin :store_dimensions plugin :validation_helpers plugin :versions, names: [:original, :card, :box] plugin :pretty_location
Attacher.validate do validate_max_size 8.megabytes, message: 'is too large(max is 8 MB)' validate_mime_type_inclusion ['image/jpeg', 'image/jpg', 'image/png'] end
def process(io, context) case context[:phase] when :store card = resize_to_fill!(io.download, 300, 300, gravity: "Center") box = resize_to_fill!(io.download, 600, 600, gravity: "Center") { original: io, card: card, box: box } end endend
require 'rails_helper'
RSpec.describe ImageUploader, type: :model do image = File.open("spec/fixtures/image.jpg")
it "should process images and extract box size" do uploader = ImageUploader.new(:store) processed = uploader.process(image, phase: :store) expect(processed[:box]).to be_present end
it "should process images and extract card size" do uploader = ImageUploader.new(:store) processed = uploader.process(image, phase: :store) expect(processed[:card]).to exist endend 1) ImageUploader should process images and extract box size Failure/Error: card = resize_to_fill!(io.download, 300, 300, gravity: "Center") NoMethodError: undefined method `download' for #<File:spec/fixtures/cheero.jpg> # ./app/models/image_uploader.rb:21:in `process' # ./spec/models/image_uploader_spec.rb:8:in `block (2 levels) in <top (required)>'
2) ImageUploader should process images and extract card size Failure/Error: card = resize_to_fill!(io.download, 300, 300, gravity: "Center") NoMethodError: undefined method `download' for #<File:spec/fixtures/cheero.jpg> # ./app/models/image_uploader.rb:21:in `process' # ./spec/models/image_uploader_spec.rb:14:in `block (2 levels) in <top (required)>'
require 'rails_helper'
RSpec.describe ImageUploader, type: :model do
image = File.open("spec/fixtures/image.jpg")
cached = ImageUploader.new(:cache).upload(image)
it "should process images and extract box size" do
uploader = ImageUploader.new(:store)
processed = uploader.process(cached, phase: :store)
expect(processed[:box]).to be_present
end
it "should process images and extract card size" do
uploader = ImageUploader.new(:store)
processed = uploader.process(cached, phase: :store)
expect(processed[:card]).to exist
end
end--
You received this message because you are subscribed to a topic in the Google Groups "Shrine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruby-shrine/9upc1ADpy3M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ruby-shrine...@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/bb61c884-8a07-42ae-8a28-46468349e7a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.