Skip to content

Commit

Permalink
enhancement: location field on preview & static option (#3602)
Browse files Browse the repository at this point in the history
* enhancement: location field on preview & static option

* fix test

* split complexity
  • Loading branch information
Paul-Bob authored Jan 22, 2025
1 parent 723d218 commit 1e74a91
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ gem "image_processing", "~> 1.12"
gem "prefixed_ids"

gem "mapkick-rb", "~> 0.1.4"

gem "mapkick-static"
gem "pluggy", path: "./pluggy"

gem "hashid-rails", "~> 1.4", ">= 1.4.1"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ GEM
net-pop
net-smtp
mapkick-rb (0.1.5)
mapkick-static (0.1.1)
marcel (1.0.4)
matrix (0.4.2)
memory_profiler (1.1.0)
Expand Down Expand Up @@ -731,6 +732,7 @@ DEPENDENCIES
launchy
listen (>= 3.5.1)
mapkick-rb (~> 0.1.4)
mapkick-static
meta-tags
money-rails (~> 1.12)
net-smtp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
<%= 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, **field.mapkick_options %>
<% else %>
<% end %>
<%= field.render_map(params) %>
<% end %>
57 changes: 40 additions & 17 deletions lib/avo/fields/location_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,72 @@
module Avo
module Fields
class LocationField < BaseField
attr_reader :mapkick_options
attr_reader :stored_as, :zoom

def initialize(id, **args, &block)
hide_on :index
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
# You can pass it an array of db columns [:latitude, :longitude]
@stored_as = args[:stored_as]
end

def render_map(params)
return "—" if !value_present?

render_static_map = params[:action] == "preview" || @args[:static]

Avo::Current.view_context.send render_static_map ? :static_map : :js_map,
[{latitude: value[0], longitude: value[1]}],
**default_mapkick_options(render_static_map)
end

def default_mapkick_options(render_static_map)
default_options = if render_static_map
{
width: 300,
height: 300
}
else
{
id: "location-map",
zoom: @args[:zoom]&.to_i || 15,
controls: true
}
end

default_options.merge(@args[:mapkick_options] || {})
end

def value_as_array?
stored_as.is_a?(Array) && stored_as.count == 2
@stored_as.is_a?(Array) && @stored_as.count == 2
end

def as_lat_long_field_id(get)
if get == :lat
stored_as.first
@stored_as.first
elsif get == :long
stored_as.last
@stored_as.last
end
end

def as_lat_long_placeholder(get)
if get == :lat
"Enter #{stored_as.first}"
"Enter #{@stored_as.first}"
elsif get == :long
"Enter #{stored_as.last}"
"Enter #{@stored_as.last}"
end
end

def as_lat_long_value(get)
if get == :lat
record.send(stored_as.first)
record.send(@stored_as.first)
elsif get == :long
record.send(stored_as.last)
record.send(@stored_as.last)
end
end

def fill_field(record, key, value, params)
if value_as_array?
latitude_field, longitude_field = stored_as
latitude_field, longitude_field = @stored_as
record.send(:"#{latitude_field}=", value[latitude_field])
record.send(:"#{longitude_field}=", value[longitude_field])
record
Expand All @@ -64,7 +87,7 @@ def to_permitted_param

def value
if value_as_array?
[@record.send(stored_as.first), @record.send(stored_as.last)]
[@record.send(@stored_as.first), @record.send(@stored_as.last)]
else
super
end
Expand All @@ -77,9 +100,9 @@ def value_present?
end

def assign_value(record:, value:)
return super if stored_as.blank?
return super if @stored_as.blank?

stored_as.each_with_index do |database_id, index|
@stored_as.each_with_index do |database_id, index|
record.send(:"#{database_id}=", value[index])
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/dummy/app/avo/resources/city.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ class Avo::Resources::City < Avo::BaseResource
}

def base_fields
field :preview, as: :preview
field :id, as: :id
field :coordinates,
as: :location,
show_on: :preview,
stored_as: [:latitude, :longitude],
mapkick_options: {
style: "mapbox://styles/mapbox/satellite-v9",
controls: true,
markers: {color: "#FFC0CB"}
}
field :city_center_area,
Expand Down
2 changes: 1 addition & 1 deletion spec/system/avo/location_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

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

expect(page).to have_text("map_content_here")
Expand Down

0 comments on commit 1e74a91

Please sign in to comment.