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]) ⇒ Object



93
94
95
96
97
# File 'lib/shrine/plugins/model.rb', line 93

def initialize(model_cache: shrine_class.opts[:model][:cache], **)
  super(**)
  @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`.



101
102
103
104
# File 'lib/shrine/plugins/model.rb', line 101

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

#model_assign(value) ⇒ Object

Called by the attachment attribute setter on the model.



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

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

#setObject

Writes uploaded file data into the model.



123
124
125
126
127
# File 'lib/shrine/plugins/model.rb', line 123

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.



108
109
110
111
# File 'lib/shrine/plugins/model.rb', line 108

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

#writeObject

Writes the attachment data into the model attribute.



130
131
132
133
134
# File 'lib/shrine/plugins/model.rb', line 130

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