0
votes

i have a question/problem - im not sure actually, maybe this is how it should behave(?)

I'm using Rails 4 with carrierwave to upload files, I just added the minimagick gem to create versions of the uploaded images.

when i upload image - it works great and make the version.
but when i load a page with images it seems to call the "version" method for every image that is displayed in the screen.... (and its not the upload process! only showing images!)
So even before i uploaded an image and actually created a version of it -> i've added a 'version' method to my uploader file -> refreshed a page in my site -> 'version' method was called (didnt upload anything yet)

my uploader file:

class PhotoUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick
  storage :fog

  version :nav_thumb, if: :is_avatar? do
    process :resize_to_fit => [50, 50]
  end

  version :box_thumb, if: :is_logo? do
    process :resize_to_fit => [40, 40]
  end

  def is_avatar?(picture)
    mounted_as == :avatar
  end

  def is_logo?(picture)
    mounted_as == :logo
  end
end

it seems that every image displayed in my page is getting to the is_avatar? method (i used pry debugger to see whats going on)

and -> it makes my page load slower, which is the opposite of what i wanted to achieve with smaller image versions.

is this normal behaviour? should it go to the version methods every time i load an image?
what should i do to fix it?

thanks.

2
I think amazon S3 service is the better way for upload image...ethan
im uploading the images to S3.... but it has nothing to do with my problemZiv Galili

2 Answers

0
votes

I think the issue results from stuffing the logic inside your version methods. Instead of writing if statements inside them, try changing them to these:

version :nav_thumb do
   process resize_to_fit: [50,50]
end

version :box_thumb do
   process resize_to_fit: [40,40]
end

and then write an additional method to determine which image version should be used (and then implement the result in your view with image_tag image.image_url(:version)).

0
votes

Try putting your is_*? methods in a protected block:

...
version :nav_thumb, if: :is_avatar? do
  process :resize_to_fit => [50, 50]
end

protected
  def is_avatar?(picture)
    mounted_as.eql?(:avatar)
  end