From 2143122a303e610cdce79d370a7bf4bbb7f20daf Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 18 Jun 2020 12:28:04 +0200 Subject: [PATCH 1/6] Add helper method for correctly generating with fingerprints in Serializers --- app/helpers/serializer_helper.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/helpers/serializer_helper.rb b/app/helpers/serializer_helper.rb index f30d998f73f..091c93d145c 100644 --- a/app/helpers/serializer_helper.rb +++ b/app/helpers/serializer_helper.rb @@ -13,4 +13,11 @@ def required_attributes(model, serializer) (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 From 551daaadea62d1c98b2b20d4a30ebaa7f7d61a91 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 18 Jun 2020 13:53:58 +0200 Subject: [PATCH 2/6] Use #image_path correctly on map marker icons --- .../api/enterprise_shopfront_list_serializer.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/serializers/api/enterprise_shopfront_list_serializer.rb b/app/serializers/api/enterprise_shopfront_list_serializer.rb index dd83ce43237..eb82b964301 100644 --- a/app/serializers/api/enterprise_shopfront_list_serializer.rb +++ b/app/serializers/api/enterprise_shopfront_list_serializer.rb @@ -1,6 +1,8 @@ # 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 @@ -13,13 +15,13 @@ def path def icon icons = { - hub: "/assets/map_005-hub.svg", - hub_profile: "/assets/map_006-hub-profile.svg", - producer_hub: "/assets/map_005-hub.svg", - producer_shop: "/assets/map_003-producer-shop.svg", - producer: "/assets/map_001-producer-only.svg", + hub: "map_005-hub.svg", + hub_profile: "map_006-hub-profile.svg", + producer_hub: "map_005-hub.svg", + producer_shop: "map_003-producer-shop.svg", + producer: "map_001-producer-only.svg", } - icons[enterprise.category] + image_path icons[enterprise.category] end def icon_font From 9a39cbbbc5a70d0aaeca23d9bbb781dfc5dfc00b Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 18 Jun 2020 13:36:50 +0200 Subject: [PATCH 3/6] Convert MapConfiguration service to .js.erb.coffee --- ...ap_configuration.js.coffee => map_configuration.js.erb.coffee} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/assets/javascripts/darkswarm/services/{map_configuration.js.coffee => map_configuration.js.erb.coffee} (100%) diff --git a/app/assets/javascripts/darkswarm/services/map_configuration.js.coffee b/app/assets/javascripts/darkswarm/services/map_configuration.js.erb.coffee similarity index 100% rename from app/assets/javascripts/darkswarm/services/map_configuration.js.coffee rename to app/assets/javascripts/darkswarm/services/map_configuration.js.erb.coffee From 75e57e5c2c3859ae1592c538905f408cdf38b13d Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 18 Jun 2020 13:41:32 +0200 Subject: [PATCH 4/6] Call #image_path on map cluster icon --- .../darkswarm/services/map_configuration.js.erb.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/darkswarm/services/map_configuration.js.erb.coffee b/app/assets/javascripts/darkswarm/services/map_configuration.js.erb.coffee index 93e4104f0ae..61598c414c6 100644 --- a/app/assets/javascripts/darkswarm/services/map_configuration.js.erb.coffee +++ b/app/assets/javascripts/darkswarm/services/map_configuration.js.erb.coffee @@ -4,7 +4,7 @@ Darkswarm.factory "MapConfiguration", -> center: latitude: -37.4713077 longitude: 144.7851531 - cluster_icon: 'assets/map_009-cluster.svg' + cluster_icon: "<%= image_path('map_009-cluster.svg') %>" zoom: 12 additional_options: # mapTypeId: 'satellite' 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 5/6] 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 091c93d145c..f30d998f73f 100644 --- a/app/helpers/serializer_helper.rb +++ b/app/helpers/serializer_helper.rb @@ -13,11 +13,4 @@ def required_attributes(model, serializer) (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 eb82b964301..874e3516d50 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 @@ def icon 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 00000000000..b7cf5815dba --- /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 From 130f639b61db51ccf74ea205ae58ae80201d8f55 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 18 Jun 2020 17:14:50 +0200 Subject: [PATCH 6/6] Rename service and use #call --- app/serializers/api/enterprise_shopfront_list_serializer.rb | 2 +- .../{image_path_service.rb => image_path_generator.rb} | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename app/services/{image_path_service.rb => image_path_generator.rb} (85%) diff --git a/app/serializers/api/enterprise_shopfront_list_serializer.rb b/app/serializers/api/enterprise_shopfront_list_serializer.rb index 874e3516d50..0dccfaa1c2f 100644 --- a/app/serializers/api/enterprise_shopfront_list_serializer.rb +++ b/app/serializers/api/enterprise_shopfront_list_serializer.rb @@ -19,7 +19,7 @@ def icon producer_shop: "map_003-producer-shop.svg", producer: "map_001-producer-only.svg", } - ImagePathService.image_path(icons[enterprise.category]) + ImagePathGenerator.call(icons[enterprise.category]) end def icon_font diff --git a/app/services/image_path_service.rb b/app/services/image_path_generator.rb similarity index 85% rename from app/services/image_path_service.rb rename to app/services/image_path_generator.rb index b7cf5815dba..48c9ee93d14 100644 --- a/app/services/image_path_service.rb +++ b/app/services/image_path_generator.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -class ImagePathService +class ImagePathGenerator # 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) + def self.call(path) ActionController::Base.helpers.image_path(path) end end