active_shrine 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_shrine/attachment.rb +60 -58
- data/lib/active_shrine/model.rb +1 -0
- data/lib/active_shrine/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 005c13b67e5b89937f02958dbfb9cb5746aed4b94ff34faf96f028acce259515
|
4
|
+
data.tar.gz: cab0ce9f41ac2706093fc2e00a6d95b6465fc01203d699b5ae33dad1c0ccafd5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2f730760fbfa6e0f685ea41153d9b48621102e4ddbe9ec24f697e898edfefbf172c385bd65561a871163685864e0ecd2c9893fbf444e7d781daf15cf2f725da
|
7
|
+
data.tar.gz: ab387674b136d533f0e3db7aa3d9ac396636c5694e6ab37bd4f6e59dd1e73cca4e42774436840b51c0b5be3cfa4cd9af03cb5385e2619998a9616d1216f59951
|
@@ -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)
|