Files
grecia/db/dev_seeds/proposals.rb
Javi Martín 1290e2ecd3 Store files with both Paperclip and ActiveStorage
In order to migrate existing files from Paperclip to ActiveStorage, we
need Paperclip to find out the files associated to existing database
records. So we can't simply replace Paperclip with ActiveStorage.

That's why it's usually recommended [1] to first run the migration and
then replace Paperclip with ActiveStorage using two consecutive
deployments.

However, in our case we can't rely on two consecutive deployments
because we have to make an easy process so existing CONSUL installations
don't run into any issues. We can't just release version 1.4.0 and 1.5.0
and day and ask everyone to upgrade twice on the same day.

Instead, we're following a different plan:

* We're going to provide a Rake task (which will require Paperclip) to
  migrate existing files
* We still use Paperclip to generate link and image tags
* New files are handled using both Paperclip and ActiveStorage; that
  way, when we make the switch, we won't have to migrate them, and in
  the meantime they'll be accessible thanks to Paperclip
* After we make the switch, we'll update the `name` column in the active
  storage attachments tables in order to remove the `storage_` prefix

Regarding our handling of new files, the exception are cached
attachments. Since those attachments are temporary files used while
submitting a form and we have to delete them afterwards, we're only
handling them with Paperclip. We'll handle these ones in version 1.5.0.

Note the task creating the dev seeds was failing after these changes
with an `ActiveStorage::IntegrityError` exception because we were
opening some files without closing them. If the same file was attached
twice, it failed the second time.

We're solving it by closing the files with `File.open` and a block. Even
though we didn't get any errors, we're doing the same thing in the
`Attachable` concern because it's a good practice to close files after
we're done with them.

Also note we have to change the CKEditor Active Storage code so it's
compatible with Paperclip. In this case, I haven't been able to write a
test to confirm the attachment exists; I was getting the same
`ActiveStorage::IntegrityError` mentioned above.

Finally, we're updating the site customization image controller to use
`update` so the image and the attachment are updated within the same
transaction. This is also what we do in most controllers.

[1] https://www.youtube.com/watch?v=tZ_WNUytO9o
2021-09-24 13:39:15 +02:00

145 lines
6.0 KiB
Ruby

IMAGE_FILES = %w[
firdouss-ross-414668-unsplash_846x475.jpg
nathan-dumlao-496190-unsplash_713x475.jpg
steve-harvey-597760-unsplash_713x475.jpg
tim-mossholder-302931-unsplash_713x475.jpg
].map do |filename|
Rails.root.join("db",
"dev_seeds",
"images",
"proposals", filename)
end
def add_image_to(imageable)
# imageable should respond to #title & #author
File.open(IMAGE_FILES.sample) do |file|
imageable.image = Image.create!({
imageable: imageable,
title: imageable.title,
attachment: file,
user: imageable.author
})
end
imageable.save!
end
section "Creating Proposals" do
tags = Faker::Lorem.words(number: 25)
30.times do
title = Faker::Lorem.sentence(word_count: 3).truncate(60)
summary = Faker::Lorem.sentence(word_count: 3)
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join("</p><p>")}</p>"
proposal = Proposal.create!(author: author,
title: title,
summary: summary,
responsible_name: Faker::Name.name,
description: description,
created_at: rand((Time.current - 1.week)..Time.current),
tag_list: tags.sample(3).join(","),
geozone: Geozone.all.sample,
terms_of_service: "1",
published_at: Time.current)
random_locales.map do |locale|
Globalize.with_locale(locale) do
proposal.title = "Title for locale #{locale}"
proposal.summary = "Summary for locale #{locale}"
proposal.description = "<p>Description for locale #{locale}</p>"
proposal.save!
end
end
add_image_to proposal
end
end
section "Creating Archived Proposals" do
tags = Faker::Lorem.words(number: 25)
5.times do
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join("</p><p>")}</p>"
months_to_archive_proposals = Setting["months_to_archive_proposals"]
proposal = Proposal.create!(author: author,
title: Faker::Lorem.sentence(word_count: 3).truncate(60),
summary: Faker::Lorem.sentence(word_count: 3),
responsible_name: Faker::Name.name,
description: description,
tag_list: tags.sample(3).join(","),
geozone: Geozone.all.sample,
terms_of_service: "1",
created_at: months_to_archive_proposals.to_i.months.ago,
published_at: months_to_archive_proposals.to_i.months.ago)
random_locales.map do |locale|
Globalize.with_locale(locale) do
proposal.title = "Archived proposal title for locale #{locale}"
proposal.summary = "Archived proposal title summary for locale #{locale}"
proposal.description = "<p>Archived proposal description for locale #{locale}</p>"
proposal.save!
end
end
add_image_to proposal
end
end
section "Creating Successful Proposals" do
tags = Faker::Lorem.words(number: 25)
10.times do
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join("</p><p>")}</p>"
proposal = Proposal.create!(author: author,
title: Faker::Lorem.sentence(word_count: 3).truncate(60),
summary: Faker::Lorem.sentence(word_count: 3),
responsible_name: Faker::Name.name,
description: description,
created_at: rand((Time.current - 1.week)..Time.current),
tag_list: tags.sample(3).join(","),
geozone: Geozone.all.sample,
terms_of_service: "1",
cached_votes_up: Setting["votes_for_proposal_success"],
published_at: Time.current)
random_locales.map do |locale|
Globalize.with_locale(locale) do
proposal.title = "Successful proposal title for locale #{locale}"
proposal.summary = "Successful proposal title summary for locale #{locale}"
proposal.description = "<p>Successful proposal description for locale #{locale}</p>"
proposal.save!
end
end
add_image_to proposal
end
tags = Tag.where(kind: "category")
30.times do
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join("</p><p>")}</p>"
proposal = Proposal.create!(author: author,
title: Faker::Lorem.sentence(word_count: 4).truncate(60),
summary: Faker::Lorem.sentence(word_count: 3),
responsible_name: Faker::Name.name,
description: description,
created_at: rand((Time.current - 1.week)..Time.current),
tag_list: tags.sample(3).join(","),
geozone: Geozone.all.sample,
terms_of_service: "1",
published_at: Time.current)
random_locales.map do |locale|
Globalize.with_locale(locale) do
proposal.title = "Tagged proposal title for locale #{locale}"
proposal.summary = "Tagged proposal title summary for locale #{locale}"
proposal.description = "<p>Tagged proposal description for locale #{locale}</p>"
proposal.save!
end
end
add_image_to proposal
end
end
section "Creating proposal notifications" do
100.times do |i|
ProposalNotification.create!(title: "Proposal notification title #{i}",
body: "Proposal notification body #{i}",
author: User.all.sample,
proposal: Proposal.all.sample)
end
end