gmaps4rails 1.5.8 → 2.0.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +12 -0
- data/.travis.yml +9 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +149 -0
- data/Guardfile.old +24 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +129 -0
- data/Rakefile +4 -0
- data/app/views/gmaps4rails/_gmaps4rails.html.erb +20 -0
- data/gmaps4rails.gemspec +38 -0
- data/lib/generators/gmaps4rails/install_generator.rb +43 -0
- data/lib/generators/templates/README +2 -0
- data/lib/gmaps4rails.rb +26 -0
- data/lib/gmaps4rails/acts_as_gmappable.rb +54 -0
- data/lib/gmaps4rails/api_wrappers/base_net_methods.rb +40 -0
- data/lib/gmaps4rails/api_wrappers/direction.rb +87 -0
- data/lib/gmaps4rails/api_wrappers/geocoder.rb +54 -0
- data/lib/gmaps4rails/api_wrappers/places.rb +74 -0
- data/lib/gmaps4rails/base.rb +126 -0
- data/lib/gmaps4rails/extensions/enumerable.rb +14 -0
- data/lib/gmaps4rails/extensions/hash.rb +9 -0
- data/lib/gmaps4rails/helper/gmaps4rails_helper.rb +33 -0
- data/lib/gmaps4rails/js_builder.rb +154 -0
- data/lib/gmaps4rails/json_builder.rb +140 -0
- data/lib/gmaps4rails/model_handler.rb +101 -0
- data/lib/gmaps4rails/version.rb +3 -0
- data/lib/gmaps4rails/view_helper.rb +172 -0
- data/lib/tasks/jasmine.rake +8 -0
- data/public/javascripts/gmaps4rails/all.js +2144 -0
- data/public/javascripts/gmaps4rails/base.js +792 -0
- data/public/javascripts/gmaps4rails/google.js +556 -0
- data/public/javascripts/gmaps4rails/openlayers.js +377 -0
- data/public/stylesheets/gmaps4rails.css +24 -0
- data/spec/dummy/.gitignore +3 -0
- data/spec/dummy/.rspec +1 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +18 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/assets/stylesheets/gmaps4rails.css +24 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/users_controller.rb +105 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.gitkeep +0 -0
- data/spec/dummy/app/models/.gitkeep +0 -0
- data/spec/dummy/app/models/place.rb +16 -0
- data/spec/dummy/app/models/user.rb +12 -0
- data/spec/dummy/app/views/layouts/application.html.erb +17 -0
- data/spec/dummy/app/views/users/_form.html.erb +37 -0
- data/spec/dummy/app/views/users/edit.html.erb +6 -0
- data/spec/dummy/app/views/users/index.html.erb +268 -0
- data/spec/dummy/app/views/users/new.html.erb +5 -0
- data/spec/dummy/app/views/users/show.html.erb +30 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +62 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +67 -0
- data/spec/dummy/config/environments/test.rb +37 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/gmaps4rails.rb +1 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +60 -0
- data/spec/dummy/db/migrate/20120408170155_create_users.rb +16 -0
- data/spec/dummy/db/schema.rb +29 -0
- data/spec/dummy/lib/assets/.gitkeep +0 -0
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/javascripts/Player.js +22 -0
- data/spec/dummy/public/javascripts/Song.js +7 -0
- data/spec/dummy/public/logo.png +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/factories/place_factory.rb +16 -0
- data/spec/factories/user_factory.rb +23 -0
- data/spec/fixtures/google_direction_valid.json +65 -0
- data/spec/fixtures/google_geocoding_toulon_france.json +58 -0
- data/spec/fixtures/google_places_valid.json +45 -0
- data/spec/fixtures/google_wrong_geocoding.json +4 -0
- data/spec/launchers/all_but_requests.rb +0 -0
- data/spec/launchers/all_specs.rb +0 -0
- data/spec/launchers/requests.rb +0 -0
- data/spec/lib/base_spec.rb +59 -0
- data/spec/lib/direction_spec.rb +53 -0
- data/spec/lib/geocoder_spec.rb +46 -0
- data/spec/lib/js_builder_spec.rb +134 -0
- data/spec/lib/json_builder_spec.rb +232 -0
- data/spec/lib/places_spec.rb +25 -0
- data/spec/models/place_spec.rb +39 -0
- data/spec/models/user_spec.rb +187 -0
- data/spec/spec_helper.rb +38 -0
- data/spec/support/geocoding.rb +40 -0
- data/spec/support/matchers.rb +73 -0
- data/todo +0 -0
- data/vendor/assets/javascripts/gmaps4rails/all.js +1 -0
- data/vendor/assets/javascripts/gmaps4rails/base.coffee +1 -0
- data/vendor/assets/javascripts/gmaps4rails/base/common.coffee +43 -0
- data/vendor/assets/javascripts/gmaps4rails/base/configuration.coffee +7 -0
- data/vendor/assets/javascripts/gmaps4rails/base/gmaps.coffee +13 -0
- data/vendor/assets/javascripts/gmaps4rails/base/interfaces/basic_interface.coffee +15 -0
- data/vendor/assets/javascripts/gmaps4rails/base/interfaces/controller_interface.coffee +15 -0
- data/vendor/assets/javascripts/gmaps4rails/base/interfaces/map_interface.coffee +27 -0
- data/vendor/assets/javascripts/gmaps4rails/base/interfaces/marker_interface.coffee +15 -0
- data/vendor/assets/javascripts/gmaps4rails/base/main.coffee +124 -0
- data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/circle_controller.coffee +29 -0
- data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/kml_controller.coffee +5 -0
- data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/marker_controller.coffee +76 -0
- data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/polygon_controller.coffee +29 -0
- data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/polyline_controller.coffee +29 -0
- data/vendor/assets/javascripts/gmaps4rails/base/objects/circle.coffee +12 -0
- data/vendor/assets/javascripts/gmaps4rails/base/objects/kml.coffee +8 -0
- data/vendor/assets/javascripts/gmaps4rails/base/objects/map.coffee +62 -0
- data/vendor/assets/javascripts/gmaps4rails/base/objects/marker.coffee +24 -0
- data/vendor/assets/javascripts/gmaps4rails/base/objects/polygon.coffee +11 -0
- data/vendor/assets/javascripts/gmaps4rails/base/objects/polyline.coffee +11 -0
- data/vendor/assets/javascripts/gmaps4rails/bing.coffee +1 -0
- data/vendor/assets/javascripts/gmaps4rails/bing/main.coffee +29 -0
- data/vendor/assets/javascripts/gmaps4rails/bing/objects/map.coffee +63 -0
- data/vendor/assets/javascripts/gmaps4rails/bing/objects/marker.coffee +78 -0
- data/vendor/assets/javascripts/gmaps4rails/bing/shared.coffee +20 -0
- data/vendor/assets/javascripts/gmaps4rails/google.coffee +1 -0
- data/vendor/assets/javascripts/gmaps4rails/google/main.coffee +47 -0
- data/vendor/assets/javascripts/gmaps4rails/google/objects/circle.coffee +38 -0
- data/vendor/assets/javascripts/gmaps4rails/google/objects/kml.coffee +16 -0
- data/vendor/assets/javascripts/gmaps4rails/google/objects/map.coffee +71 -0
- data/vendor/assets/javascripts/gmaps4rails/google/objects/marker.coffee +114 -0
- data/vendor/assets/javascripts/gmaps4rails/google/objects/polygon.coffee +38 -0
- data/vendor/assets/javascripts/gmaps4rails/google/objects/polyline.coffee +49 -0
- data/vendor/assets/javascripts/gmaps4rails/google/shared.coffee +27 -0
- data/vendor/assets/javascripts/gmaps4rails/openlayers.coffee +1 -0
- data/vendor/assets/javascripts/gmaps4rails/openlayers/main.coffee +138 -0
- data/vendor/assets/javascripts/gmaps4rails/openlayers/objects/map.coffee +49 -0
- data/vendor/assets/javascripts/gmaps4rails/openlayers/objects/marker.coffee +68 -0
- data/vendor/assets/javascripts/gmaps4rails/openlayers/objects/polyline.coffee +39 -0
- data/vendor/assets/javascripts/gmaps4rails/openlayers/shared.coffee +19 -0
- metadata +378 -14
- checksums.yaml +0 -7
@@ -0,0 +1,14 @@
|
|
1
|
+
module Enumerable
|
2
|
+
#Scopes on models generate Arrays
|
3
|
+
#this method enables short call to the json creation for all elements in the array
|
4
|
+
def to_gmaps4rails(&block)
|
5
|
+
output = "["
|
6
|
+
json_array = []
|
7
|
+
each do |object|
|
8
|
+
json = Gmaps4rails.create_json(object, &block)
|
9
|
+
json_array << json.to_s unless json.nil?
|
10
|
+
end
|
11
|
+
output << json_array * (",")
|
12
|
+
output << "]"
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
# this method extracts all info from the hash to create javascript
|
4
|
+
# this javascript is then rendered raw to the view so it can be interpreted and executed
|
5
|
+
def to_gmaps4rails
|
6
|
+
Gmaps4rails.create_js_from_hash(self.with_indifferent_access)
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Gmaps4railsHelper
|
2
|
+
|
3
|
+
# shortcut helper for marker display with convenient default settings
|
4
|
+
# @params [String] builder is a json string
|
5
|
+
def gmaps4rails(builder)
|
6
|
+
options = {
|
7
|
+
:map_options => { :auto_adjust => true},
|
8
|
+
:markers => { :data => builder, :options => {:do_clustering => true} }
|
9
|
+
}
|
10
|
+
gmaps(options)
|
11
|
+
end
|
12
|
+
|
13
|
+
# full helper to pass all variables and their options
|
14
|
+
# @params [Hash] options is a Hash containing data and options. Example: { markers:{ data: @json, options: { do_clustering: true } } }
|
15
|
+
def gmaps(options)
|
16
|
+
options_with_indifferent_access = options.with_indifferent_access
|
17
|
+
view_helper = Gmaps4rails::ViewHelper.new(options_with_indifferent_access)
|
18
|
+
|
19
|
+
js_dependencies = if Gmaps4rails.escape_js_url
|
20
|
+
view_helper.js_dependencies_array
|
21
|
+
else
|
22
|
+
view_helper.js_dependencies_array.map(&:html_safe)
|
23
|
+
end
|
24
|
+
|
25
|
+
render :partial => '/gmaps4rails/gmaps4rails',
|
26
|
+
:locals => {
|
27
|
+
:options => options_with_indifferent_access,
|
28
|
+
:js_dependencies => js_dependencies,
|
29
|
+
:dom => view_helper.dom_attributes
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
module Gmaps4rails
|
2
|
+
class JsBuilder
|
3
|
+
|
4
|
+
DEFAULT_MAP_ID = "map"
|
5
|
+
DATA_KEYS = [:markers, :polylines, :polygons, :circles, :direction, :kml]
|
6
|
+
|
7
|
+
#the 'option_hash' must have the following structure
|
8
|
+
#{
|
9
|
+
# :map_options => hash,
|
10
|
+
# :markers => { :data => json, :options => hash },
|
11
|
+
# :polylines => { :data => json, :options => hash },
|
12
|
+
# :polygons => { :data => json, :options => hash },
|
13
|
+
# :circles => { :data => json, :options => hash },
|
14
|
+
# :direction => { :data => hash, :options => hash },
|
15
|
+
# :kml => { :data => json, :options => hash }
|
16
|
+
#}
|
17
|
+
#should be with only symbol keys or with indifferent access
|
18
|
+
def initialize(option_hash)
|
19
|
+
@js = Array.new
|
20
|
+
@hash = option_hash
|
21
|
+
end
|
22
|
+
|
23
|
+
def create_js
|
24
|
+
@js << "#{gmap_id} = new #{ map_constructor };"
|
25
|
+
@js << "Gmaps.#{js_function_name} = function() {"
|
26
|
+
|
27
|
+
process_map_options
|
28
|
+
|
29
|
+
@js << "#{gmap_id}.initialize();"
|
30
|
+
|
31
|
+
process_data
|
32
|
+
|
33
|
+
@js << "#{gmap_id}.adjustMapToBounds();"
|
34
|
+
@js << "#{gmap_id}.callback();"
|
35
|
+
@js << "};"
|
36
|
+
@js << "Gmaps.oldOnload = window.onload;\n window.onload = function() { Gmaps.triggerOldOnload(); Gmaps.loadMaps(); };" if load_map?
|
37
|
+
|
38
|
+
@js * ("\n")
|
39
|
+
end
|
40
|
+
|
41
|
+
def process_map_options
|
42
|
+
return unless map_options
|
43
|
+
map_options.each do |option_key, option_value|
|
44
|
+
next if [:class, :container_class].include? option_key.to_sym
|
45
|
+
case option_key.to_sym
|
46
|
+
when :bounds, :raw #particular case, render the content unescaped
|
47
|
+
@js << "#{gmap_id}.map_options.#{option_key} = #{option_value};"
|
48
|
+
else
|
49
|
+
@js << "#{gmap_id}.map_options.#{option_key} = #{option_value.to_json};"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def process_data
|
55
|
+
data.each do |name, hash|
|
56
|
+
datum = ::Gmaps4rails::JsBuilder::Datum.new(gmap_id, name, hash)
|
57
|
+
@js.concat datum.create_js
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def map_options
|
62
|
+
@hash[:map_options]
|
63
|
+
end
|
64
|
+
|
65
|
+
def data
|
66
|
+
@hash.select{|key, value| DATA_KEYS.include?(key.to_sym) }
|
67
|
+
end
|
68
|
+
|
69
|
+
def load_map?
|
70
|
+
@hash[:last_map].nil? || @hash[:last_map] == true
|
71
|
+
end
|
72
|
+
|
73
|
+
def js_function_name
|
74
|
+
"load_" + map_id
|
75
|
+
end
|
76
|
+
|
77
|
+
def gmap_id
|
78
|
+
@gmap_id ||= "Gmaps." + map_id
|
79
|
+
end
|
80
|
+
|
81
|
+
def map_id
|
82
|
+
@map_id ||= map_options.try(:[],:id) || DEFAULT_MAP_ID
|
83
|
+
end
|
84
|
+
|
85
|
+
def map_constructor
|
86
|
+
map_options.try(:[],:provider) ? "Gmaps4Rails#{map_options[:provider].capitalize}()" : "Gmaps4RailsGoogle()"
|
87
|
+
end
|
88
|
+
|
89
|
+
class Datum
|
90
|
+
# example:
|
91
|
+
# - name: :markers
|
92
|
+
# - hash: { :data => json, :options => hash }
|
93
|
+
|
94
|
+
delegate :options, :data, :to => :@element_info
|
95
|
+
|
96
|
+
def initialize(gmap_id, name, hash)
|
97
|
+
@gmap_id, @name, @js = gmap_id, name, Array.new
|
98
|
+
@element_info = OpenStruct.new(hash)
|
99
|
+
end
|
100
|
+
|
101
|
+
def create_js
|
102
|
+
if @name.to_sym == :direction
|
103
|
+
create_direction_js
|
104
|
+
else
|
105
|
+
create_standard_js
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def create_standard_js
|
110
|
+
set_configuration_variables
|
111
|
+
@js << "#{@gmap_id}.add#{@name.capitalize}(#{data});"
|
112
|
+
end
|
113
|
+
|
114
|
+
def create_direction_js
|
115
|
+
@js << "#{@gmap_id}.direction_conf.origin = '#{data["from"]}';"
|
116
|
+
@js << "#{@gmap_id}.direction_conf.destination = '#{data["to"]}';"
|
117
|
+
|
118
|
+
set_direction_variables
|
119
|
+
|
120
|
+
@js << "#{@gmap_id}.create_direction();"
|
121
|
+
end
|
122
|
+
|
123
|
+
def set_configuration_variables
|
124
|
+
return unless options
|
125
|
+
options.each do |option_key, option_value|
|
126
|
+
@js << if option_key.to_sym == :raw
|
127
|
+
"#{@gmap_id}.#{@name}_conf.#{option_key} = #{option_value};"
|
128
|
+
else
|
129
|
+
"#{@gmap_id}.#{@name}_conf.#{option_key} = #{option_value.to_json};"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def set_direction_variables
|
135
|
+
return unless options
|
136
|
+
options.each do |option_key, option_value|
|
137
|
+
if option_key.to_sym == :waypoints
|
138
|
+
waypoints = Array.new
|
139
|
+
option_value.each do |waypoint|
|
140
|
+
waypoints << { "location" => waypoint, "stopover" => true }.to_json
|
141
|
+
end
|
142
|
+
@js << "#{@gmap_id}.direction_conf.waypoints = [#{waypoints * (",")}];"
|
143
|
+
else
|
144
|
+
@js << "#{@gmap_id}.direction_conf.#{option_key} = #{option_value.to_json};"
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
|
153
|
+
end
|
154
|
+
|
@@ -0,0 +1,140 @@
|
|
1
|
+
module Gmaps4rails
|
2
|
+
# the to_gmaps4rails method accepts a block to customize:
|
3
|
+
# - infowindow
|
4
|
+
# - picture
|
5
|
+
# - title
|
6
|
+
# - sidebar
|
7
|
+
# - json
|
8
|
+
#
|
9
|
+
# This works this way:
|
10
|
+
# @json = User.all.to_gmaps4rails do |user, marker|
|
11
|
+
# marker.infowindow render_to_string(:partial => "/users/my_template", :locals => { :object => user}).gsub(/\n/, '').gsub(/"/, '\"')
|
12
|
+
# marker.picture({
|
13
|
+
# :picture => "http://www.blankdots.com/img/github-32x32.png",
|
14
|
+
# :width => "32",
|
15
|
+
# :height => "32"
|
16
|
+
# })
|
17
|
+
# marker.title "i'm the title"
|
18
|
+
# marker.sidebar "i'm the sidebar"
|
19
|
+
# marker.json({ :id => user.id })
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# For backward compability, a mere string could be passed:
|
23
|
+
# @json = User.all.to_gmaps4rails do |user, marker|
|
24
|
+
# "\"id\": #{user.id}"
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
class JsonBuilder
|
28
|
+
|
29
|
+
delegate :position, :lat_column, :lng_column, :to => :@options
|
30
|
+
|
31
|
+
def initialize(object)
|
32
|
+
@object, @json_hash, @custom_json = object, Hash.new, nil
|
33
|
+
@options = OpenStruct.new @object.gmaps4rails_options
|
34
|
+
end
|
35
|
+
|
36
|
+
def process(&block)
|
37
|
+
if compliant?
|
38
|
+
handle_block(&block) if block_given?
|
39
|
+
handle_model_methods
|
40
|
+
return_json
|
41
|
+
else
|
42
|
+
nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def infowindow(string)
|
47
|
+
@json_hash[:description] = string
|
48
|
+
true
|
49
|
+
end
|
50
|
+
|
51
|
+
def title(string)
|
52
|
+
@json_hash[:title] = string
|
53
|
+
true
|
54
|
+
end
|
55
|
+
|
56
|
+
def sidebar(string)
|
57
|
+
@json_hash[:sidebar] = string
|
58
|
+
true
|
59
|
+
end
|
60
|
+
|
61
|
+
def json(json)
|
62
|
+
return @json_hash.merge! json if json.is_a? Hash
|
63
|
+
true
|
64
|
+
end
|
65
|
+
|
66
|
+
def picture(hash)
|
67
|
+
@json_hash.merge! hash
|
68
|
+
true
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def model_attributes
|
74
|
+
{
|
75
|
+
:description => :gmaps4rails_infowindow,
|
76
|
+
:title => :gmaps4rails_title,
|
77
|
+
:sidebar => :gmaps4rails_sidebar,
|
78
|
+
:marker_picture => :gmaps4rails_marker_picture
|
79
|
+
}.merge(coordinates_attributes)
|
80
|
+
end
|
81
|
+
|
82
|
+
def coordinates_attributes
|
83
|
+
position_from_array? ? {:position => position } : {:lat => lat_column, :lng => lng_column}
|
84
|
+
end
|
85
|
+
|
86
|
+
def handle_model_methods
|
87
|
+
model_attributes.each do |json_name, method_name|
|
88
|
+
if @object.respond_to? method_name
|
89
|
+
if json_name == :marker_picture
|
90
|
+
@json_hash.merge!(@object.send(method_name)) unless @json_hash.has_key? "picture"
|
91
|
+
elsif json_name == :position
|
92
|
+
@json_hash.merge!(:lat => lat, :lng => lng)
|
93
|
+
else
|
94
|
+
@json_hash[json_name] = @object.send(method_name) unless @json_hash.has_key? json_name
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# returns the proper json
|
101
|
+
# three cases here:
|
102
|
+
# - no custom json provided
|
103
|
+
# - custom json provided as a hash (marker.json { :id => user.id }) => merge hashes then create json
|
104
|
+
# - custom json provided as string (marker.json {"\"id\": #{user.id}" } => create json from hash then insert string inside
|
105
|
+
def return_json
|
106
|
+
return @json_hash.to_json if @custom_json.nil?
|
107
|
+
case @custom_json
|
108
|
+
when Hash
|
109
|
+
@json_hash.merge! @custom_json
|
110
|
+
return @json_hash.to_json
|
111
|
+
when String
|
112
|
+
output = @json_hash.to_json
|
113
|
+
return output.insert(1, @custom_json + ",")
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def compliant?
|
118
|
+
!(lat.blank? && lng.blank?)
|
119
|
+
end
|
120
|
+
|
121
|
+
def handle_block(&block)
|
122
|
+
block_result = yield(@object, self)
|
123
|
+
@custom_json = block_result unless block_result == true
|
124
|
+
end
|
125
|
+
|
126
|
+
def position_from_array?
|
127
|
+
position #if gmaps4rails_options[:position] is filled, means user is indicating an array
|
128
|
+
end
|
129
|
+
|
130
|
+
def lat
|
131
|
+
position_from_array? ? @object.send("#{position}")[0] : @object.send("#{lat_column}")
|
132
|
+
end
|
133
|
+
|
134
|
+
def lng
|
135
|
+
position_from_array? ? @object.send("#{position}")[1] : @object.send("#{lng_column}")
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module Gmaps4rails
|
2
|
+
|
3
|
+
class ModelHandler
|
4
|
+
|
5
|
+
attr_accessor :options, :object
|
6
|
+
|
7
|
+
delegate :process_geocoding, :check_process, :checker, :lat_column, :lng_column, :position, :msg, :validation,
|
8
|
+
:language, :protocol, :address, :callback, :normalized_address,
|
9
|
+
:to => :options
|
10
|
+
|
11
|
+
def initialize(object, gmaps4rails_options)
|
12
|
+
@options = ::OpenStruct.new(gmaps4rails_options)
|
13
|
+
@object = object
|
14
|
+
end
|
15
|
+
|
16
|
+
# saves coordinates according to the various options
|
17
|
+
def retrieve_coordinates
|
18
|
+
return if prevent_geocoding?
|
19
|
+
checked_coordinates do
|
20
|
+
position? ? set_position : set_lat_lng
|
21
|
+
# save normalized address if required
|
22
|
+
object.send("#{normalized_address}=", coordinates.first[:matched_address]) if normalized_address
|
23
|
+
# Call the callback method to let the user do what he wants with the data
|
24
|
+
object.send callback, coordinates.first[:full_data] if callback
|
25
|
+
# update checker if required
|
26
|
+
object.send("#{checker}=", true) if check_geocoding?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
# sets array for non relationnal db
|
33
|
+
def set_position
|
34
|
+
object.send("#{position}=", [lat, lng])
|
35
|
+
end
|
36
|
+
|
37
|
+
#sets regular columns
|
38
|
+
def set_lat_lng
|
39
|
+
object.send("#{lng_column}=", lng)
|
40
|
+
object.send("#{lat_column}=", lat)
|
41
|
+
end
|
42
|
+
|
43
|
+
def lat
|
44
|
+
coordinates.first[:lat]
|
45
|
+
end
|
46
|
+
|
47
|
+
def lng
|
48
|
+
coordinates.first[:lng]
|
49
|
+
end
|
50
|
+
|
51
|
+
def position?
|
52
|
+
position
|
53
|
+
end
|
54
|
+
|
55
|
+
def checked_coordinates(&block)
|
56
|
+
yield if coordinates
|
57
|
+
end
|
58
|
+
|
59
|
+
def coordinates
|
60
|
+
@coordinates ||= get_coordinates
|
61
|
+
end
|
62
|
+
|
63
|
+
def get_coordinates
|
64
|
+
Gmaps4rails.geocode(object.send(address), language, false, protocol)
|
65
|
+
rescue GeocodeStatus, GeocodeInvalidQuery => e #address was invalid, add error to address.
|
66
|
+
Rails.logger.warn(e)
|
67
|
+
object.errors[address] << msg if condition_eval(object, validation)
|
68
|
+
false
|
69
|
+
rescue GeocodeNetStatus => e #connection error, No need to prevent save.
|
70
|
+
Rails.logger.warn(e)
|
71
|
+
false
|
72
|
+
end
|
73
|
+
|
74
|
+
# to prevent geocoding each time a save is made
|
75
|
+
# if process_geocoding is a TrueClass or a FalseClass, 'check_process' and 'checker' play an additional role
|
76
|
+
# if process_geocoding is a Proc or a Symbol, 'check_process' and 'checker' are skipped since process_geocoding bears the whole logic
|
77
|
+
def prevent_geocoding?
|
78
|
+
if process_geocoding.is_a?(TrueClass) || process_geocoding.is_a?(FalseClass)
|
79
|
+
return true if !condition_eval(object, process_geocoding)
|
80
|
+
condition_eval(object, check_process) && object.send(checker)
|
81
|
+
else
|
82
|
+
!condition_eval(object, process_geocoding)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Do we have to check the geocoding
|
87
|
+
def check_geocoding?
|
88
|
+
if process_geocoding.is_a?(TrueClass) || process_geocoding.is_a?(FalseClass)
|
89
|
+
condition_eval(object, check_process)
|
90
|
+
else
|
91
|
+
false
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def condition_eval(*args)
|
96
|
+
Gmaps4rails.condition_eval(*args)
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|