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

137
138
139
# File 'lib/shrine/plugins/entity.rb', line 137

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":{...}}' }

128
129
130
# File 'lib/shrine/plugins/entity.rb', line 128

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

#readObject

Loads attachment from the entity attribute.


120
121
122
# File 'lib/shrine/plugins/entity.rb', line 120

def read
  load_column(read_attribute)
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
117
# File 'lib/shrine/plugins/entity.rb', line 113

def reload
  read
  @previous = nil
  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