From d3ca4820f8551e009e1b5374dcf41ab50e580981 Mon Sep 17 00:00:00 2001 From: Nevelito Date: Fri, 17 Jan 2025 13:55:22 +0100 Subject: [PATCH 1/3] Add mapkick options to location field --- .../avo/fields/location_field/show_component.html.erb | 2 +- lib/avo/fields/location_field.rb | 2 ++ spec/dummy/app/avo/resources/city.rb | 9 ++++++++- spec/system/avo/location_field_spec.rb | 8 +++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/components/avo/fields/location_field/show_component.html.erb b/app/components/avo/fields/location_field/show_component.html.erb index acd773d788..e67862e231 100644 --- a/app/components/avo/fields/location_field/show_component.html.erb +++ b/app/components/avo/fields/location_field/show_component.html.erb @@ -1,6 +1,6 @@ <%= field_wrapper **field_wrapper_args do %> <% if field.value_present? %> - <%= js_map [{latitude: field.value[0], longitude: field.value[1]}], id: "location-map", zoom: field.zoom, controls: true %> + <%= js_map [{latitude: field.value[0], longitude: field.value[1]}], id: "location-map", zoom: field.zoom, controls: true, **field.mapkick_options %> <% else %> — <% end %> diff --git a/lib/avo/fields/location_field.rb b/lib/avo/fields/location_field.rb index e92954c1c0..45a492d125 100644 --- a/lib/avo/fields/location_field.rb +++ b/lib/avo/fields/location_field.rb @@ -3,6 +3,7 @@ module Avo module Fields class LocationField < BaseField + attr_reader :mapkick_options attr_reader :stored_as, :zoom def initialize(id, **args, &block) @@ -10,6 +11,7 @@ def initialize(id, **args, &block) super(id, **args, &block) @stored_as = args[:stored_as].present? ? args[:stored_as] : nil # You can pass it an array of db columns [:latitude, :longitude] + @mapkick_options = args[:mapkick_options].presence || {} @zoom = args[:zoom].present? ? args[:zoom].to_i : 15 end diff --git a/spec/dummy/app/avo/resources/city.rb b/spec/dummy/app/avo/resources/city.rb index fe94612476..bd1d3d296f 100644 --- a/spec/dummy/app/avo/resources/city.rb +++ b/spec/dummy/app/avo/resources/city.rb @@ -36,7 +36,14 @@ class Avo::Resources::City < Avo::BaseResource def base_fields field :id, as: :id - field :coordinates, as: :location, stored_as: [:latitude, :longitude] + field :coordinates, + as: :location, + stored_as: [:latitude, :longitude], + mapkick_options: { + style: "mapbox://styles/mapbox/satellite-v9", + controls: true, + markers: {color: "#000"} + } field :city_center_area, as: :area, geometry: :polygon, diff --git a/spec/system/avo/location_field_spec.rb b/spec/system/avo/location_field_spec.rb index 7ec9a3c4ab..e42b8c0866 100644 --- a/spec/system/avo/location_field_spec.rb +++ b/spec/system/avo/location_field_spec.rb @@ -58,12 +58,18 @@ let!(:city) { create :city, latitude: 48.8584, longitude: 2.2945 } context "show" do - it "renders a map" do + before do Avo::Fields::LocationField::ShowComponent.any_instance.stub(:js_map).and_return("map_content_here") visit "/admin/resources/cities/#{city.id}" + end + it "renders a map" do expect(page).to have_text("map_content_here") end + + it "applies mapkick options correctly" do + expect(page).to have_css("div#location-map [data-marker-color='#000']") + end end context "edit" do From dd1c7c3f7d706612dba56a919fb3c076056b3987 Mon Sep 17 00:00:00 2001 From: Nevelito Date: Fri, 17 Jan 2025 14:57:11 +0100 Subject: [PATCH 2/3] Delete spec --- spec/system/avo/location_field_spec.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/spec/system/avo/location_field_spec.rb b/spec/system/avo/location_field_spec.rb index e42b8c0866..7ec9a3c4ab 100644 --- a/spec/system/avo/location_field_spec.rb +++ b/spec/system/avo/location_field_spec.rb @@ -58,18 +58,12 @@ let!(:city) { create :city, latitude: 48.8584, longitude: 2.2945 } context "show" do - before do + it "renders a map" do Avo::Fields::LocationField::ShowComponent.any_instance.stub(:js_map).and_return("map_content_here") visit "/admin/resources/cities/#{city.id}" - end - it "renders a map" do expect(page).to have_text("map_content_here") end - - it "applies mapkick options correctly" do - expect(page).to have_css("div#location-map [data-marker-color='#000']") - end end context "edit" do From f3810061a0a46d9d3bfc7bde395636e4a681b13e Mon Sep 17 00:00:00 2001 From: Nevelito Date: Fri, 17 Jan 2025 15:03:47 +0100 Subject: [PATCH 3/3] Fix alignment --- spec/dummy/app/avo/resources/city.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/spec/dummy/app/avo/resources/city.rb b/spec/dummy/app/avo/resources/city.rb index bd1d3d296f..da9d9dae8c 100644 --- a/spec/dummy/app/avo/resources/city.rb +++ b/spec/dummy/app/avo/resources/city.rb @@ -37,13 +37,13 @@ class Avo::Resources::City < Avo::BaseResource def base_fields field :id, as: :id field :coordinates, - as: :location, - stored_as: [:latitude, :longitude], - mapkick_options: { - style: "mapbox://styles/mapbox/satellite-v9", - controls: true, - markers: {color: "#000"} - } + as: :location, + stored_as: [:latitude, :longitude], + mapkick_options: { + style: "mapbox://styles/mapbox/satellite-v9", + controls: true, + markers: {color: "#FFC0CB"} + } field :city_center_area, as: :area, geometry: :polygon,