module Shrine::Plugins::Mongoid::AttacherMethods

The _persistence plugin uses mongoid_persist, mongoid_reload and mongoid? to implement the following methods:

* Attacher#persist
* Attacher#atomic_persist
* Attacher#atomic_promote

Private Instance Methods

mongoid?() click to toggle source

Returns whether the record is a Mongoid document. Used by the _persistence plugin.

# File lib/shrine/plugins/mongoid.rb, line 116
def mongoid?
  record.is_a?(::Mongoid::Document)
end
mongoid_after_destroy() click to toggle source

Deletes attached files. Called after model destroy.

# File lib/shrine/plugins/mongoid.rb, line 88
def mongoid_after_destroy
  destroy_attached
end
mongoid_after_save() click to toggle source

Finalizes attachment and persists changes. Called after model save.

# File lib/shrine/plugins/mongoid.rb, line 80
def mongoid_after_save
  return unless changed?

  finalize
  persist
end
mongoid_before_save() click to toggle source

Calls Attacher#save. Called before model save.

# File lib/shrine/plugins/mongoid.rb, line 73
def mongoid_before_save
  return unless changed?

  save
end
mongoid_hash_attribute?() click to toggle source

Returns true if the data attribute represents a Hash field. Used by the _persistence plugin to determine whether serialization should be skipped.

# File lib/shrine/plugins/mongoid.rb, line 109
def mongoid_hash_attribute?
  field = record.class.fields[attribute.to_s]
  field && field.type == Hash
end
mongoid_persist() click to toggle source

Saves changes to the model instance, raising exception on validation errors. Used by the _persistence plugin.

# File lib/shrine/plugins/mongoid.rb, line 94
def mongoid_persist
  record.save(validate: false)
end
mongoid_reload() { |reload| ... } click to toggle source

Yields the reloaded record. Used by the _persistence plugin.

# File lib/shrine/plugins/mongoid.rb, line 99
def mongoid_reload
  record_copy    = record.dup
  record_copy.id = record.id

  yield record_copy.reload
end
mongoid_validate() click to toggle source
# File lib/shrine/plugins/mongoid.rb, line 64
def mongoid_validate
  return unless respond_to?(:errors)

  errors.each do |message|
    record.errors.add(name, message)
  end
end