From f30032eee7f8d64e7444df15a33760b6da98596f Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Sun, 29 Jul 2018 15:51:24 +0800 Subject: [PATCH] Include image URLs in serialized enterprise for admin --- .../api/admin/enterprise_serializer.rb | 29 ++++++++++ .../admin/enterprise_serializer_spec.rb | 54 ++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/app/serializers/api/admin/enterprise_serializer.rb b/app/serializers/api/admin/enterprise_serializer.rb index e29954992f..348bb75461 100644 --- a/app/serializers/api/admin/enterprise_serializer.rb +++ b/app/serializers/api/admin/enterprise_serializer.rb @@ -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 diff --git a/spec/serializers/admin/enterprise_serializer_spec.rb b/spec/serializers/admin/enterprise_serializer_spec.rb index 0898b7d4a0..5bf40559d0 100644 --- a/spec/serializers/admin/enterprise_serializer_spec.rb +++ b/spec/serializers/admin/enterprise_serializer_spec.rb @@ -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