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
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
Deletes attached files. Called after model destroy.
# File lib/shrine/plugins/activerecord.rb, line 95 def activerecord_after_destroy destroy_attached end
Finalizes attachment and persists changes. Called after model save.
# File lib/shrine/plugins/activerecord.rb, line 89 def activerecord_after_save finalize persist end
Calls Attacher#save
. Called before model save.
# File lib/shrine/plugins/activerecord.rb, line 84 def activerecord_before_save save end
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
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
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
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