stratocaster 0.4.0 → 0.4.1

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: 76d62f5aa583c126c93c03936721fee40c94e52f9a5c99b075deaef9f4eb0c46
4
- data.tar.gz: 3e5a37e88a5f76d16c1c5fbb0295523fffa21953eea6b5da37d964618ef5da20
3
+ metadata.gz: 2b7b8160480bffa7931410db64dea27330921142958f7837122f18bc5f7eac9f
4
+ data.tar.gz: 7697cb85701d1b6aec1e2a1203b6215b6ba2a56f25203fb87575411b35d66590
5
5
  SHA512:
6
- metadata.gz: b004638c553dea0929a9ad488e0f9d134f14bfdfa0d5b9da6a1d973bd1d89aec683a6e26ccae7f6945dbe20c799cd169d8d9e5d9f6b9563ff54b34da8d886e64
7
- data.tar.gz: f73752ac70f201b5da23e6550b20a8edbea43ccb0cfb50fa09f691e832e894b0cbdf35bac40e3ab54b63221909ae8be96e04e6f49a67508cebac81cb5600fb62
6
+ metadata.gz: 5718f1372392ffe3a78c312fe74ba33714bbe5573b15831d7a544646ee9f88f04dc6729223108dbd1775901009b5d81e70efb6ad437aa8dd4d313e06773056d3
7
+ data.tar.gz: 35e75720fe35d562706f317f169c894ad559be2ada5af218f2c93a5a382197bcec987ce62bc2024ecbb260f76f52e6324d263c3d93c8e5f8b8fbca60f895a813
@@ -8,6 +8,7 @@ module Stratocaster
8
8
 
9
9
  define_method("#{base_name}?") { send("#{base_name}_filename").present? }
10
10
  strattachments.merge!(base_name => [])
11
+ Stratocaster.attachments << [name, "#{base_name}_filename"] if name
11
12
  block.call(base_name)
12
13
  before_save :upload_strattachment_originals
13
14
  after_commit :perform_processing_job, on: %i[create update]
@@ -67,14 +68,15 @@ module Stratocaster
67
68
  # it. Collect the keys and let a job do the deleting once the transaction has committed, so a rollback
68
69
  # also stops us from orphaning rows that still point at deleted files.
69
70
  def purge_strattachments
70
- filenames = strattachments.flat_map do |base_name, variants|
71
+ files = Hash.new { |hash, filename| hash[filename] = [] }
72
+ strattachments.each do |base_name, variants|
71
73
  filename = send("#{base_name}_filename")
72
- next [] if filename.blank?
74
+ next if filename.blank?
73
75
 
74
- [filename] + variants.map { |variant_name, _options| strat_md5(filename, variant_name) }
76
+ files[filename].concat(variants.map { |variant_name, _options| strat_md5(filename, variant_name) })
75
77
  end
76
78
 
77
- Stratocaster::PurgeJob.perform_later(filenames) if filenames.any?
79
+ Stratocaster::PurgeJob.perform_later(files.to_h) if files.any?
78
80
  end
79
81
  end
80
82
  end
@@ -4,8 +4,12 @@ module Stratocaster
4
4
 
5
5
  self.priority = 3
6
6
 
7
- def perform(filenames)
8
- filenames.each { |filename| purge(filename) }
7
+ def perform(files)
8
+ files.each do |original, variants|
9
+ next if Stratocaster.referenced?(original)
10
+
11
+ [original, *variants].each { |filename| purge(filename) }
12
+ end
9
13
  end
10
14
 
11
15
  private
@@ -1,3 +1,3 @@
1
1
  module Stratocaster
2
- VERSION = "0.4.0".freeze
2
+ VERSION = "0.4.1".freeze
3
3
  end
data/lib/stratocaster.rb CHANGED
@@ -12,4 +12,15 @@ require "image_processing/vips"
12
12
 
13
13
  module Stratocaster
14
14
  cattr_accessor :config
15
+ mattr_accessor :attachments, default: Set.new
16
+
17
+ # Filenames are content hashes, so two records holding the same image share every stored file. A file is only
18
+ # safe to delete once no attachment column anywhere still points at it.
19
+ def self.referenced?(filename)
20
+ Rails.application.eager_load! unless Rails.application.config.eager_load
21
+
22
+ attachments.any? do |class_name, column|
23
+ class_name.safe_constantize&.unscoped&.exists?(column => filename)
24
+ end
25
+ end
15
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stratocaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johan Halse
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  requirements: []
71
- rubygems_version: 4.0.15
71
+ rubygems_version: 4.0.20
72
72
  specification_version: 4
73
73
  summary: Another flippin image uploader
74
74
  test_files: []