Include image URLs in serialized enterprise for admin

This commit is contained in:
Kristina Lim
2018-07-29 15:51:24 +08:00
parent 28792fc895
commit f30032eee7
2 changed files with 82 additions and 1 deletions

View File

@@ -5,11 +5,20 @@ class Api::Admin::EnterpriseSerializer < ActiveModel::Serializer
attributes :preferred_product_selection_from_inventory_only
attributes :owner, :contact, :users, :tag_groups, :default_tag_group
attributes :require_login, :allow_guest_orders, :allow_order_changes
attributes :logo, :promo_image
has_one :owner, serializer: Api::Admin::UserSerializer
has_many :users, serializer: Api::Admin::UserSerializer
has_one :address, serializer: Api::AddressSerializer
def logo
attachment_urls(object.logo, [:thumb, :small, :medium])
end
def promo_image
attachment_urls(object.promo_image, [:thumb, :medium, :large])
end
def tag_groups
object.tag_rules.prioritised.reject(&:is_default).each_with_object([]) do |tag_rule, tag_groups|
tag_group = find_match(tag_groups, tag_rule.preferred_customer_tags.split(",").map{ |t| { text: t } })
@@ -33,4 +42,24 @@ class Api::Admin::EnterpriseSerializer < ActiveModel::Serializer
end
return { tags: tags, rules: [] }
end
private
# Returns a hash of URLs for specified versions of an attachment.
#
# Example:
#
# attachment_urls(object.logo, [:thumb, :small, :medium])
# # {
# # thumb: LOGO_THUMB_URL,
# # small: LOGO_SMALL_URL,
# # medium: LOGO_MEDIUM_URL
# # }
def attachment_urls(attachment, versions)
return unless attachment.exists?
versions.each_with_object({}) do |version, urls|
urls[version] = attachment.url(version)
end
end
end