Extract serializer helper method to service so the method isn't globally available

This commit is contained in:
Matt-Yorkley
2020-06-18 16:42:58 +02:00
parent 75e57e5c2c
commit 5338e782f8
3 changed files with 11 additions and 10 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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