module Shrine::Plugins::Activerecord::AttacherMethods

The _persistence plugin uses activerecord_persist, activerecord_reload and activerecord? to implement the following methods:

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

Private Instance Methods

activerecord?() click to toggle source

Returns whether the record is an ActiveRecord model. Used by the _persistence plugin.

# File lib/shrine/plugins/activerecord.rb, line 121
def activerecord?
  record.is_a?(::ActiveRecord::Base)
end
activerecord_after_destroy() click to toggle source

Deletes attached files. Called after model destroy.

# File lib/shrine/plugins/activerecord.rb, line 95
def activerecord_after_destroy
  destroy_attached
end
activerecord_after_save() click to toggle source

Finalizes attachment and persists changes. Called after model save.

# File lib/shrine/plugins/activerecord.rb, line 89
def activerecord_after_save
  finalize
  persist
end
activerecord_before_save() click to toggle source

Calls Attacher#save. Called before model save.

# File lib/shrine/plugins/activerecord.rb, line 84
def activerecord_before_save
  save
end
activerecord_hash_attribute?() click to toggle source

Returns true if the data attribute represents a JSON or JSONB column. Used by the _persistence plugin to determine whether serialization should be skipped.

# File lib/shrine/plugins/activerecord.rb, line 114
def activerecord_hash_attribute?
  column = record.class.columns_hash[attribute.to_s]
  column && [:json, :jsonb].include?(column.type)
end
activerecord_persist() click to toggle source

Saves changes to the model instance, skipping validations. Used by the _persistence plugin.

# File lib/shrine/plugins/activerecord.rb, line 101
def activerecord_persist
  record.save(validate: false)
end
activerecord_reload() { |clone.reload(lock: true)| ... } click to toggle source

Locks the database row and yields the reloaded record. Used by the _persistence plugin.

# File lib/shrine/plugins/activerecord.rb, line 107
def activerecord_reload
  record.transaction { yield record.clone.reload(lock: true) }
end
activerecord_validate() click to toggle source

Adds file validation errors to the model. Called on model validation.

# File lib/shrine/plugins/activerecord.rb, line 75
def activerecord_validate
  return unless respond_to?(:errors)

  errors.each do |(type, options)|
    record.errors.add(name, type, **options.to_h)
  end
end