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][:cache], **options) ⇒ Object


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

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`.


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, **options) ⇒ 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, **options)
  if model_cache?
    assign(value, **options)
  else
    attach(value, **options)
  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