Repository navigation
Expand file tree
/
Copy pathactiverecord.rb
More file actions
150 lines (126 loc) · 4.87 KB
/
Copy pathactiverecord.rb
File metadata and controls
150 lines (126 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true
require "active_record"
class Shrine
module Plugins
# Documentation can be found on https://shrinerb.com/docs/plugins/activerecord
module Activerecord
def self.load_dependencies(uploader, **)
uploader.plugin :model
uploader.plugin :_persistence, plugin: self
end
def self.configure(uploader, **opts)
uploader.opts[:activerecord] ||= { callbacks: true, validations: true, attribute_types: false }
uploader.opts[:activerecord].merge!(opts)
end
module AttachmentMethods
def included(model)
super
return unless model < ::ActiveRecord::Base
name = @name
if shrine_class.opts[:activerecord][:validations]
model.validate do
send(:"#{name}_attacher").send(:activerecord_validate)
end
end
if shrine_class.opts[:activerecord][:callbacks]
model.before_save do
if send(:"#{name}_attacher").changed?
send(:"#{name}_attacher").send(:activerecord_before_save)
end
end
[:create, :update].each do |action|
model.after_commit on: action do
if send(:"#{name}_attacher").changed?
send(:"#{name}_attacher").send(:activerecord_after_save)
end
end
end
model.after_commit on: :destroy do
if send(:"#{name}_attacher").attached?
send(:"#{name}_attacher").send(:activerecord_after_destroy)
end
end
end
# reload the attacher on record reload
define_method :reload do |*args|
result = super(*args)
send(:"#{name}_attacher").reload if instance_variable_defined?(:"@#{name}_attacher")
result
end
end
end
# The _persistence plugin uses #activerecord_persist,
# #activerecord_reload and #activerecord? to implement the following
# methods:
#
# * Attacher#persist
# * Attacher#atomic_persist
# * Attacher#atomic_promote
module AttacherMethods
private
# Adds file validation errors to the model. Called on model validation.
def activerecord_validate
return unless respond_to?(:errors)
errors.each do |(type, options)|
record.errors.add(name, type, **options.to_h)
end
end
# Calls Attacher#save. Called before model save.
def activerecord_before_save
save
end
# Finalizes attachment and persists changes. Called after model save.
def activerecord_after_save
finalize
persist
end
# Deletes attached files. Called after model destroy.
def activerecord_after_destroy
destroy_attached
end
# Saves changes to the model instance, skipping validations. Used by
# the _persistence plugin.
def activerecord_persist
record.save(validate: false)
end
# Locks the database row and yields the reloaded record. Used by the
# _persistence plugin.
def activerecord_reload
record.transaction { yield record.clone.reload(lock: true) }
end
# Returns true if the data attribute represents a JSON or JSONB type.
# Used by the _persistence plugin to determine whether serialization
# should be skipped.
#
# By default only real database columns are checked. When the
# `:attribute_types` plugin option is enabled, the check uses
# `type_for_attribute`, so attributes declared via the Attributes API
# are recognized as well.
def activerecord_hash_attribute?
if shrine_class.opts[:activerecord][:attribute_types]
attribute_type = record.class.type_for_attribute(attribute.to_s)
attribute_type && [:json, :jsonb].include?(attribute_type.type)
else
column = record.class.columns_hash[attribute.to_s]
column && [:json, :jsonb].include?(column.type)
end
end
# Returns whether the record is an ActiveRecord model. Used by the
# _persistence plugin.
def activerecord?
record.is_a?(::ActiveRecord::Base)
end
# Prevents the previous file from being destroyed when this save
# created the record (as opposed to updating an existing one), since
# a record being created can't yet have its own confirmed attachment
# to safely replace (e.g. when it was duplicated via `#dup` from
# another, still-persisted record).
def destroy_previous?
return super unless activerecord?
super && !record.previously_new_record?
end
end
end
register_plugin(:activerecord, Activerecord)
end
end