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
|
# File 'lib/echo_uploads/prm_file_writing.rb', line 17
def echo_uploads_configure_prm_file_writing(attr, options)
define_model_callbacks :echo_uploads_write_prm
after_save do |model|
@echo_uploads_prm_files_saved ||= {}
if (file = send(attr)).present? and @echo_uploads_prm_files_saved[attr.to_sym] != file
run_callbacks :echo_uploads_write_prm do
@echo_uploads_prm_files_saved[attr.to_sym] = file
if options[:multiple]
metas = send("#{attr}_metadatas")
else
metas = [send("#{attr}_metadata")].compact
end
if metas.any?
metas.each(&:destroy)
end
if options[:multiple]
mapped_files = send("mapped_#{attr}") ||
raise('echo_uploads called with :multiple, but :map option was missing')
metas = mapped_files.map do |mapped_file|
::EchoUploads::File.new(
owner: model, temporary: false, file: mapped_file
)
end
send("#{attr}_metadatas=", metas)
elsif options[:map]
mapped_files = send("mapped_#{attr}")
metas = [::EchoUploads::File.new(
owner: model, temporary: false, file: mapped_files.first
)]
send("#{attr}_metadata=", metas.first)
else
metas = [::EchoUploads::File.new(
owner: model, temporary: false, file: send(attr)
)]
send("#{attr}_metadata=", metas.first)
end
metas.each do |meta|
meta.persist! attr, options
end
end
elsif metas = send("#{attr}_tmp_metadata")
run_callbacks :echo_uploads_write_prm do
if options[:multiple]
model.send("#{attr}_metadatas").each(&:destroy)
elsif old = model.send("#{attr}_metadata")
old.destroy
end
metas.each do |meta|
meta.owner = model
meta.temporary = false
meta.expires_at = nil
meta.save!
end
if options[:multiple]
send("#{attr}_metadatas=", metas)
else
send("#{attr}_metadata=", metas.first)
end
end
end
end
end
|