Shrine

Sorbet-friendly attachment declarations

Problem

Users adding Sorbet to their codebase are immediately greeted with type checker warnings:

app/models/editorial.rb:5: include must only contain constant literals https://srb.help/4002
     5 |  include ImageUploader::Attachment.new(:headline_image)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is because Sorbet doesn’t support dynamic includes like this (I can’t include links as a new user but look at the link in the code snippet above or Adopting Sorbet section within their docs page).

Proposal

Add an alternative way to add attachments to a model for Sorbet users.

Something like:

class Photo < Sequel::Model
  include Shrine::Attachment

  # Proposed new method
  attachment_name :image
end

From a cursory glance at the codebase I didn’t see anything that would make this impossible/very hard to do but thought @janko and other contributors will have more context.

Hi @janklimo :wave:

It would be nice to find a Sorbet-friendly syntax. In your proposed way, would it be possible to use different uploaders for different attachments? Something like this in current syntax:

class Profile < Sequel::Model
  include ImageUploader::Attachment.new(:image)
  include VideoUploader::Attachment.new(:video)
end

Thank you for your response @janko!

In fact this proposal won’t be relevant. As I learned later, you can do the following to get around compiler warnings:

class Profile < Sequel::Model
  Attachment = ImageUploader::Attachment.new(:image)
  include Attachment
end