From 5338e782f827347986a89e9608cfa7715cf26aed Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 18 Jun 2020 16:42:58 +0200 Subject: [PATCH] Extract serializer helper method to service so the method isn't globally available --- app/helpers/serializer_helper.rb | 7 ------- .../api/enterprise_shopfront_list_serializer.rb | 4 +--- app/services/image_path_service.rb | 10 ++++++++++ 3 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 app/services/image_path_service.rb diff --git a/app/helpers/serializer_helper.rb b/app/helpers/serializer_helper.rb index 091c93d145..f30d998f73 100644 --- a/app/helpers/serializer_helper.rb +++ b/app/helpers/serializer_helper.rb @@ -13,11 +13,4 @@ module SerializerHelper (serializer_attributes & model_attributes).map { |attr| "#{model.table_name}.#{attr}" } end - - # Since Rails 4 has adjusted the way assets paths are handled, we have to access certain - # asset-based helpers like this, when outside of a view or controller context. - # See: https://stackoverflow.com/a/16609815 - def image_path(path) - ActionController::Base.helpers.image_path(path) - end end diff --git a/app/serializers/api/enterprise_shopfront_list_serializer.rb b/app/serializers/api/enterprise_shopfront_list_serializer.rb index eb82b96430..874e3516d5 100644 --- a/app/serializers/api/enterprise_shopfront_list_serializer.rb +++ b/app/serializers/api/enterprise_shopfront_list_serializer.rb @@ -1,8 +1,6 @@ # Represents the minimum details of an Enterprise when all shopfronts are being listed module Api class EnterpriseShopfrontListSerializer < ActiveModel::Serializer - include SerializerHelper - attributes :name, :id, :latitude, :longitude, :is_primary_producer, :is_distributor, :path, :icon, :icon_font, :producer_icon_font, :address_id, :sells, :permalink @@ -21,7 +19,7 @@ module Api producer_shop: "map_003-producer-shop.svg", producer: "map_001-producer-only.svg", } - image_path icons[enterprise.category] + ImagePathService.image_path(icons[enterprise.category]) end def icon_font diff --git a/app/services/image_path_service.rb b/app/services/image_path_service.rb new file mode 100644 index 0000000000..b7cf5815db --- /dev/null +++ b/app/services/image_path_service.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class ImagePathService + # Since Rails 4 has adjusted the way assets paths are handled, we have to access certain + # asset-based helpers like this when outside of a view or controller context. + # See: https://stackoverflow.com/a/16609815 + def self.image_path(path) + ActionController::Base.helpers.image_path(path) + end +end