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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: febb0b3a70dd46f389f6c152b835d5119429221993922c1ba1bc7029468e1cb7
4
- data.tar.gz: bec1e1fba9f38d806032782af7ac0137819345c6ef72f63eee86b9786c7e5bf5
3
+ metadata.gz: 005c13b67e5b89937f02958dbfb9cb5746aed4b94ff34faf96f028acce259515
4
+ data.tar.gz: cab0ce9f41ac2706093fc2e00a6d95b6465fc01203d699b5ae33dad1c0ccafd5
5
5
  SHA512:
6
- metadata.gz: 4afaf0a2dca9474a4722168fed7da28568c59ef82ed227b203b1846e14fca0fa851c103af298a5c2daa8417577250f82ba0d8fbca9390f06779a3e63d8cb4b95
7
- data.tar.gz: e206891de1e623b33883c6a95679d477040454c4e88b0fdc98bef1f0ac8abc96d06ffb4c2b8150a49b524abd86c06a28459981177733868742fbcc900a171a46
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
- def url
40
- file_url
41
- end
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
- def extension
52
- file.extension
53
- end
44
+ def content_type
45
+ file.mime_type
46
+ end
54
47
 
55
- def representable?
56
- %r{image/.*}.match? content_type
57
- end
48
+ def filename
49
+ file.original_filename
50
+ end
58
51
 
59
- def signed_id
60
- # add the id to ensure uniqueness
61
- value = ({id:, file: file.to_json} if file.present?) || {}
62
- Rails.application.message_verifier(:active_shrine_attachment).generate value
63
- end
52
+ def extension
53
+ file.extension
54
+ end
64
55
 
65
- def file=(value)
66
- # It is the same file. we are good to go.
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
- super(value)
84
- rescue ActiveSupport::MessageVerifier::InvalidSignature
85
- errors.add(:file, "is invalid")
86
- end
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
- def purge
89
- file_attacher.destroy_block { destroy } if file_attacher.respond_to?(:destroy_block)
90
- destroy
91
- end
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
- def purge_later
94
- file_attacher.destroy_background
95
- file_attacher.instance_variable_set :@file, nil # prevent shrine from attempting to destroy the file again
96
- destroy
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
- private
89
+ def purge
90
+ file_attacher.destroy_block { destroy } if file_attacher.respond_to?(:destroy_block)
91
+ destroy
92
+ end
103
93
 
104
- def maybe_store_record
105
- return unless record.present?
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
- metadata.merge! record_type:, record_id:
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
@@ -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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveShrine
4
- VERSION = "0.3.3"
4
+ VERSION = "0.3.4"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_shrine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Radioactive Labs