module Shrine::Plugins::Model::AttacherMethods

Public Class Methods

new(model_cache: shrine_class.opts[:model][:cache], **options) click to toggle source
Calls superclass method
# File lib/shrine/plugins/model.rb, line 93
def initialize(model_cache: shrine_class.opts[:model][:cache], **options)
  super(**options)
  @model_cache = model_cache
  @model       = nil
end

Public Instance Methods

load_model(record, name) click to toggle source

Saves record and name and initializes attachment from the model attribute. Called from ‘Attacher.from_model`.

# File lib/shrine/plugins/model.rb, line 101
def load_model(record, name)
  set_model(record, name)
  read
end
model_assign(value, **options) click to toggle source

Called by the attachment attribute setter on the model.

# File lib/shrine/plugins/model.rb, line 114
def model_assign(value, **options)
  if model_cache?
    assign(value, **options)
  else
    attach(value, **options)
  end
end
set(*) click to toggle source

Writes uploaded file data into the model.

Calls superclass method
# File lib/shrine/plugins/model.rb, line 123
def set(*)
  result = super
  write if model?
  result
end
set_model(record, name) click to toggle source

Saves record and name without loading attachment from the model attribute.

# File lib/shrine/plugins/model.rb, line 108
def set_model(record, name)
  set_entity(record, name)
  @model = true
end
write() click to toggle source

Writes the attachment data into the model attribute.

# File lib/shrine/plugins/model.rb, line 130
def write
  column_values.each do |name, value|
    write_attribute(name, value)
  end
end

Private Instance Methods

model?() click to toggle source

Returns whether the attacher is being backed by a model instance. This allows users to still use the attacher with an entity instance or without any record instance.

# File lib/shrine/plugins/model.rb, line 152
def model?
  @model
end
model_cache?() click to toggle source

Returns whether assigned files should be uploaded to/loaded from temporary storage.

# File lib/shrine/plugins/model.rb, line 145
def model_cache?
  @model_cache
end
write_attribute(name = attribute, value) click to toggle source

Writes given value into the model attribute.

# File lib/shrine/plugins/model.rb, line 139
def write_attribute(name = attribute, value)
  record.public_send(:"#{name}=", value)
end