Module: Shrine::Plugins::Entity::AttacherMethods

Defined in:
lib/shrine/plugins/entity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



86
87
88
# File 'lib/shrine/plugins/entity.rb', line 86

def name
  @name
end

#recordObject (readonly)

Returns the value of attribute record.



86
87
88
# File 'lib/shrine/plugins/entity.rb', line 86

def record
  @record
end

Instance Method Details

#attributeObject

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

attacher = Shrine::Attacher.from_entity(photo, :image)
attacher.attribute #=> :image_data


131
132
133
# File 'lib/shrine/plugins/entity.rb', line 131

def attribute
  :"#{name}_data" if name
end

#column_valuesObject

Returns a hash with entity attribute name and column data.

attacher.column_values
#=> { image_data: '{"id":"...","storage":"...","metadata":{...}}' }


122
123
124
# File 'lib/shrine/plugins/entity.rb', line 122

def column_values
  { attribute => column_data }
end

#load_entity(record, name) ⇒ Object

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



90
91
92
93
# File 'lib/shrine/plugins/entity.rb', line 90

def load_entity(record, name)
  set_entity(record, name)
  read
end

#reloadObject

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>


113
114
115
116
# File 'lib/shrine/plugins/entity.rb', line 113

def reload
  read
  self
end

#set_entity(record, name) ⇒ Object

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



97
98
99
100
101
102
# File 'lib/shrine/plugins/entity.rb', line 97

def set_entity(record, name)
  @record = record
  @name   = name.to_sym

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