Different prefix per uploader

227 views
Skip to first unread message

josuea...@gmail.com

unread,
Mar 19, 2017, 1:32:24 AM3/19/17
to Shrine
Hi, 

Im now using this inside each uploader

plugin :default_storage, store: :listing_store

And inside the initializer 

listing_store: Shrine::Storage::S3.new(prefix: "listings", **s3_options)

This is just because I want different prefix per uploader, I have also a photo_store with the prefix "photos", etc.

I tried to pass the prefix: option with the upload_options plugin, but I got an error saying thats an invalid argument.

Im right using different storages just for change the prefix or is a better way to do this? 

Thanks!  

Janko Marohnić

unread,
Mar 19, 2017, 3:16:04 AM3/19/17
to josuea...@gmail.com, Shrine
Well, an alternative is to modify `#generate_location` for each uploader to add the desired prefix.

class ListingUploader < Shrine
  def generate_location(io, context)
    "listings/#{super}"
  end
end

The only difference is that this way the prefix will be included in the attachment id, while with the :prefix option to the storage this is not the case.

Both are fine, but if you are creating a new storage for every uploader, it's probably simpler and makes more sense to go with the strategy of overriding `#generate_location`.

Kind regards,
Janko

--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine+unsubscribe@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/398dc88b-d72b-4af7-8093-c8dd63e49bc3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Josue A. de Lima

unread,
Mar 19, 2017, 4:11:11 AM3/19/17
to Shrine
Thanks for the answer, as you said overriding '#generate_location' makes more sense, create new storages only makes sense if Im going to use different storages like filesystem or s3, but not just to change the prefix.

I have another question, lets supuse I have two models (M1, M2) with has_many :photos, also Photo belogns_to: M1 and M2, Photo has an image so the uploader is ImageUploader, ideally is good to reuse same model and also the ImageUploader when I try to attach a photo to each model, but how if each model need different thumbnails size? How can I pass arguments to the uploader? making a @variable like this and on each model and read that on the uploader or there is better way?

Thanks!


On Sunday, March 19, 2017 at 1:16:04 AM UTC-6, Janko Marohnić wrote:
Well, an alternative is to modify `#generate_location` for each uploader to add the desired prefix.

class ListingUploader < Shrine
  def generate_location(io, context)
    "listings/#{super}"
  end
end

The only difference is that this way the prefix will be included in the attachment id, while with the :prefix option to the storage this is not the case.

Both are fine, but if you are creating a new storage for every uploader, it's probably simpler and makes more sense to go with the strategy of overriding `#generate_location`.

Kind regards,
Janko

On Sun, Mar 19, 2017 at 4:32 PM, <josuea...@gmail.com> wrote:
Hi, 

Im now using this inside each uploader

plugin :default_storage, store: :listing_store

And inside the initializer 

listing_store: Shrine::Storage::S3.new(prefix: "listings", **s3_options)

This is just because I want different prefix per uploader, I have also a photo_store with the prefix "photos", etc.

I tried to pass the prefix: option with the upload_options plugin, but I got an error saying thats an invalid argument.

Im right using different storages just for change the prefix or is a better way to do this? 

Thanks!  

--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.

Janko Marohnić

unread,
Mar 19, 2017, 4:49:22 AM3/19/17
to Josue A. de Lima, Shrine
When you're generating thumbnails, you have access to the model instance to which you're attaching the file, so you can decide on the thumbnail dimensions dynamically while processing:

class MyUploader < Shrine

  plugin :processing

  plugin :versions


  process(:store) do |io, context|

    original = io.download


    thumbnail_dimensions =

      case context[:record]

      when Listing then [300, 300] 

      when Photo  then [200, 200]

      end


    thumbnail = resize_to_limit(original, *thumbnail_dimensions)


    {original: original, thumbnail: thumbnail}

  end

end


Kind regards,
Janko

To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine+unsubscribe@googlegroups.com.

To post to this group, send email to ruby-...@googlegroups.com.

Josue A. de Lima

unread,
Mar 19, 2017, 6:28:42 PM3/19/17
to Shrine
Great! Thanks!, also thanks, for making so versatie upload gem.
Reply all
Reply to author
Forward
0 new messages