Module: Shrine::Plugins::Model::AttacherMethods

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

Instance Method Summary collapse

Instance Method Details

#initialize(model_cache: shrine_class.opts[:model], **options) ⇒ Object



91
92
93
94
95
# File 'lib/shrine/plugins/model.rb', line 91

def initialize(model_cache: shrine_class.opts[:model][:cache], **options)
  super(**options)
  @model_cache = model_cache
  @model       = nil
end

#load_model(record, name) ⇒ Object

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



99
100
101
102
# File 'lib/shrine/plugins/model.rb', line 99

def load_model(record, name)
  set_model(record, name)
  read
end

#model_assign(value, **options) ⇒ Object

Called by the attachment attribute setter on the model.



112
113
114
115
116
117
118
# File 'lib/shrine/plugins/model.rb', line 112

def model_assign(value, **options)
  if model_cache?
    assign(value, **options)
  else
    attach(value, **options)
  end
end

#setObject

Writes uploaded file data into the model.



121
122
123
124
125
# File 'lib/shrine/plugins/model.rb', line 121

def set(*)
  result = super
  write if model?
  result
end

#set_model(record, name) ⇒ Object

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



106
107
108
109
# File 'lib/shrine/plugins/model.rb', line 106

def set_model(record, name)
  set_entity(record, name)
  @model = true
end

#writeObject

Writes the attachment data into the model attribute.



128
129
130
131
132
# File 'lib/shrine/plugins/model.rb', line 128

def write
  column_values.each do |name, value|
    write_attribute(name, value)
  end
end