better_record 0.19.0 → 0.19.1

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: 3e89311a82ba55ea3a011faa5ad3c85770bc88d29dd402fbd5856167a2151ab8
4
- data.tar.gz: '01396e71a81cc99478664cb762c30d4d663abe3e06c76ce4a82e71e4d676d65d'
3
+ metadata.gz: 6f5322959bf87be1c0bb13ca7f5b72c71566023bb790006e5980bb8c0b59cd88
4
+ data.tar.gz: 9f58c4bae300c5451ef78fd566e90578f1fb741c9b36da9236ab4551169ee545
5
5
  SHA512:
6
- metadata.gz: f527649a03436eeabdbddf11aa1d0ac1b2bf60a0db5c42831fd143c47a3f3aac9fd9f85d6f18cf2142290c03fedef48d0cd2987d826e56f32cbc1f7adfd9c72a
7
- data.tar.gz: 015501e77bd844a80c679ef16b443f9e1f9f561162aaea06102c22f3c66b7f7706521c3869080ba2aa21434d3abe99a82f8377a3d7797635e0bbdef16c1de5aa
6
+ metadata.gz: ba2d95d83eea2ea992b2a190331259553e778ac08999b63d19c27dab614ddf08302f9f08786088ef76b4957a1b92b5ecd39ef3b5b78d258f3f790948438d843b
7
+ data.tar.gz: 55193234a82cfacfa2a85771b6c516bf44f8b610fec7baf8d322aaf30fcebff46aed0634611a2fa418a30d5d80ebb9c964d8f6d752e1c7654cb8b95489f1523e
@@ -5,18 +5,29 @@ module BetterRecord
5
5
  def perform(**params)
6
6
  begin
7
7
  if record = params[:model].constantize.find_by(params[:query].deep_symbolize_keys)
8
- blob = record.__send__(params[:attachment].to_sym).blob
8
+ name = params[:attachment].to_sym
9
+ blob = record.__send__(name).blob
9
10
  tmp = Tempfile.new
10
11
  tmp.binmode
11
- tmp.write(blob.service.download(blob.variant(blob.filename.to_s =~ /-resized?/ ? {resize: '70%'} : params[:options]).processed.key))
12
+ tmp.write(blob.service.download(blob.variant(blob.filename.to_s =~ /-resized/ ? {resize: '70%'} : params[:options]).processed.key))
12
13
  tmp.flush
13
14
  tmp.rewind
14
- record.__send__(params[:attachment]).reload.attach(
15
- io: tmp,
15
+
16
+ blob.analyze if blob.filename.to_s =~ /-resized/
17
+
18
+ attachment = ActionDispatch::Http::UploadedFile.new(
19
+ tempfile: tmp,
16
20
  filename: blob.filename.to_s.sub(/(\.[^.]*)$/, '-resized\1').sub(/(-resized)+/, '-resized'),
17
- content_type: blob.content_type
21
+ type: blob.content_type
18
22
  )
19
- tmp.close
23
+
24
+ record.
25
+ __send__ :delete_attachment, name, true
26
+
27
+ record.
28
+ reload.__send__(params[:attachment]).
29
+ reload.attach(attachment)
30
+
20
31
  puts "\n\nSAVED IMAGE\n\n"
21
32
  begin
22
33
  if params[:backup_action].present?
@@ -25,10 +25,11 @@ module BetterRecord
25
25
  # == Class Methods ========================================================
26
26
  def self.set_audit_methods!
27
27
  begin
28
- connection.execute(%Q(SELECT 1 FROM #{BetterRecord::LoggedAction.table_name.sub('_view', '')}_#{self.table_name} LIMIT 1))
28
+ t_name = BetterRecord::LoggedAction.table_name.sub('_view', '')
29
+ connection.execute(%Q(SELECT 1 FROM #{t_name}_#{self.table_name} LIMIT 1))
29
30
 
30
31
  self.const_set(:LoggedAction, Class.new(ApplicationRecord))
31
- self.const_get(:LoggedAction).table_name = "#{BetterRecord::LoggedAction.table_name}_#{self.table_name}"
32
+ self.const_get(:LoggedAction).table_name = "#{t_name}_#{self.table_name}"
32
33
  rescue ActiveRecord::StatementInvalid
33
34
  self.const_set(:LoggedAction, BetterRecord::LoggedAction)
34
35
  end
@@ -143,19 +143,8 @@ module BetterRecord
143
143
  define_method :"copy_#{avatar_name}" do |from = avatar_name, to = :"last_#{avatar_name}"|
144
144
  puts "COPYING #{from} TO #{to}"
145
145
  from_attachment = __send__ from
146
- to_attachment = __send__ to
147
146
 
148
- if to_attachment.attached?
149
- begin
150
- atch = ActiveStorage::Attachment.find_by(to_attachment.id)
151
- atch&.purge_later
152
- rescue Exception
153
- begin
154
- ActiveStorage::Attachment.find_by(to_attachment.id).destroy
155
- rescue Exception
156
- end
157
- end
158
- end
147
+ delete_attachment to
159
148
 
160
149
  if from_attachment.attached?
161
150
  tmp = Tempfile.new
@@ -168,12 +157,33 @@ module BetterRecord
168
157
  from_attachment = r.__send__ from
169
158
  to_attachment = r.__send__ to
170
159
 
171
- to_attachment.attach(io: tmp, filename: from_attachment.filename, content_type: from_attachment.content_type)
160
+ to_attachment.attach(
161
+ ActionDispatch::Http::UploadedFile.new(
162
+ tempfile: tmp,
163
+ filename: from_attachment.filename.to_s,
164
+ type: from_attachment.content_type
165
+ )
166
+ )
172
167
  tmp.close
173
168
  end
174
169
  true
175
170
  end
176
171
 
172
+ define_method :delete_attachment do |att_name = avatar_name, now = false|
173
+ atchd = __send__ att_name
174
+ if atchd.attached?
175
+ begin
176
+ atch = ActiveStorage::Attachment.find_by(atchd.id)
177
+ atch&.__send__ now ? :purge : :purge_later
178
+ rescue Exception
179
+ begin
180
+ ActiveStorage::Attachment.find_by(atchd.id).destroy
181
+ rescue Exception
182
+ end
183
+ end
184
+ end
185
+ end
186
+
177
187
  define_method :reloaded_record do
178
188
  self.class.find_by(id: self.id)
179
189
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BetterRecord
4
- VERSION = '0.19.0'
4
+ VERSION = '0.19.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.0
4
+ version: 0.19.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sampson Crowley