Skip to content

Commit 1e74a91

Browse files
authored
enhancement: location field on preview & static option (#3602)
* enhancement: location field on preview & static option * fix test * split complexity
1 parent 723d218 commit 1e74a91

File tree

6 files changed

+47
-25
lines changed

6 files changed

+47
-25
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ gem "image_processing", "~> 1.12"
175175
gem "prefixed_ids"
176176

177177
gem "mapkick-rb", "~> 0.1.4"
178-
178+
gem "mapkick-static"
179179
gem "pluggy", path: "./pluggy"
180180

181181
gem "hashid-rails", "~> 1.4", ">= 1.4.1"

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ GEM
380380
net-pop
381381
net-smtp
382382
mapkick-rb (0.1.5)
383+
mapkick-static (0.1.1)
383384
marcel (1.0.4)
384385
matrix (0.4.2)
385386
memory_profiler (1.1.0)
@@ -731,6 +732,7 @@ DEPENDENCIES
731732
launchy
732733
listen (>= 3.5.1)
733734
mapkick-rb (~> 0.1.4)
735+
mapkick-static
734736
meta-tags
735737
money-rails (~> 1.12)
736738
net-smtp
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
<%= field_wrapper(**field_wrapper_args) do %>
2-
<% if field.value_present? %>
3-
<%= js_map [{latitude: field.value[0], longitude: field.value[1]}], id: "location-map", zoom: field.zoom, controls: true, **field.mapkick_options %>
4-
<% else %>
5-
6-
<% end %>
2+
<%= field.render_map(params) %>
73
<% end %>

lib/avo/fields/location_field.rb

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,72 @@
33
module Avo
44
module Fields
55
class LocationField < BaseField
6-
attr_reader :mapkick_options
7-
attr_reader :stored_as, :zoom
8-
96
def initialize(id, **args, &block)
107
hide_on :index
118
super(id, **args, &block)
129

13-
@stored_as = args[:stored_as].present? ? args[:stored_as] : nil # You can pass it an array of db columns [:latitude, :longitude]
14-
@mapkick_options = args[:mapkick_options].presence || {}
15-
@zoom = args[:zoom].present? ? args[:zoom].to_i : 15
10+
# You can pass it an array of db columns [:latitude, :longitude]
11+
@stored_as = args[:stored_as]
12+
end
13+
14+
def render_map(params)
15+
return "—" if !value_present?
16+
17+
render_static_map = params[:action] == "preview" || @args[:static]
18+
19+
Avo::Current.view_context.send render_static_map ? :static_map : :js_map,
20+
[{latitude: value[0], longitude: value[1]}],
21+
**default_mapkick_options(render_static_map)
22+
end
23+
24+
def default_mapkick_options(render_static_map)
25+
default_options = if render_static_map
26+
{
27+
width: 300,
28+
height: 300
29+
}
30+
else
31+
{
32+
id: "location-map",
33+
zoom: @args[:zoom]&.to_i || 15,
34+
controls: true
35+
}
36+
end
37+
38+
default_options.merge(@args[:mapkick_options] || {})
1639
end
1740

1841
def value_as_array?
19-
stored_as.is_a?(Array) && stored_as.count == 2
42+
@stored_as.is_a?(Array) && @stored_as.count == 2
2043
end
2144

2245
def as_lat_long_field_id(get)
2346
if get == :lat
24-
stored_as.first
47+
@stored_as.first
2548
elsif get == :long
26-
stored_as.last
49+
@stored_as.last
2750
end
2851
end
2952

3053
def as_lat_long_placeholder(get)
3154
if get == :lat
32-
"Enter #{stored_as.first}"
55+
"Enter #{@stored_as.first}"
3356
elsif get == :long
34-
"Enter #{stored_as.last}"
57+
"Enter #{@stored_as.last}"
3558
end
3659
end
3760

3861
def as_lat_long_value(get)
3962
if get == :lat
40-
record.send(stored_as.first)
63+
record.send(@stored_as.first)
4164
elsif get == :long
42-
record.send(stored_as.last)
65+
record.send(@stored_as.last)
4366
end
4467
end
4568

4669
def fill_field(record, key, value, params)
4770
if value_as_array?
48-
latitude_field, longitude_field = stored_as
71+
latitude_field, longitude_field = @stored_as
4972
record.send(:"#{latitude_field}=", value[latitude_field])
5073
record.send(:"#{longitude_field}=", value[longitude_field])
5174
record
@@ -64,7 +87,7 @@ def to_permitted_param
6487

6588
def value
6689
if value_as_array?
67-
[@record.send(stored_as.first), @record.send(stored_as.last)]
90+
[@record.send(@stored_as.first), @record.send(@stored_as.last)]
6891
else
6992
super
7093
end
@@ -77,9 +100,9 @@ def value_present?
77100
end
78101

79102
def assign_value(record:, value:)
80-
return super if stored_as.blank?
103+
return super if @stored_as.blank?
81104

82-
stored_as.each_with_index do |database_id, index|
105+
@stored_as.each_with_index do |database_id, index|
83106
record.send(:"#{database_id}=", value[index])
84107
end
85108
end

spec/dummy/app/avo/resources/city.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ class Avo::Resources::City < Avo::BaseResource
3535
}
3636

3737
def base_fields
38+
field :preview, as: :preview
3839
field :id, as: :id
3940
field :coordinates,
4041
as: :location,
42+
show_on: :preview,
4143
stored_as: [:latitude, :longitude],
4244
mapkick_options: {
4345
style: "mapbox://styles/mapbox/satellite-v9",
44-
controls: true,
4546
markers: {color: "#FFC0CB"}
4647
}
4748
field :city_center_area,

spec/system/avo/location_field_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
context "show" do
6161
it "renders a map" do
62-
Avo::Fields::LocationField::ShowComponent.any_instance.stub(:js_map).and_return("map_content_here")
62+
Avo::Fields::LocationField.any_instance.stub(:render_map).and_return("map_content_here")
6363
visit "/admin/resources/cities/#{city.id}"
6464

6565
expect(page).to have_text("map_content_here")

0 commit comments

Comments
 (0)