forked from apneadiving/Google-Maps-for-Rails
/
acts_as_gmappable.rb
62 lines (46 loc) · 2.42 KB
/
acts_as_gmappable.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
module Gmaps4rails
module ActsAsGmappable
def self.included base
base.send :include, InstanceMethods
base.send :extend, ClassMethods
end
module InstanceMethods
# This is a validation method which triggers the geocoding and save its results
def process_geocoding
Gmaps4rails::ModelHandler.new(self, gmaps4rails_options).retrieve_coordinates
end
# creates json for one instance
def to_gmaps4rails(&block)
json = "["
object_json = Gmaps4rails.create_json(self, &block)
json << object_json.to_s unless object_json.nil?
json << "]"
end
end
module ClassMethods
def acts_as_gmappable args = {}
validate :process_geocoding
#instance method containing all the options to configure the behaviour of the gem regarding the current Model
define_method "gmaps4rails_options" do
{
:process_geocoding => args[:process_geocoding].nil? ? true : args[:process_geocoding],
:check_process => args[:check_process].nil? ? true : args[:check_process],
:checker => args[:checker] || "gmaps",
:lat_column => args[:lat] || "latitude",
:lng_column => args[:lng] || "longitude",
# purposefully no default.
# Leaving out the :position arg means we are using the default lat/lng to store coordinates
:position => args[:position],
:msg => args[:msg] || "Address invalid",
:validation => args[:validation].nil? ? true : args[:validation],
:language => args[:language] || "en",
:protocol => args[:protocol] || "http",
:address => args[:address] || "gmaps4rails_address",
:callback => args[:callback],
:normalized_address => args[:normalized_address]
}
end
end
end
end
end