mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
I could have split this into several commits: * DRY direct linking to images. * Check S3 config before direct linking. * Just check if service is public instead of relying on name. Developers may copy a staging or production database or experiment with S3 storage. But when the S3 config is missing then calling `service` raises an ArgumentError due to a missing name. Now we only try to call `service` if the S3 config is present.
32 lines
822 B
Ruby
32 lines
822 B
Ruby
# frozen_string_literal: true
|
|
|
|
class ApplicationRecord < ActiveRecord::Base
|
|
include DelegateBelongsTo
|
|
include Spree::Core::Permalinks
|
|
include Spree::Preferences::Preferable
|
|
include Searchable
|
|
include ArelHelpers::ArelTable
|
|
include ArelHelpers::Aliases
|
|
include ArelHelpers::JoinAssociation
|
|
|
|
self.abstract_class = true
|
|
|
|
def self.image_service
|
|
ENV["S3_BUCKET"].present? ? :amazon_public : :local
|
|
end
|
|
|
|
# We might have a development environment without S3 but with a database
|
|
# dump pointing to S3 images. Accessing the service fails then.
|
|
def image_variant_url_for(variant)
|
|
if ENV["S3_BUCKET"].present? && variant.service.public?
|
|
variant.processed.url
|
|
else
|
|
url_for(variant)
|
|
end
|
|
end
|
|
|
|
def url_for(*args)
|
|
Rails.application.routes.url_helpers.url_for(*args)
|
|
end
|
|
end
|