module Shrine::Plugins::Entity::AttacherMethods

Attributes

name[R]
record[R]

Public Instance Methods

attribute() click to toggle source

Returns the entity attribute name used for reading and writing attachment data.

attacher = Shrine::Attacher.from_entity(photo, :image)
attacher.attribute #=> :image_data
# File lib/shrine/plugins/entity.rb, line 143
def attribute
  :"#{name}_data" if name
end
column_values() click to toggle source

Returns a hash with entity attribute name and column data.

attacher.column_values
#=> { image_data: '{"id":"...","storage":"...","metadata":{...}}' }
# File lib/shrine/plugins/entity.rb, line 134
def column_values
  { attribute => column_data }
end
load_entity(record, name) click to toggle source

Saves record and name and initializes attachment from the entity attribute. Called from ‘Attacher.from_entity`.

# File lib/shrine/plugins/entity.rb, line 96
def load_entity(record, name)
  set_entity(record, name)
  read
end
read() click to toggle source

Loads attachment from the entity attribute.

# File lib/shrine/plugins/entity.rb, line 126
def read
  load_column(read_attribute)
end
reload() click to toggle source

Overwrites the current attachment with the one from model attribute.

photo.image_data #=> nil
attacher = Shrine::Attacher.from_entity(photo, :image)
photo.image_data = uploaded_file.to_json

attacher.file #=> nil
attacher.reload
attacher.file #=> #<Shrine::UploadedFile>
# File lib/shrine/plugins/entity.rb, line 119
def reload
  read
  @previous = nil
  self
end
set_entity(record, name) click to toggle source

Sets record and name without loading the attachment from the entity attribute.

# File lib/shrine/plugins/entity.rb, line 103
def set_entity(record, name)
  @record = record
  @name   = name.to_sym

  @context.merge!(record: record, name: name)
end

Private Instance Methods

read_attribute() click to toggle source

Reads value from the entity attribute.

# File lib/shrine/plugins/entity.rb, line 150
def read_attribute
  record.public_send(attribute)
end