better_record 0.21.3 → 0.22.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: 1e7333954348a14017d9656ebb1dad398b7b2cde2f079cd1d28f9c4a996644a6
4
- data.tar.gz: 6d75a37541af0305d17227205631db27b3a1908886de354baf59a695bb4b22b5
3
+ metadata.gz: f87fad71d3c41ee882975f1fa04439d66097c08e73e612b4566b2080dc42a3d4
4
+ data.tar.gz: 0a3802ec44c019360ef5e85437c941b797b02ef22e7e630da5ecd6ff21f124b0
5
5
  SHA512:
6
- metadata.gz: 282f473b7382023664f51d68e5e30d62d98039a749c0ca6101a537588430d7eb7905cf1a0efd8088dcde563a2ac1308a1cea7a02708bfa3a1a7f421baa516227
7
- data.tar.gz: ec97ecd9c65d53b34b1c1d78299c5e30b150ace7982b6863a569084a6aab5107005cdaad851ad9bf0961dec39cc0f90eced8fa2b755180d6e6cff12c465f2e4c
6
+ metadata.gz: 4f6592c9d85fcdd10c3dcf0782b0c3785d1f48a5575756cec4a177668d606785968814b1e8455ce094aa66dead9cd8c636858f8c283869d97b4d8a0c228b50ff
7
+ data.tar.gz: e40e619662ad6a3c6a1aa68767c110cdd0417cdfd56d05cdb06b9c1f21b2c988cea3381d958e0b3571c7d9c154bf98d5028206e714362af09aec16966e9be314
@@ -28,9 +28,11 @@ module BetterRecord
28
28
  # == Boolean Class Methods ================================================
29
29
 
30
30
  # == Class Methods ========================================================
31
- # def self.boolean_columns
32
- # @@boolean_columns ||= %i[ ran ].freeze
33
- # end
31
+ def self.delete_invalid
32
+ BetterRecord::AttachmentValidation.
33
+ where.not( id: BetterRecord::AttachmentValidation.joins(:attachment).select(:id) ).
34
+ delete_all
35
+ end
34
36
 
35
37
  # == Boolean Methods ======================================================
36
38
 
@@ -58,10 +58,14 @@ module BetterRecord
58
58
  end
59
59
 
60
60
  define_method :"create_#{avatar_name}_validation" do |ran=false|
61
+ BetterRecord::AttachmentValidation.delete_invalid
61
62
  begin
62
- AttachmentValidation.create!(name: image_validator, attachment_id: __send__(avatar_name).id, ran: ran)
63
+ opts = { name: image_validator, attachment_id: reloaded_record&.__send__(avatar_name)&.attachment&.id, ran: ran }
64
+ AttachmentValidation.create!(opts) if opts[:attachment_id]
63
65
  rescue
64
- __send__(:"#{avatar_name}_validation_record").update(ran: true) if $!.is_a?(PG::UniqueViolation) && ran && !__send__(:"#{avatar_name}_validation_record").ran
66
+ if ran && $!.is_a?(PG::UniqueViolation) && !__send__(:"#{avatar_name}_validation_record").ran
67
+ __send__(:"#{avatar_name}_validation_record").update(ran: true)
68
+ end
65
69
  end
66
70
  __send__(:"#{avatar_name}_validation_record")
67
71
  end
@@ -88,7 +92,7 @@ module BetterRecord
88
92
  puts "\nSHRINKING\n"
89
93
  self.__send__ :"shrinking_#{avatar_name}=", true
