Skip to content

Commit

Permalink
Reuse blobs in migration and for new versions of Exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
Dantemss committed Feb 3, 2021
1 parent 71dff00 commit 3308a20
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 1 addition & 4 deletions app/models/exercise.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class Exercise < ApplicationRecord

EQUALITY_ASSOCIATIONS = [
:images,
:logic,
:tags,
publication: [
Expand All @@ -27,7 +26,6 @@ class Exercise < ApplicationRecord

# deep_clone does not iterate through hashes, so each hash must have only 1 key
NEW_VERSION_DUPED_ASSOCIATIONS = [
:images,
:logic,
:tags,
{
Expand All @@ -43,7 +41,6 @@ class Exercise < ApplicationRecord
:answers,
{
collaborator_solutions: [
:images,
:logic,
:stylings
]
Expand All @@ -60,7 +57,6 @@ class Exercise < ApplicationRecord
]

CACHEABLE_ASSOCIATIONS = [
:images,
:logic,
:tags,
publication: [
Expand Down Expand Up @@ -119,6 +115,7 @@ def new_version
nv.publication.yanked_at = nil
nv.publication.embargoed_until = nil
nv.publication.major_change = false
images.each { |image| nv.images.attach image.blob }
nv
end

Expand Down
11 changes: 9 additions & 2 deletions db/migrate/20210202184008_copy_attachments_to_active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ def up

exercise.attachments.each do |attachment|
filename = attachment.read_attribute :asset
tempfile = open("#{ASSET_BASE}/#{filename}") rescue next # Asset not found etc

exercise.images.attach io: tempfile, filename: filename
blob = ActiveStorage::Blob.find_by filename: filename

if blob.nil?
tempfile = open("#{ASSET_BASE}/#{filename}") rescue next # HTTP errors etc

exercise.images.attach io: tempfile, filename: filename
else
exercise.images.attach blob
end
end

exercise.save!
Expand Down

0 comments on commit 3308a20

Please sign in to comment.