Files
fizzy/app/models/concerns/attachments.rb
T
Mike Dalessio ef6198d394 Make sure the image variants we use are preprocessed
to avoid implicitly creating a variant during a GET to a read replica,
which will raise an exception and cause the image to fail to load.

This feels pretty brittle, to be honest, and we'll need to be careful
in the future about creating and using new variants; we will probably
need to backfill the new variant for existing images because we can't
guarantee that they will be created implicitly/lazily if the GET lands
on the replica.

ref: https://fizzy.37signals.com/5986089/collections/7/cards/942
2025-07-18 15:43:31 -04:00

39 lines
1.4 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
module Attachments
extend ActiveSupport::Concern
# The variants we use must all declared here so that we can preprocess them.
#
# If they are not preprocessed, then Rails will attempt to transform the image on-the-fly when
# they are first viewed, which may be on the read replica where writing to the database is not
# allowed. Chaos will ensue if that happens.
#
# These variants are patched into ActionText::RichText in config/initializers/action_text.rb
VARIANTS = {
# vipsthumbnail used to create sized image variants has a intent setting to manage colors during
# resize. By setting an invalid intent value the gif-incompatible intent filtering is skipped and
# the gif can be rendered with all its frame intact.
#
# Only `n` is accepted as an override, using the full parameter name `intent` doesnt work.
#
# This was cargo-culted from know-it-all and I imagine it may be fixed at some point.
small: { loader: { n: -1 }, resize_to_limit: [ 800, 600 ] },
large: { loader: { n: -1 }, resize_to_limit: [ 1024, 768 ] }
}
def attachments
rich_text_record&.embeds || []
end
def has_attachments?
attachments.any?
end
private
def rich_text_record
@rich_text_record ||= begin
association = self.class.reflect_on_all_associations(:has_one).find { it.klass == ActionText::RichText }
public_send(association.name)
end
end
end