mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-02 06:51:40 +00:00
We configured Paperclip to convert images to JPG in some cases but I omitted that here because we don't need it. If an image is better represented as PNG or another format then the user should be able to choose that. Some specs were also testing the generated URL but the Active Storage URL doesn't contain a style name anymore and it's not helpful to test the URL.
102 lines
3.7 KiB
Ruby
102 lines
3.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Api
|
|
module Admin
|
|
class EnterpriseSerializer < ActiveModel::Serializer
|
|
attributes :name, :id, :is_primary_producer, :is_distributor, :sells, :category, :permalink,
|
|
:payment_method_ids, :shipping_method_ids, :producer_profile_only,
|
|
:long_description, :preferred_product_selection_from_inventory_only,
|
|
:preferred_shopfront_message, :preferred_shopfront_closed_message,
|
|
:preferred_shopfront_taxon_order, :preferred_shopfront_producer_order,
|
|
:preferred_shopfront_order_cycle_order, :show_customer_names_to_suppliers,
|
|
:preferred_shopfront_product_sorting_method, :owner, :contact, :users, :tag_groups,
|
|
:default_tag_group, :require_login, :allow_guest_orders, :allow_order_changes,
|
|
:logo, :promo_image, :terms_and_conditions,
|
|
:terms_and_conditions_file_name, :terms_and_conditions_updated_at,
|
|
:preferred_invoice_order_by_supplier, :preferred_product_low_stock_display,
|
|
:visible
|
|
|
|
has_one :owner, serializer: Api::Admin::UserSerializer
|
|
has_many :users, serializer: Api::Admin::UserSerializer
|
|
has_one :address, serializer: Api::AddressSerializer
|
|
has_one :business_address, serializer: Api::AddressSerializer
|
|
|
|
def logo
|
|
attachment_urls(object.logo, Enterprise::LOGO_SIZES)
|
|
end
|
|
|
|
def promo_image
|
|
attachment_urls(object.promo_image, Enterprise::PROMO_IMAGE_SIZES)
|
|
end
|
|
|
|
def terms_and_conditions
|
|
return unless object.terms_and_conditions.attached?
|
|
|
|
Rails.application.routes.url_helpers.
|
|
url_for(object.terms_and_conditions)
|
|
end
|
|
|
|
def terms_and_conditions_file_name
|
|
object.terms_and_conditions_blob&.filename
|
|
end
|
|
|
|
def terms_and_conditions_updated_at
|
|
object.terms_and_conditions_blob&.created_at&.to_s
|
|
end
|
|
|
|
def tag_groups
|
|
prioritized_tag_rules.each_with_object([]) do |tag_rule, tag_groups|
|
|
tag_group = find_match(tag_groups, tag_rule.preferred_customer_tags.
|
|
split(",").
|
|
map{ |t| { text: t } })
|
|
if tag_group[:rules].blank?
|
|
tag_groups << tag_group
|
|
tag_group[:position] = tag_groups.count
|
|
end
|
|
tag_group[:rules] << Api::Admin::TagRuleSerializer.new(tag_rule).serializable_hash
|
|
end
|
|
end
|
|
|
|
def default_tag_group
|
|
default_rules = object.tag_rules.select(&:is_default)
|
|
serialized_rules =
|
|
ActiveModel::ArraySerializer.new(default_rules,
|
|
each_serializer: Api::Admin::TagRuleSerializer)
|
|
{ tags: [], rules: serialized_rules }
|
|
end
|
|
|
|
def find_match(tag_groups, tags)
|
|
tag_groups.each do |tag_group|
|
|
return tag_group if tag_group[:tags].length == tags.length &&
|
|
(tag_group[:tags] & tags) == tag_group[:tags]
|
|
end
|
|
{ tags: tags, rules: [] }
|
|
end
|
|
|
|
private
|
|
|
|
def prioritized_tag_rules
|
|
object.tag_rules.prioritised.reject(&:is_default)
|
|
end
|
|
|
|
# Returns a hash of URLs for specified versions of an attachment.
|
|
#
|
|
# Example result:
|
|
#
|
|
# {
|
|
# thumb: LOGO_THUMB_URL,
|
|
# small: LOGO_SMALL_URL,
|
|
# medium: LOGO_MEDIUM_URL
|
|
# }
|
|
def attachment_urls(attachment, styles)
|
|
return unless attachment.attached?
|
|
|
|
styles.transform_values do |transformation|
|
|
Rails.application.routes.url_helpers.
|
|
url_for(attachment.variant(transformation))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|