This repository has been archived by the owner on Mar 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
builder.rb
85 lines (73 loc) · 2.91 KB
/
builder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module BHM
module GoogleMaps
class Location
def initialize(object)
@lat, @lng = BHM::GoogleMaps.address_to_lat_lng_proc.call(object)
@address = BHM::GoogleMaps.address_to_s_proc.call(@address)
end
attr_reader :lat, :lng, :address
end
class Builder
def initialize(template, addresses, options)
@template = template
@addresses = Array.wrap(addresses).map {|l| Location.new(l) }
@options = options.symbolize_keys
@marker_options = @options.delete(:marker) || {}
@static = @options.delete(:static)
@css_class = "#{BHM::GoogleMaps.container_class} #{BHM::GoogleMaps.static_map_class} #{@options.delete(:class)}"
end
def to_html
image = build_static_map
@static ? image : build_container(image)
end
def build_static_map
url = StaticMap.new(@addresses, @marker_options.merge(@options||{})).to_url
@template.image_tag(url, {:alt => alt_text}.reverse_merge(@options||{}))
end
def build_container(image)
container_options = { :class => @css_class }
if selector = @options.delete(:location_data_selector)
# Lat/Lng data is embedded elsewhere in the page
container_options[:'data-locations-selector'] = selector
elsif @addresses.length == 1
embed_location_data_for_location(container_options)
else
embed_location_data_for_locations(container_options)
end
#Pass along users html options
#container_options.reverse_merge!(@options)
@template.content_tag(:div, image, container_options)
end
def embed_location_data_for_location(container_options)
lat, lng = @addresses.first.lat, @addresses.first.lng
container_options.merge! 'data-latitude' => lat, 'data-longitude' => lng
#@marker_options[:title] ||= self.address_as_string
@marker_options.each_pair do |k, v|
container_options[:"data-marker-#{k.to_s.dasherize}"] = v
end
end
def embed_location_data_for_locations(container_options)
latitudes, longitudes = [], []
@addresses.each do |address|
latitudes << address.lat
longitudes << address.lng
end
container_options.merge!(
'data-latitude' => latitudes.join(', '),
'data-longitude' => longitudes.join(', ')
)
#@marker_options[:title] ||= self.address_as_string
#@marker_options.each_pair do |k, v|
#container_options[:"data-marker-#{k.to_s.dasherize}"] = v
#end
end
def alt_text
if (count = @addresses.length) > 1
@template.pluralize(count, "address") + " plotted on a map"
else
BHM::GoogleMaps.address_to_s_proc.call(@addresses.first)
end
end
end
end
end