Skip to main content

Included

The included plugin allows you to hook up to the .included hook of the attachment module, and call additional methods on the model that includes it.

class ImageUploader < Shrine
  plugin :included do |name|
    # called when attachment module is included into a model

    self #=> Photo (the model class)
    name #=> :image
  end
end
class Photo
  include ImageUploader::Attachment(:image) # triggers the included block
end

For example, you can use it to define additional methods on the model:

class ImageUploader < Shrine
  plugin :included do |name|
    define_method(:"#{name}_width")  { send(name)&.width  }
    define_method(:"#{name}_height") { send(name)&.height }
  end
end
photo = Photo.new(image: file)
photo.image_width  #=> 1200
photo.image_height #=> 800