better_record 0.15.1 → 0.15.2

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: f547a8cc802463bac755a8c63cd0a56a29820e4dff4e1fca44aaca4522113f63
4
- data.tar.gz: 4c491bc39f6047d0c4abeb6b4a92df97facbdea967d634b9b1f912fd36cb6a79
3
+ metadata.gz: 38d1f355a28bcc6fa75e43137e4f6d2096c78b85de3420733cf201826424e22d
4
+ data.tar.gz: d5da519726817d407cc67a027041dc0c258daf715f6c8675de934cda2c3b626b
5
5
  SHA512:
6
- metadata.gz: b47c26116fc3921a517895e13f0f2fb9d2110829fbb08c50214a79e084ce3a710f844e5a5b582faf1d0eb63a654e05c2557081224040a1a5547f94515788cad3
7
- data.tar.gz: 2140d13ad729a20cd2545d0884d4ecfad9cd9c2e20a921f66314050d5a87183dabd5293feb36a1dda5ec5464cfeaf11b4bf0c218a14ed194c3c6e39242236c72
6
+ metadata.gz: 2cdb3aeac811064a91c3450942a5e5f0776e539aba2300a14c2ec7f2883caaf985d745e44a0dfaa5761ed794a0ba37322e2583c5d014435c5e5364852799c223
7
+ data.tar.gz: 7e965a6f43b3fb45b47ab026d682bf00294ec8b417077183189d85c39f965d247eb6a83d90e6d3a0e59c2194e4aa938a1c1d7a748262962a64fef211be494892
@@ -13,20 +13,35 @@ module BetterRecord
13
13
  tmp.rewind
14
14
  record.__send__(params[:attachment]).attach(
15
15
  io: tmp,
16
- filename: blob.filename,
16
+ filename: blob.filename.to_s.sub(/(\.[^.]*)$/, '-resized\1'),
17
17
  content_type: blob.content_type
18
18
  )
19
+ tmp.close
20
+ puts "\n\nSAVED IMAGE\n\n"
19
21
  begin
20
- blob.purge_later
22
+ if params[:backup_action].present?
23
+ record.class.find_by(params[:query]).__send__(params[:backup_action].to_sym)
24
+ end
21
25
  rescue
26
+ puts "BACKUP ACTION FAILED"
27
+ puts $!.message
28
+ puts $!.backtrace
22
29
  end
23
- params[:backup_action].present? && record.__send__(params[:backup_action].to_sym)
30
+ begin
31
+ puts "\n\n PURGING BLOB \n\n"
32
+ puts "blob exists? #{blob = ActiveStorage::Blob.find_by(id: blob.id).present?}"
33
+ blob.purge if blob.present?
34
+ puts "\n\n FINISHED PURGING BLOB \n\n"
35
+ rescue
36
+ end
37
+ else
38
+ raise ActiveRecord::RecordNotFound
24
39
  end
25
- true
40
+ return true
26
41
  rescue
27
- p $!.to_s
28
- p $!.message
29
- p $!.backtrace.first(25)
42
+ "ERROR RESIZING IMAGE"
43
+ puts $!.message
44
+ puts $!.backtrace.first(25)
30
45
  return false
31
46
  end
32
47
  end
@@ -16,7 +16,6 @@ module BetterRecord
16
16
  min_image_size: nil,
17
17
  max_image_size: 500.kilobytes,
18
18
  shrink_large_image: false,
19
- shrink_later: false,
20
19
  **opts
21
20
  )
22
21
  # == Constants ============================================================
@@ -61,22 +60,14 @@ module BetterRecord
61
60
  define_method :valid_image_size do
62
61
  if max_image_size && __send__(avatar_name).blob.byte_size > max_image_size
63
62
  if shrink_large_image.present?
64
- begin
65
- blob = __send__(avatar_name).blob
66
- @copy_later = true
67
- ResizeBlobImageJob.
68
- __send__ (shrink_later ? :perform_later : :perform_now), {
69
- model: self.class.to_s,
70
- query: {id: self.id},
71
- attachment: avatar_name.to_s,
72
- backup_action: "cache_current_#{avatar_name}",
73
- options: shrink_large_image
74
- }
75
- rescue
76
- puts $!.message
77
- puts $!.backtrace
78
- return false
79
- end
63
+ @copy_later = true
64
+ ResizeBlobImageJob.perform_later(
65
+ model: self.class.to_s,
66
+ query: {id: self.id},
67
+ attachment: avatar_name.to_s,
68
+ backup_action: image_validator.to_s,
69
+ options: shrink_large_image
70
+ )
80
71
  else
81
72
  errors.add(avatar_name, "is too large, maximum #{ActiveSupport::NumberHelper.number_to_human_size(max_image_size)}")
82
73
  return false
@@ -89,12 +80,17 @@ module BetterRecord
89
80
  end
90
81
 
91
82
  define_method :valid_image do
92
- return unless __send__(avatar_name).attached?
83
+ return true unless __send__(avatar_name).attached?
93
84
 
94
85
  if valid_image_format && valid_image_size
95
- __send__(:"cache_current_#{avatar_name}") unless @copy_later
86
+ return true if @copy_later
87
+ puts "\n\nCOPYING AVATAR\n\n"
88
+ result = self.class.find_by(id: self.id).__send__(:"cache_current_#{avatar_name}")
89
+ puts "\n\nDONE COPYING AVATAR: #{result}\n\n"
90
+ result
96
91
  else
97
- purge(__send__(avatar_name))
92
+ r = self.find_by(id: self.id).__send__(avatar_name)
93
+ r.purge if r.attached?
98
94
  __send__(:"load_last_#{avatar_name}") if __send__(:"last_#{avatar_name}").attached?
99
95
  false
100
96
  end
@@ -109,18 +105,23 @@ module BetterRecord
109
105
  end
110
106
 
111
107
  define_method :"copy_#{avatar_name}" do |from = avatar_name, to = :"last_#{avatar_name}"|
112
- from = __send__ from
113
- to = __send__ to
108
+ puts "COPYING #{from} TO #{to}"
109
+ from_attachment = __send__ from
110
+ to_attachment = __send__ to
114
111
 
115
- purge(to) if to.attached?
112
+ purge(to_attachment) if to_attachment.attached?
116
113
 
117
114
  tmp = Tempfile.new
118
115
  tmp.binmode
119
- tmp.write(from.download)
116
+ tmp.write(from_attachment.download)
120
117
  tmp.flush
121
118
  tmp.rewind
122
119
 
123
- to.attach(io: tmp, filename: from.filename, content_type: from.content_type)
120
+ r = self.class.find_by(id: self.id)
121
+ from_attachment = r.__send__ from
122
+ to_attachment = r.__send__ to
123
+
124
+ to_attachment.attach(io: tmp, filename: from_attachment.filename, content_type: from_attachment.content_type)
124
125
  tmp.close
125
126
  true
126
127
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BetterRecord
4
- VERSION = '0.15.1'
4
+ VERSION = '0.15.2'
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.15.1
4
+ version: 0.15.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sampson Crowley