effective_assets 1.11.5 → 1.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/jobs/effective/asset_replacer_job.rb +17 -0
- data/app/jobs/effective/snippet_replacer_job.rb +17 -0
- data/app/models/effective/asset_replacer.rb +156 -0
- data/app/models/effective/snippet_replacer.rb +67 -0
- data/lib/effective_assets/version.rb +1 -1
- data/lib/tasks/replace_effective_assets.rake +5 -0
- data/lib/tasks/replace_effective_snippets.rake +5 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b5a4d4ffc1fe6f0e98998af6b9c575c60b56430e9d90d40d82bc73efc7f0b43
|
4
|
+
data.tar.gz: 96ae371e5b1a1d4c78056d1beba02fcb32ab934a7a35367347287efde86345e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05bd4a11642356cefd2488cf4bc7b7712519863e346b84b7f86ebdf0ec36c0cb4a439ad9bd6222d3ba6ec27041a26abfa636ab52ff891334cf14405f70d09586
|
7
|
+
data.tar.gz: 818fb0813001f652a07339dad3ec5f92227990d92c29a07557ecaf883961c63a9f169349815bf157f988e4fb628cb556dc41c4c14f470df6cc41ceb48e05b322
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Effective
|
2
|
+
class AssetReplacerJob < ::ApplicationJob
|
3
|
+
|
4
|
+
queue_as :default
|
5
|
+
|
6
|
+
if defined?(Sidekiq)
|
7
|
+
# The retry setting works. But none of these waits seem to. Still getting exponential backoff.
|
8
|
+
sidekiq_options retry: 2, retry_in: 10, wait: 10
|
9
|
+
sidekiq_retry_in { |count, exception| 10 }
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform(attachment, box)
|
13
|
+
Effective::AssetReplacer.new.replace_attachment!(attachment, box)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Effective
|
2
|
+
class SnippetReplacerJob < ::ApplicationJob
|
3
|
+
|
4
|
+
queue_as :default
|
5
|
+
|
6
|
+
if defined?(Sidekiq)
|
7
|
+
# The retry setting works. But none of these waits seem to. Still getting exponential backoff.
|
8
|
+
sidekiq_options retry: 2, retry_in: 10, wait: 10
|
9
|
+
sidekiq_retry_in { |count, exception| 10 }
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform(region)
|
13
|
+
Effective::SnippetReplacer.new.replace_region!(region)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,156 @@
|
|
1
|
+
# Looks at the Effective::Asset and Effective::Attachments and converts to ActiveStorage
|
2
|
+
#
|
3
|
+
# 1. Put in the has_attached_files to each model to be upgraded
|
4
|
+
# 2. Remove the acts_as_asset_box
|
5
|
+
|
6
|
+
require 'timeout'
|
7
|
+
|
8
|
+
module Effective
|
9
|
+
class AssetReplacer
|
10
|
+
include ActiveStorage::Blob::Analyzable
|
11
|
+
|
12
|
+
BATCH_SIZE = 500
|
13
|
+
TIMEOUT = 120
|
14
|
+
|
15
|
+
def replace!(skip_existing: false)
|
16
|
+
verify!
|
17
|
+
|
18
|
+
attachments = attachments_to_process().to_a
|
19
|
+
|
20
|
+
while(true)
|
21
|
+
wait_for_active_job!
|
22
|
+
|
23
|
+
puts "\nEnqueuing #{attachments.length} attachments with ids #{attachments.first.id} to #{attachments.last.id}"
|
24
|
+
|
25
|
+
attachments.each do |attachment|
|
26
|
+
asset = attachment.asset
|
27
|
+
attachable = attachment.attachable
|
28
|
+
next if asset.blank? || attachable.blank?
|
29
|
+
|
30
|
+
box = attachment.box.singularize
|
31
|
+
boxes = attachment.box
|
32
|
+
|
33
|
+
one = attachable.respond_to?(box) && attachable.send(box).kind_of?(ActiveStorage::Attached::One)
|
34
|
+
many = attachable.respond_to?(boxes) && attachable.send(boxes).kind_of?(ActiveStorage::Attached::Many)
|
35
|
+
box = (one ? box : boxes)
|
36
|
+
|
37
|
+
if skip_existing
|
38
|
+
existing = Array(attachable.send(box))
|
39
|
+
|
40
|
+
if existing.any? { |obj| obj.respond_to?(:filename) && obj.filename.to_s == asset.file_name }
|
41
|
+
puts("Skipping existing #{attachable.class.name} #{attachable.id} #{box} #{asset.file_name}.")
|
42
|
+
next
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
Effective::AssetReplacerJob.perform_later(attachment, box)
|
47
|
+
end
|
48
|
+
|
49
|
+
attachments = attachments_to_process().where.not(id: attachments.map(&:id))
|
50
|
+
break if attachments.to_a.blank?
|
51
|
+
|
52
|
+
GC.start
|
53
|
+
end
|
54
|
+
|
55
|
+
puts "\nAll Done. Have a great day."
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
# This is called on the background by the AssetReplacerJob
|
60
|
+
def replace_attachment!(attachment, box)
|
61
|
+
raise('expected an Effective::Attachment') unless attachment.kind_of?(Effective::Attachment)
|
62
|
+
puts("Processing attachment ##{attachment.id}")
|
63
|
+
|
64
|
+
asset = attachment.asset
|
65
|
+
attachable = attachment.attachable
|
66
|
+
|
67
|
+
attachable.replacing_asset = true if attachable.respond_to?(:replacing_asset=)
|
68
|
+
|
69
|
+
Timeout.timeout(TIMEOUT) do
|
70
|
+
attachable.send(box).attach(
|
71
|
+
io: URI.open(asset.url),
|
72
|
+
filename: asset.file_name,
|
73
|
+
content_type: asset.content_type.presence,
|
74
|
+
identify: (asset.content_type.blank?)
|
75
|
+
)
|
76
|
+
|
77
|
+
attachment.update_column(:replaced, true)
|
78
|
+
end
|
79
|
+
|
80
|
+
true
|
81
|
+
end
|
82
|
+
|
83
|
+
def verify!
|
84
|
+
raise('expected effective assets') unless defined?(Effective::Asset)
|
85
|
+
raise('expected active storage') unless defined?(ActiveStorage)
|
86
|
+
|
87
|
+
unless Effective::Attachment.new.respond_to?(:replaced?)
|
88
|
+
raise('please add a replaced boolean to Effective::Attachment. add_column :attachments, :replaced, :boolean')
|
89
|
+
end
|
90
|
+
|
91
|
+
(Effective::Attachment.all.pluck(:attachable_type, :box).uniq).each do |name, boxes|
|
92
|
+
next if name.blank? || boxes.blank?
|
93
|
+
|
94
|
+
box = boxes.singularize
|
95
|
+
|
96
|
+
klass = name.safe_constantize
|
97
|
+
raise("invalid class #{klass}") unless klass.present?
|
98
|
+
|
99
|
+
instance = klass.new
|
100
|
+
|
101
|
+
if instance.respond_to?(:effective_assets)
|
102
|
+
raise("please remove acts_as_asset_box() from #{klass.name}")
|
103
|
+
end
|
104
|
+
|
105
|
+
unless instance.respond_to?(box) || instance.respond_to?(boxes)
|
106
|
+
raise("expected #{klass.name} to has_one_attached :#{box} or has_many_attached :#{boxes}")
|
107
|
+
end
|
108
|
+
|
109
|
+
one = instance.respond_to?(box) && instance.send(box).kind_of?(ActiveStorage::Attached::One)
|
110
|
+
many = instance.respond_to?(boxes) && instance.send(boxes).kind_of?(ActiveStorage::Attached::Many)
|
111
|
+
|
112
|
+
unless one.present? || many.present?
|
113
|
+
raise("expected #{klass.name} to has_one_attached :#{box} or has_many_attached :#{boxes}")
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
puts 'All attachment classes verified.'
|
118
|
+
|
119
|
+
true
|
120
|
+
end
|
121
|
+
|
122
|
+
def reset!
|
123
|
+
Effective::Attachment.update_all(replaced: false)
|
124
|
+
end
|
125
|
+
|
126
|
+
private
|
127
|
+
|
128
|
+
def wait_for_active_job!
|
129
|
+
while(true)
|
130
|
+
if(jobs = enqueued_jobs_count) > (BATCH_SIZE / 10)
|
131
|
+
print '.'; sleep(3)
|
132
|
+
else
|
133
|
+
break
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
# The last BATCH_SIZE attachments
|
139
|
+
def attachments_to_process
|
140
|
+
Effective::Attachment.all
|
141
|
+
.includes(:asset)
|
142
|
+
.where(replaced: [nil, false])
|
143
|
+
.reorder(id: :desc)
|
144
|
+
.limit(BATCH_SIZE)
|
145
|
+
end
|
146
|
+
|
147
|
+
def enqueued_jobs_count
|
148
|
+
if Rails.application.config.active_job.queue_adapter == :sidekiq
|
149
|
+
Sidekiq::Stats.new.enqueued.to_i
|
150
|
+
else
|
151
|
+
ActiveJob::Base.queue_adapter.enqueued_jobs.count
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# Replaces the [snippet_x] in all effective regions with static content
|
2
|
+
|
3
|
+
module Effective
|
4
|
+
class SnippetReplacer
|
5
|
+
include ActiveStorage::Blob::Analyzable
|
6
|
+
include ActionView::Helpers::UrlHelper
|
7
|
+
include ActionView::Helpers::AssetTagHelper
|
8
|
+
|
9
|
+
def replace!
|
10
|
+
raise('expected effective regions') unless defined?(Effective::Region)
|
11
|
+
raise('expected effective assets') unless defined?(Effective::Asset)
|
12
|
+
raise('expected active storage') unless defined?(ActiveStorage)
|
13
|
+
|
14
|
+
Effective::Region.with_snippets.find_each do |region|
|
15
|
+
Effective::SnippetReplacerJob.perform_later(region)
|
16
|
+
end
|
17
|
+
|
18
|
+
puts 'All Done. Background jobs are running. Have a great day.'
|
19
|
+
true
|
20
|
+
end
|
21
|
+
|
22
|
+
def replace_region!(region)
|
23
|
+
region.snippet_objects.each do |snippet|
|
24
|
+
print('.')
|
25
|
+
|
26
|
+
begin
|
27
|
+
case snippet.class.name
|
28
|
+
when 'Effective::Snippets::EffectiveAsset'
|
29
|
+
replace_effective_asset(region, snippet)
|
30
|
+
else
|
31
|
+
raise("unsupported snippet: #{snippet.class.name}")
|
32
|
+
end
|
33
|
+
rescue => e
|
34
|
+
puts "\nError: #{e}\n"
|
35
|
+
remove_snippet(region, snippet)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
region.save!
|
40
|
+
end
|
41
|
+
|
42
|
+
def replace_effective_asset(region, snippet)
|
43
|
+
asset = snippet.asset
|
44
|
+
raise("Effective:Asset id=#{snippet.asset_id || 'none'} does not exist") unless asset.present?
|
45
|
+
|
46
|
+
blob = ActiveStorage::Blob.create_and_upload!(io: URI.open(asset.url), filename: asset.file_name)
|
47
|
+
url = Rails.application.routes.url_helpers.rails_blob_url(blob, only_path: true)
|
48
|
+
|
49
|
+
content = if asset.image?
|
50
|
+
image_tag(url, class: snippet.html_class, alt: snippet.link_title)
|
51
|
+
else
|
52
|
+
link_to(snippet.link_title, url, class: snippet.html_class, title: snippet.link_title)
|
53
|
+
end
|
54
|
+
|
55
|
+
region.content.sub!("[#{snippet.id}]", content.to_s)
|
56
|
+
region.snippets.delete(snippet.id)
|
57
|
+
|
58
|
+
true
|
59
|
+
end
|
60
|
+
|
61
|
+
def remove_snippet(region, snippet)
|
62
|
+
region.content.sub!("[#{snippet.id}]", '')
|
63
|
+
region.snippets.delete(snippet.id)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: effective_assets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Code and Effect
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -165,13 +165,17 @@ files:
|
|
165
165
|
- app/controllers/effective/s3_uploads_controller.rb
|
166
166
|
- app/helpers/effective_assets_helper.rb
|
167
167
|
- app/helpers/effective_assets_s3_helper.rb
|
168
|
+
- app/jobs/effective/asset_replacer_job.rb
|
168
169
|
- app/jobs/effective/process_with_active_job.rb
|
169
170
|
- app/jobs/effective/process_with_sucker_punch_job.rb
|
171
|
+
- app/jobs/effective/snippet_replacer_job.rb
|
170
172
|
- app/models/concerns/acts_as_asset_box.rb
|
171
173
|
- app/models/effective/access_denied.rb
|
172
174
|
- app/models/effective/asset.rb
|
175
|
+
- app/models/effective/asset_replacer.rb
|
173
176
|
- app/models/effective/attachment.rb
|
174
177
|
- app/models/effective/iframe_upload.rb
|
178
|
+
- app/models/effective/snippet_replacer.rb
|
175
179
|
- app/models/effective/snippets/effective_asset.rb
|
176
180
|
- app/uploaders/effective_assets_uploader.rb
|
177
181
|
- app/uploaders/test_asset_uploader.rb
|
@@ -201,6 +205,8 @@ files:
|
|
201
205
|
- lib/inputs/asset_box_input.rb
|
202
206
|
- lib/inputs/asset_box_simple_form_input.rb
|
203
207
|
- lib/tasks/effective_assets_tasks.rake
|
208
|
+
- lib/tasks/replace_effective_assets.rake
|
209
|
+
- lib/tasks/replace_effective_snippets.rake
|
204
210
|
- lib/validators/asset_box_length_validator.rb
|
205
211
|
- lib/validators/asset_box_presence_validator.rb
|
206
212
|
homepage: https://github.com/code-and-effect/effective_assets
|