module Shrine::Attachment::InstanceMethods

  1. lib/shrine/attachment.rb

Public Class methods

new (name, **options)

Instantiates an attachment module for a given attribute name, which can then be included to a model class. Second argument will be passed to an attacher module.

[show source]
# File lib/shrine/attachment.rb, line 27
def initialize(name, **options)
  @name    = name.to_sym
  @options = options

  define_attachment_methods!
end

Public Instance methods

attachment_name ()

Returns name of the attachment this module provides.

[show source]
# File lib/shrine/attachment.rb, line 67
def attachment_name
  @name
end
build_attacher (object, options)

Creates an instance of the corresponding Attacher subclass.

[show source]
# File lib/shrine/attachment.rb, line 62
def build_attacher(object, options)
  shrine_class::Attacher.new(object, @name, @options.merge(options))
end
define_attachment_methods! ()

Defines attachment methods for the specified attachment name. These methods will be added to any model that includes this module.

[show source]
# File lib/shrine/attachment.rb, line 36
def define_attachment_methods!
  attachment = self
  name = attachment_name

  define_method :"#{name}_attacher" do |**options|
    if !instance_variable_get(:"@#{name}_attacher") || options.any?
      instance_variable_set(:"@#{name}_attacher", attachment.build_attacher(self, options))
    else
      instance_variable_get(:"@#{name}_attacher")
    end
  end

  define_method :"#{name}=" do |value|
    send(:"#{name}_attacher").assign(value)
  end

  define_method :"#{name}" do
    send(:"#{name}_attacher").get
  end

  define_method :"#{name}_url" do |*args|
    send(:"#{name}_attacher").url(*args)
  end
end
inspect ()

Returns class name with attachment name included.

Shrine[:image].inspect #=> "#<Shrine::Attachment(image)>"
[show source]
# File lib/shrine/attachment.rb, line 86
def inspect
  "#<#{self.class.inspect}(#{attachment_name})>"
end
options ()

Returns options that are to be passed to the Attacher.

[show source]
# File lib/shrine/attachment.rb, line 72
def options
  @options
end
shrine_class ()

Returns the Shrine class that this attachment's class is namespaced under.

[show source]
# File lib/shrine/attachment.rb, line 92
def shrine_class
  self.class.shrine_class
end
to_s ()

Returns class name with attachment name included.

Shrine[:image].to_s #=> "#<Shrine::Attachment(image)>"
[show source]
# File lib/shrine/attachment.rb, line 79
def to_s
  "#<#{self.class.inspect}(#{attachment_name})>"
end