gmaps4rails 1.5.3 → 1.5.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -2
- data/Gemfile.lock +1 -1
- data/README.rdoc +8 -0
- data/gmaps4rails.gemspec +1 -0
- data/lib/gmaps4rails/acts_as_gmappable.rb +15 -19
- data/lib/gmaps4rails/base.rb +1 -0
- data/lib/gmaps4rails/json_builder.rb +24 -5
- data/lib/gmaps4rails/model_handler.rb +28 -6
- data/lib/gmaps4rails/version.rb +1 -1
- data/spec/dummy/app/models/place.rb +16 -0
- data/spec/factories/place_factory.rb +16 -0
- data/spec/models/place_spec.rb +35 -0
- data/spec/support/matchers.rb +31 -1
- metadata +10 -4
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -66,6 +66,14 @@ Create a migration and add the following fields to your table (here users):
|
|
66
66
|
add_column :users, :longitude, :float #you can change the name, see wiki
|
67
67
|
add_column :users, :gmaps, :boolean #not mandatory, see wiki
|
68
68
|
|
69
|
+
== Basic configuration: Non-relational DB
|
70
|
+
|
71
|
+
Mongoid example:
|
72
|
+
|
73
|
+
acts_as_gmappable :position => :location
|
74
|
+
|
75
|
+
field :location, :type => Array
|
76
|
+
|
69
77
|
== How to?
|
70
78
|
=== QuickStart!
|
71
79
|
In your controller:
|
data/gmaps4rails.gemspec
CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
|
|
17
17
|
|
18
18
|
s.add_development_dependency "rails", "~> 3.2.1"
|
19
19
|
s.add_development_dependency "sqlite3"
|
20
|
+
s.add_development_dependency "mongoid", "~> 3" if RUBY_VERSION == "1.9.3"
|
20
21
|
s.add_development_dependency 'jquery-rails'
|
21
22
|
s.add_development_dependency "rspec-rails"
|
22
23
|
s.add_development_dependency 'database_cleaner'
|
@@ -1,26 +1,18 @@
|
|
1
1
|
module Gmaps4rails
|
2
2
|
module ActsAsGmappable
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
# This is a validation method which triggers the geocoding and save its results
|
6
|
+
def process_geocoding
|
7
|
+
Gmaps4rails::ModelHandler.new(self, gmaps4rails_options).retrieve_coordinates
|
7
8
|
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
# creates json for one instance
|
17
|
-
def to_gmaps4rails(&block)
|
18
|
-
json = "["
|
19
|
-
object_json = Gmaps4rails.create_json(self, &block)
|
20
|
-
json << object_json.to_s unless object_json.nil?
|
21
|
-
json << "]"
|
22
|
-
end
|
23
|
-
|
10
|
+
# creates json for one instance
|
11
|
+
def to_gmaps4rails(&block)
|
12
|
+
json = "["
|
13
|
+
object_json = Gmaps4rails.create_json(self, &block)
|
14
|
+
json << object_json.to_s unless object_json.nil?
|
15
|
+
json << "]"
|
24
16
|
end
|
25
17
|
|
26
18
|
module ClassMethods
|
@@ -38,6 +30,10 @@ module Gmaps4rails
|
|
38
30
|
|
39
31
|
:lat_column => args[:lat] || "latitude",
|
40
32
|
:lng_column => args[:lng] || "longitude",
|
33
|
+
|
34
|
+
# purposefully no default.
|
35
|
+
# Leaving out the :position arg means we are using the default lat/lng to store coordinates
|
36
|
+
:position => args[:position],
|
41
37
|
|
42
38
|
:msg => args[:msg] || "Address invalid",
|
43
39
|
:validation => args[:validation].nil? ? true : args[:validation],
|
data/lib/gmaps4rails/base.rb
CHANGED
@@ -20,6 +20,7 @@ module Gmaps4rails
|
|
20
20
|
autoload :Geocoder, 'gmaps4rails/api_wrappers/geocoder'
|
21
21
|
autoload :Direction, 'gmaps4rails/api_wrappers/direction'
|
22
22
|
autoload :Places, 'gmaps4rails/api_wrappers/places'
|
23
|
+
autoload :ObjectAccessor, 'gmaps4rails/object_accessor'
|
23
24
|
|
24
25
|
mattr_accessor :http_proxy
|
25
26
|
|
@@ -25,9 +25,12 @@ module Gmaps4rails
|
|
25
25
|
# end
|
26
26
|
#
|
27
27
|
class JsonBuilder
|
28
|
+
|
29
|
+
delegate :position, :lat_column, :lng_column, :to => :@options
|
28
30
|
|
29
31
|
def initialize(object)
|
30
32
|
@object, @json_hash, @custom_json = object, Hash.new, nil
|
33
|
+
@options = OpenStruct.new @object.gmaps4rails_options
|
31
34
|
end
|
32
35
|
|
33
36
|
def process(&block)
|
@@ -72,10 +75,12 @@ module Gmaps4rails
|
|
72
75
|
:description => :gmaps4rails_infowindow,
|
73
76
|
:title => :gmaps4rails_title,
|
74
77
|
:sidebar => :gmaps4rails_sidebar,
|
75
|
-
:marker_picture => :gmaps4rails_marker_picture
|
76
|
-
|
77
|
-
|
78
|
-
|
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}
|
79
84
|
end
|
80
85
|
|
81
86
|
def handle_model_methods
|
@@ -83,6 +88,8 @@ module Gmaps4rails
|
|
83
88
|
if @object.respond_to? method_name
|
84
89
|
if json_name == :marker_picture
|
85
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)
|
86
93
|
else
|
87
94
|
@json_hash[json_name] = @object.send(method_name) unless @json_hash.has_key? json_name
|
88
95
|
end
|
@@ -108,7 +115,7 @@ module Gmaps4rails
|
|
108
115
|
end
|
109
116
|
|
110
117
|
def compliant?
|
111
|
-
!(
|
118
|
+
!(lat.blank? && lng.blank?)
|
112
119
|
end
|
113
120
|
|
114
121
|
def handle_block(&block)
|
@@ -116,6 +123,18 @@ module Gmaps4rails
|
|
116
123
|
@custom_json = block_result unless block_result == true
|
117
124
|
end
|
118
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
|
+
|
119
138
|
end
|
120
139
|
|
121
140
|
end
|
@@ -4,9 +4,9 @@ module Gmaps4rails
|
|
4
4
|
|
5
5
|
attr_accessor :options, :object
|
6
6
|
|
7
|
-
delegate :process_geocoding, :check_process, :checker, :lat_column, :lng_column, :msg, :validation,
|
7
|
+
delegate :process_geocoding, :check_process, :checker, :lat_column, :lng_column, :position, :msg, :validation,
|
8
8
|
:language, :protocol, :address, :callback, :normalized_address,
|
9
|
-
:to =>
|
9
|
+
:to => :options
|
10
10
|
|
11
11
|
def initialize(object, gmaps4rails_options)
|
12
12
|
@options = ::OpenStruct.new(gmaps4rails_options)
|
@@ -16,19 +16,41 @@ module Gmaps4rails
|
|
16
16
|
# saves coordinates according to the various options
|
17
17
|
def retrieve_coordinates
|
18
18
|
return if prevent_geocoding?
|
19
|
-
checked_coordinates do
|
20
|
-
|
21
|
-
object.send("#{lat_column}=", coordinates.first[:lat])
|
19
|
+
checked_coordinates do
|
20
|
+
position? ? set_position : set_lat_lng
|
22
21
|
# save normalized address if required
|
23
22
|
object.send("#{normalized_address}=", coordinates.first[:matched_address]) if normalized_address
|
24
23
|
# Call the callback method to let the user do what he wants with the data
|
25
|
-
object.send
|
24
|
+
object.send callback, coordinates.first[:full_data] if callback
|
26
25
|
# update checker if required
|
27
26
|
object.send("#{checker}=", true) if check_geocoding?
|
28
27
|
end
|
29
28
|
end
|
30
29
|
|
31
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
|
32
54
|
|
33
55
|
def checked_coordinates(&block)
|
34
56
|
yield if coordinates
|
data/lib/gmaps4rails/version.rb
CHANGED
@@ -0,0 +1,16 @@
|
|
1
|
+
if RUBY_VERSION == "1.9.3"
|
2
|
+
|
3
|
+
require 'mongoid'
|
4
|
+
|
5
|
+
class Place
|
6
|
+
include Mongoid::Document
|
7
|
+
include Gmaps4rails::ActsAsGmappable
|
8
|
+
|
9
|
+
acts_as_gmappable :address => :address, :position => :pos
|
10
|
+
|
11
|
+
field :pos, :type => Array
|
12
|
+
field :address, :type => String
|
13
|
+
field :gmaps, :type => Boolean
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
if RUBY_VERSION == "1.9.3"
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
include Geocoding
|
6
|
+
|
7
|
+
set_gmaps4rails_options!
|
8
|
+
|
9
|
+
# Mongoid 3.x only
|
10
|
+
require 'mongoid'
|
11
|
+
require 'moped'
|
12
|
+
|
13
|
+
Mongoid.configure do |config|
|
14
|
+
config.connect_to('mongoid_geo_test')
|
15
|
+
end
|
16
|
+
|
17
|
+
describe Gmaps4rails::ActsAsGmappable do
|
18
|
+
|
19
|
+
let(:place) { Factory(:place) }
|
20
|
+
let(:invalid_place) { Factory.build(:invalid_place) }
|
21
|
+
|
22
|
+
before(:each) do
|
23
|
+
Geocoding.stub_geocoding
|
24
|
+
end
|
25
|
+
|
26
|
+
context "standard configuration, valid place" do
|
27
|
+
it "should save longitude and latitude to the customized position array" do
|
28
|
+
set_gmaps4rails_options!(:position => 'location')
|
29
|
+
place.pos.should_not be_nil
|
30
|
+
place.should have_same_position_as TOULON
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/spec/support/matchers.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'rspec/expectations'
|
2
|
+
require 'ostruct'
|
2
3
|
|
3
4
|
def has_same_content_as?(actual, expected)
|
4
5
|
case actual
|
@@ -27,9 +28,38 @@ def has_same_content_as?(actual, expected)
|
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
31
|
+
class PositionMatcher
|
32
|
+
attr_reader :object, :position_hash
|
33
|
+
delegate :position, :lat_column, :lng_column, :to => :@options
|
34
|
+
|
35
|
+
def initialize object, position_hash
|
36
|
+
@object, @position_hash = object, position_hash
|
37
|
+
@options = ::OpenStruct.new object.gmaps4rails_options
|
38
|
+
end
|
39
|
+
|
40
|
+
def same_pos?
|
41
|
+
position_hash[:latitude] == lat && position_hash[:longitude] == lng
|
42
|
+
end
|
43
|
+
|
44
|
+
protected
|
45
|
+
|
46
|
+
def lat
|
47
|
+
position ? object.send("#{position}")[0] : object.send("#{lat_column}")
|
48
|
+
end
|
49
|
+
|
50
|
+
def lng
|
51
|
+
position ? object.send("#{position}")[1] : object.send("#{lng_column}")
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
def position_matcher object, position_hash
|
57
|
+
PositionMatcher.new object, position_hash
|
58
|
+
end
|
59
|
+
|
30
60
|
RSpec::Matchers.define :have_same_position_as do |position_hash|
|
31
61
|
match do |object|
|
32
|
-
|
62
|
+
position_matcher(object, position_hash).same_pos?
|
33
63
|
end
|
34
64
|
end
|
35
65
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gmaps4rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-08-
|
13
|
+
date: 2012-08-06 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rails
|
@@ -297,6 +297,7 @@ files:
|
|
297
297
|
- spec/dummy/app/helpers/application_helper.rb
|
298
298
|
- spec/dummy/app/mailers/.gitkeep
|
299
299
|
- spec/dummy/app/models/.gitkeep
|
300
|
+
- spec/dummy/app/models/place.rb
|
300
301
|
- spec/dummy/app/models/user.rb
|
301
302
|
- spec/dummy/app/views/layouts/application.html.erb
|
302
303
|
- spec/dummy/app/views/users/_form.html.erb
|
@@ -332,6 +333,7 @@ files:
|
|
332
333
|
- spec/dummy/public/javascripts/Song.js
|
333
334
|
- spec/dummy/public/logo.png
|
334
335
|
- spec/dummy/script/rails
|
336
|
+
- spec/factories/place_factory.rb
|
335
337
|
- spec/factories/user_factory.rb
|
336
338
|
- spec/fixtures/google_direction_valid.json
|
337
339
|
- spec/fixtures/google_geocoding_toulon_france.json
|
@@ -355,6 +357,7 @@ files:
|
|
355
357
|
- spec/lib/js_builder_spec.rb
|
356
358
|
- spec/lib/json_builder_spec.rb
|
357
359
|
- spec/lib/places_spec.rb
|
360
|
+
- spec/models/place_spec.rb
|
358
361
|
- spec/models/user_spec.rb
|
359
362
|
- spec/spec_helper.rb
|
360
363
|
- spec/support/geocoding.rb
|
@@ -373,7 +376,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
373
376
|
version: '0'
|
374
377
|
segments:
|
375
378
|
- 0
|
376
|
-
hash:
|
379
|
+
hash: -3497814623676724947
|
377
380
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
378
381
|
none: false
|
379
382
|
requirements:
|
@@ -382,7 +385,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
382
385
|
version: '0'
|
383
386
|
segments:
|
384
387
|
- 0
|
385
|
-
hash:
|
388
|
+
hash: -3497814623676724947
|
386
389
|
requirements: []
|
387
390
|
rubyforge_project:
|
388
391
|
rubygems_version: 1.8.24
|
@@ -402,6 +405,7 @@ test_files:
|
|
402
405
|
- spec/dummy/app/helpers/application_helper.rb
|
403
406
|
- spec/dummy/app/mailers/.gitkeep
|
404
407
|
- spec/dummy/app/models/.gitkeep
|
408
|
+
- spec/dummy/app/models/place.rb
|
405
409
|
- spec/dummy/app/models/user.rb
|
406
410
|
- spec/dummy/app/views/layouts/application.html.erb
|
407
411
|
- spec/dummy/app/views/users/_form.html.erb
|
@@ -437,6 +441,7 @@ test_files:
|
|
437
441
|
- spec/dummy/public/javascripts/Song.js
|
438
442
|
- spec/dummy/public/logo.png
|
439
443
|
- spec/dummy/script/rails
|
444
|
+
- spec/factories/place_factory.rb
|
440
445
|
- spec/factories/user_factory.rb
|
441
446
|
- spec/fixtures/google_direction_valid.json
|
442
447
|
- spec/fixtures/google_geocoding_toulon_france.json
|
@@ -460,6 +465,7 @@ test_files:
|
|
460
465
|
- spec/lib/js_builder_spec.rb
|
461
466
|
- spec/lib/json_builder_spec.rb
|
462
467
|
- spec/lib/places_spec.rb
|
468
|
+
- spec/models/place_spec.rb
|
463
469
|
- spec/models/user_spec.rb
|
464
470
|
- spec/spec_helper.rb
|
465
471
|
- spec/support/geocoding.rb
|