90
94
  (
91
- shrink_wait_time ?
95
+ shrink_wait_time ?
92
96
  ResizeBlobImageJob.set(wait: shrink_wait_time) :
93
97
  ResizeBlobImageJob
94
98
  ).perform_later(
@@ -112,75 +116,62 @@ module BetterRecord
112
116
  define_method :valid_image do
113
117
  return true unless __send__(avatar_name).attached?
114
118
  __send__(:"create_#{avatar_name}_validation", true)
119
+ raise "Uh Oh" unless __send__(:"#{avatar_name}_validation_ran?", true)
115
120
 
116
121
  if valid_image_format && valid_image_size
117
122
  self.__send__(:"shrinking_#{avatar_name}") ||
118
- reloaded_record.__send__(:"cache_current_#{avatar_name}")
123
+ __send__(:"cache_current_#{avatar_name}")
124
+ true
119
125
  else
120
- r = reloaded_record.__send__(avatar_name)
121
- begin
122
- r.purge_later if r.attached?
123
- rescue Exception
124
- end
125
- __send__(:"load_last_#{avatar_name}") if __send__(:"last_#{avatar_name}").attached?
126
+ __send__(:"load_last_#{avatar_name}")
126
127
  false
127
128
  end
128
129
  end
129
130
 
130
131
  define_method :"check_#{image_validator}" do |*args|
131
- return true unless __send__(avatar_name).attached?
132
+ return true if self.id.blank? || !reloaded_record&.__send__(avatar_name).attached?
132
133
  __send__(image_validator) unless __send__(:"#{avatar_name}_validation_ran?", true)
133
134
  end
134
135
 
135
136
  define_method :"cache_current_#{avatar_name}" do
136
- reloaded_record.__send__ :"copy_#{avatar_name}"
137
+ __send__ :"copy_#{avatar_name}"
137
138
  end
138
139
 
139
140
  define_method :"load_last_#{avatar_name}" do
140
- reloaded_record.__send__ :"copy_#{avatar_name}", :"last_#{avatar_name}", avatar_name
141
+ __send__ :"copy_#{avatar_name}", :"last_#{avatar_name}", avatar_name
142
+ __send__(:"create_#{avatar_name}_validation", true)
141
143
  end
142
144
 
143
145
  define_method :"copy_#{avatar_name}" do |from = avatar_name, to = :"last_#{avatar_name}"|
144
146
  puts "COPYING #{from} TO #{to}"
147
+ # begin
148
+ # rescue
149
+ # puts $!.message
150
+ # puts $!.backtrace
151
+ # end
152
+
145
153
  from_attachment = __send__ from
154
+ to_attachment = __send__ to
146
155
 
147
- delete_attachment to
148
156
 
149
157
  if from_attachment.attached?
150
- tmp = Tempfile.new
151
- tmp.binmode
152
- tmp.write(from_attachment.download)
153
- tmp.flush
154
- tmp.rewind
155
-
156
- r = reloaded_record
157
- from_attachment = r.__send__ from
158
- to_attachment = r.__send__ to
159
-
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
- )
167
- tmp.close
158
+ return true if from_attachment.attachment&.blob_id == to_attachment.attachment&.blob_id
159
+ delete_attachment to
160
+ to_attachment.attach from_attachment.blob
161
+ else
162
+ delete_attachment to
168
163
  end
169
- true
170
164
  end
171
165
 
172
166
  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(id: atchd.id)
177
- atch&.__send__ now ? :purge : :purge_later
178
- rescue Exception
179
- begin
180
- ActiveStorage::Attachment.find_by(id: atchd.id).destroy
181
- rescue Exception
182
- end
167
+ begin
168
+ atchd = __send__ att_name
169
+ if atchd.attachment
170
+ atchd_blob = atchd.blob
171
+ atchd.detach
172
+ atchd_blob&.__send__ now ? :purge : :purge_later
183
173
  end
174
+ rescue Exception
184
175
  end
185
176
  end
186
177
 
@@ -0,0 +1 @@
1
+ ActiveStorage::Attachment.belongs_to :record, polymorphic: true, touch: false
@@ -80,16 +80,6 @@ module BetterRecord
80
80
  val
81
81
  end
82
82
 
83
- def keys(pattern = ".*")
84
- data.keys.filter {|k| k.to_s =~ /^#{pattern}$/}
85
- end
86
-
87
- def del(*keys)
88
- i = 0
89
- keys.map {|k| data.delete(k) && (i += 1) }
90
- i
91
- end
92
-
93
83
  def method_missing
94
84
  return nil
95
85
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BetterRecord
4
- VERSION = '0.21.3'
4
+ VERSION = '0.22.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.3
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sampson Crowley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-24 00:00:00.000000000 Z
11
+ date: 2019-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -252,6 +252,7 @@ files:
252
252
  - config/initializers/active_record/gender_type.rb
253
253
  - config/initializers/active_record/money_integer_type.rb
254
254
  - config/initializers/active_record/three_state_type.rb
255
+ - config/initializers/active_storage/attachment.rb
255
256
  - config/initializers/active_support/time_with_zone.rb
256
257
  - config/initializers/concerns.rb
257
258
  - config/initializers/core_ext/date.rb