mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
Include image URLs in serialized enterprise for admin
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
require 'spec_helper'
|
||||
require "spec_helper"
|
||||
|
||||
describe Api::Admin::EnterpriseSerializer do
|
||||
let(:enterprise) { create(:distributor_enterprise) }
|
||||
@@ -6,4 +6,56 @@ describe Api::Admin::EnterpriseSerializer do
|
||||
serializer = Api::Admin::EnterpriseSerializer.new enterprise
|
||||
serializer.to_json.should match enterprise.name
|
||||
end
|
||||
|
||||
context "for logo" do
|
||||
let(:enterprise) { create(:distributor_enterprise, logo: image) }
|
||||
|
||||
context "when there is a logo" do
|
||||
let(:image) do
|
||||
image_path = File.open(Rails.root.join("app", "assets", "images", "logo-black.png"))
|
||||
Rack::Test::UploadedFile.new(image_path, "image/png")
|
||||
end
|
||||
|
||||
it "includes URLs of image versions" do
|
||||
serializer = Api::Admin::EnterpriseSerializer.new(enterprise)
|
||||
expect(serializer.as_json[:logo]).to_not be_blank
|
||||
expect(serializer.as_json[:logo][:medium]).to match(/logo-black.png/)
|
||||
end
|
||||
end
|
||||
|
||||
context "when there is no logo" do
|
||||
let(:image) { nil }
|
||||
|
||||
it "includes URLs of image versions" do
|
||||
serializer = Api::Admin::EnterpriseSerializer.new(enterprise)
|
||||
expect(serializer.as_json[:logo]).to be_blank
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "for promo image" do
|
||||
let(:enterprise) { create(:distributor_enterprise, promo_image: image) }
|
||||
|
||||
context "when there is a promo image" do
|
||||
let(:image) do
|
||||
image_path = File.open(Rails.root.join("app", "assets", "images", "logo-black.png"))
|
||||
Rack::Test::UploadedFile.new(image_path, "image/png")
|
||||
end
|
||||
|
||||
it "includes URLs of image versions" do
|
||||
serializer = Api::Admin::EnterpriseSerializer.new(enterprise)
|
||||
expect(serializer.as_json[:promo_image]).to_not be_blank
|
||||
expect(serializer.as_json[:promo_image][:medium]).to match(/logo-black.jpg/)
|
||||
end
|
||||
end
|
||||
|
||||
context "when there is no promo image" do
|
||||
let(:image) { nil }
|
||||
|
||||
it "includes URLs of image versions" do
|
||||
serializer = Api::Admin::EnterpriseSerializer.new(enterprise)
|
||||
expect(serializer.as_json[:promo_image]).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user