active_shrine 0.3.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 995a4b8dce06a3080e166a8953c6fc3af4b6df04bdfc68a7e291b9a106990f4f
|
4
|
+
data.tar.gz: 857c7b1fbe6976f22c1bb0d20e95173117c29d20c36298fbbe13e9189e125c97
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 332f75c6d900ba40f7304fecd0f9d8220ebb2784098824be24d904ab34f875eac625ead20ace853922159c632a66a6281246d3549ba8a6a319b6e50dc32e87df
|
7
|
+
data.tar.gz: 8ffd4133f22f296d0bb18b26f05e4546d2aeee0710e456ea3dcf78d42c0f693e3576c2529fd5ab9fb686c5e30c8c79ea7e37186da2e2a6dfbfecd5680712eef1
|
@@ -23,6 +23,14 @@ module ActiveShrine
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def save
|
26
|
+
unless attachment.valid?
|
27
|
+
attachment.errors.each do |error|
|
28
|
+
record.errors.add(name, error.message)
|
29
|
+
end
|
30
|
+
|
31
|
+
raise ActiveRecord::RecordInvalid.new(record)
|
32
|
+
end
|
33
|
+
|
26
34
|
record.public_send(:"#{name}_attachment=", attachment)
|
27
35
|
end
|
28
36
|
|
@@ -36,75 +36,77 @@ module ActiveShrine
|
|
36
36
|
|
37
37
|
before_save :maybe_store_record
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
def content_type
|
44
|
-
file.mime_type
|
45
|
-
end
|
46
|
-
|
47
|
-
def filename
|
48
|
-
file.original_filename
|
49
|
-
end
|
39
|
+
module AttachmentMethods
|
40
|
+
def url
|
41
|
+
file_url
|
42
|
+
end
|
50
43
|
|
51
|
-
|
52
|
-
|
53
|
-
|
44
|
+
def content_type
|
45
|
+
file.mime_type
|
46
|
+
end
|
54
47
|
|
55
|
-
|
56
|
-
|
57
|
-
|
48
|
+
def filename
|
49
|
+
file.original_filename
|
50
|
+
end
|
58
51
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
Rails.application.message_verifier(:active_shrine_attachment).generate value
|
63
|
-
end
|
52
|
+
def extension
|
53
|
+
file.extension
|
54
|
+
end
|
64
55
|
|
65
|
-
|
66
|
-
|
67
|
-
return if value == signed_id
|
68
|
-
|
69
|
-
if value.is_a?(String)
|
70
|
-
# it is an already uploaded file. either
|
71
|
-
# - via direct upload so the form is sending us a json hash to set
|
72
|
-
# - or was set because a previous submission failed, so the form is sending us the signed_id
|
73
|
-
begin
|
74
|
-
# attempt to parse as a json hash
|
75
|
-
value = JSON.parse value
|
76
|
-
rescue JSON::ParserError
|
77
|
-
# this is not a valid json hash, let's check if it is a valid signed_id
|
78
|
-
unsigned = Rails.application.message_verifier(:active_shrine_attachment).verify value
|
79
|
-
value = JSON.parse unsigned["file"]
|
80
|
-
end
|
56
|
+
def representable?
|
57
|
+
%r{image/.*}.match? content_type
|
81
58
|
end
|
82
59
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
60
|
+
def signed_id
|
61
|
+
# add the id to ensure uniqueness
|
62
|
+
value = ({id:, file: file.to_json} if file.present?) || {}
|
63
|
+
Rails.application.message_verifier(:active_shrine_attachment).generate value
|
64
|
+
end
|
87
65
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
66
|
+
def file=(value)
|
67
|
+
# It is the same file. we are good to go.
|
68
|
+
return if value == signed_id
|
69
|
+
|
70
|
+
if value.is_a?(String)
|
71
|
+
# it is an already uploaded file. either
|
72
|
+
# - via direct upload so the form is sending us a json hash to set
|
73
|
+
# - or was set because a previous submission failed, so the form is sending us the signed_id
|
74
|
+
begin
|
75
|
+
# attempt to parse as a json hash
|
76
|
+
value = JSON.parse value
|
77
|
+
rescue JSON::ParserError
|
78
|
+
# this is not a valid json hash, let's check if it is a valid signed_id
|
79
|
+
unsigned = Rails.application.message_verifier(:active_shrine_attachment).verify value
|
80
|
+
value = JSON.parse unsigned["file"]
|
81
|
+
end
|
82
|
+
end
|
92
83
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
rescue NoMethodError
|
98
|
-
raise NotImplementedError, ("You need to enable Shrine backgrounding to use purge_later: " \
|
99
|
-
"https://shrinerb.com/docs/plugins/backgrounding")
|
100
|
-
end
|
84
|
+
super(value)
|
85
|
+
rescue ActiveSupport::MessageVerifier::InvalidSignature
|
86
|
+
errors.add(:file, "is invalid")
|
87
|
+
end
|
101
88
|
|
102
|
-
|
89
|
+
def purge
|
90
|
+
file_attacher.destroy_block { destroy } if file_attacher.respond_to?(:destroy_block)
|
91
|
+
destroy
|
92
|
+
end
|
103
93
|
|
104
|
-
|
105
|
-
|
94
|
+
def purge_later
|
95
|
+
file_attacher.destroy_background
|
96
|
+
file_attacher.instance_variable_set :@file, nil # prevent shrine from attempting to destroy the file again
|
97
|
+
destroy
|
98
|
+
rescue NoMethodError
|
99
|
+
raise NotImplementedError, ("You need to enable Shrine backgrounding to use purge_later: " \
|
100
|
+
"https://shrinerb.com/docs/plugins/backgrounding")
|
101
|
+
end
|
106
102
|
|
107
|
-
|
103
|
+
private
|
104
|
+
|
105
|
+
def maybe_store_record
|
106
|
+
return unless record.present?
|
107
|
+
|
108
|
+
metadata.merge! record_type:, record_id:
|
109
|
+
end
|
108
110
|
end
|
109
111
|
end
|
110
112
|
end
|
data/lib/active_shrine/model.rb
CHANGED
@@ -48,6 +48,7 @@ module ActiveShrine
|
|
48
48
|
# Dynamically create a new class that inherits from ActiveShrine::Attachment
|
49
49
|
Class.new(::ActiveShrine::Attachment) do
|
50
50
|
include uploader::Attachment(:file)
|
51
|
+
include ::ActiveShrine::Attachment::AttachmentMethods
|
51
52
|
end.tap do |klass|
|
52
53
|
# Define the class in the ActiveShrine namespace
|
53
54
|
ActiveShrine.const_set(:"#{uploader}Attachment", klass)
|
@@ -139,9 +140,6 @@ module ActiveShrine
|
|
139
140
|
|
140
141
|
after_save do
|
141
142
|
shrine_attachment_changes[name.to_s]&.save
|
142
|
-
rescue => e
|
143
|
-
errors.add(name, :invalid, message: "failed to save. Please make sure it is a valid file.")
|
144
|
-
raise ActiveRecord::RecordInvalid.new(self)
|
145
143
|
end
|
146
144
|
|
147
145
|
after_commit(on: %i[create update]) { shrine_attachment_changes.delete(name.to_s) }
|
@@ -239,9 +237,6 @@ module ActiveShrine
|
|
239
237
|
|
240
238
|
after_save do
|
241
239
|
shrine_attachment_changes[name.to_s]&.save
|
242
|
-
rescue => e
|
243
|
-
errors.add(name, :invalid, message: "failed to save. Please make sure it is a valid file.")
|
244
|
-
raise ActiveRecord::RecordInvalid.new(self)
|
245
240
|
end
|
246
241
|
|
247
242
|
after_commit(on: %i[create update]) { shrine_attachment_changes.delete(name.to_s) }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_shrine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Radioactive Labs
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-02-
|
11
|
+
date: 2025-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|