gmaps4rails 0.7.9 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -21,10 +21,11 @@ See screencasts here: http://www.youtube.com/user/TheApneadiving
21
21
  In your model, add:
22
22
 
23
23
  acts_as_gmappable
24
-
25
- def gmaps4rails_address
26
- self.address #describe how to retrieve the address from your model
27
- end
24
+
25
+ def gmaps4rails_address
26
+ #describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
27
+ "#{self.street}, #{self.city}, #{self.country}"
28
+ end
28
29
 
29
30
  Create a migration and add the following fields to your table (here users):
30
31
 
@@ -77,4 +78,4 @@ MIT license.
77
78
 
78
79
  Authors: Benjamin Roth, David Ruyer
79
80
 
80
- Contributor: Alex Vorobiev
81
+ Contributors: Mircea Pricop, Alex Vorobiev
@@ -11,16 +11,7 @@ module Gmaps4rails
11
11
  class DirectionStatus < StandardError; end
12
12
  class DirectionNetStatus < StandardError; end
13
13
  class DirectionInvalidQuery < StandardError; end
14
-
15
- # def Gmaps4rails.create_json(object)
16
- # unless object[object.gmaps4rails_options[:lat_column]].blank? && object[object.gmaps4rails_options[:lng_column]].blank?
17
- # "{
18
- # \"description\": \"#{object.gmaps4rails_infowindow}\", \"title\": \"#{object.gmaps4rails_title}\", \"sidebar\": \"#{object.gmaps4rails_sidebar}\",
19
- # \"longitude\": \"#{object[object.gmaps4rails_options[:lng_column]]}\", \"latitude\": \"#{object[object.gmaps4rails_options[:lat_column]]}\", \"picture\": \"#{object.gmaps4rails_marker_picture['picture']}\", \"width\": \"#{object.gmaps4rails_marker_picture['width']}\", \"height\": \"#{object.gmaps4rails_marker_picture['height']}\"
20
- # } ,"
21
- # end
22
- # end
23
-
14
+
24
15
  def Gmaps4rails.create_json(object)
25
16
  unless object[object.gmaps4rails_options[:lat_column]].blank? && object[object.gmaps4rails_options[:lng_column]].blank?
26
17
  "{#{Gmaps4rails.description(object)}#{Gmaps4rails.title(object)}#{Gmaps4rails.sidebar(object)}\"longitude\": \"#{object[object.gmaps4rails_options[:lng_column]]}\", \"latitude\": \"#{object[object.gmaps4rails_options[:lat_column]]}\"#{Gmaps4rails.picture(object)}},\n"
@@ -114,7 +105,7 @@ module Gmaps4rails
114
105
  if output == "pretty"
115
106
  #polylines contain levels data, which are not that useful.
116
107
  polylines.map{|poly| poly.delete("levels")}
117
- #creat valid json from all polylines, this could be directly passed to javascript for display
108
+ #create valid json from all polylines, this could be directly passed to javascript for display
118
109
  json = polylines.map { |poly| {"coded_array" => poly["points"]} }.to_json
119
110
  #merge results in legs
120
111
  legs.last.merge!({ "polylines" => json })
@@ -146,18 +137,20 @@ module Gmaps4rails
146
137
 
147
138
  def process_geocoding
148
139
  #to prevent geocoding each time a save is made
149
- return true if gmaps4rails_options[:check_process] == true && self[gmaps4rails_options[:checker]] == true
150
-
140
+ return true if gmaps4rails_options[:check_process] == true && self.send(gmaps4rails_options[:checker]) == true
151
141
  begin
152
- coordinates = Gmaps4rails.geocode(self.gmaps4rails_address)
153
- rescue GeocodeStatus #adress was invalid, add error to base.
154
- errors[:base] << gmaps4rails_options[:msg] if gmaps4rails_options[:validation]
142
+ coordinates = Gmaps4rails.geocode(self.send(gmaps4rails_options[:address]))
143
+ rescue GeocodeStatus #address was invalid, add error to base.
144
+ errors[gmaps4rails_options[:address]] << gmaps4rails_options[:msg] if gmaps4rails_options[:validation]
155
145
  rescue GeocodeNetStatus => e #connection error, No need to prevent save.
156
146
  logger.warn(e)
157
147
  #TODO add customization here?
158
148
  else #if no exception
159
149
  self[gmaps4rails_options[:lng_column]] = coordinates.first[:lng]
160
150
  self[gmaps4rails_options[:lat_column]] = coordinates.first[:lat]
151
+ unless gmaps4rails_options[:normalized_address].nil?
152
+ self.send(gmaps4rails_options[:normalized_address].to_s+"=", coordinates.first[:matched_address])
153
+ end
161
154
  if gmaps4rails_options[:check_process] == true
162
155
  self[gmaps4rails_options[:checker]] = true
163
156
  end
@@ -173,28 +166,23 @@ module Gmaps4rails
173
166
  end
174
167
 
175
168
  module ClassMethods
176
- mattr_accessor :gmaps4rails_options
177
169
 
178
170
  def acts_as_gmappable args = {}
179
171
  unless args[:process_geocoding] == false
180
172
  validate :process_geocoding
181
173
  end
182
-
183
- # [:lat, :lng, :check_process, :checker, :msg, :validation].each do |sym|
184
- # Gmaps4rails::ActsAsGmappable.gmaps4rails_options[sym] = args[sym] unless args[sym].nil?
185
- # end
186
-
187
- Gmaps4rails::ActsAsGmappable::ClassMethods.gmaps4rails_options = args
188
174
 
189
175
  #instance method
190
176
  define_method "gmaps4rails_options" do
191
177
  {
192
- :lat_column => args[:lat] || "latitude",
193
- :lng_column => args[:lng] || "longitude",
194
- :check_process => args[:check_process].nil? ? true : args[:check_process],
195
- :checker => args[:checker] || "gmaps",
196
- :msg => args[:msg] || "Address invalid",
197
- :validation => args[:validation].nil? ? true : args[:validation]
178
+ :lat_column => args[:lat] || "latitude",
179
+ :lng_column => args[:lng] || "longitude",
180
+ :check_process => args[:check_process].nil? ? true : args[:check_process],
181
+ :checker => args[:checker] || "gmaps",
182
+ :msg => args[:msg] || "Address invalid",
183
+ :validation => args[:validation].nil? ? true : args[:validation],
184
+ :normalized_address => args[:normalized_address],
185
+ :address => args[:address] || "gmaps4rails_address"
198
186
  #TODO: address as a proc?
199
187
  }
200
188
  end
data/lib/gmaps4rails.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  if defined?(Rails) && Rails::VERSION::MAJOR == 3
2
2
  module Gmaps4rails
3
3
  require "rails"
4
- require "action_controller"
4
+ #require "action_controller"
5
5
  require 'array'
6
6
  require 'hash'
7
7
  require 'application_helper'
@@ -2,28 +2,28 @@ class User < ActiveRecord::Base
2
2
  acts_as_gmappable
3
3
 
4
4
  def gmaps4rails_address
5
- address
6
- end
7
-
8
- def gmaps4rails_infowindow
9
- "My Beautiful Name: #{name}"
10
- end
11
-
12
- def gmaps4rails_marker_picture
13
- {
14
- "picture" => "http://www.blankdots.com/img/github-32x32.png",
15
- "width" => "32",
16
- "height" => "32"
17
- }
18
- end
19
-
20
- def gmaps4rails_title
21
- "Sweet Title"
22
- end
23
-
24
- def gmaps4rails_sidebar
25
- "sidebar content"
5
+ sec_address
26
6
  end
7
+ #
8
+ # def gmaps4rails_infowindow
9
+ # "My Beautiful Name: #{name}"
10
+ # end
11
+ #
12
+ # def gmaps4rails_marker_picture
13
+ # {
14
+ # "picture" => "http://www.blankdots.com/img/github-32x32.png",
15
+ # "width" => "32",
16
+ # "height" => "32"
17
+ # }
18
+ # end
19
+ #
20
+ # def gmaps4rails_title
21
+ # "Sweet Title"
22
+ # end
23
+ #
24
+ # def gmaps4rails_sidebar
25
+ # "sidebar content"
26
+ # end
27
27
  # def gmaps4rails_sidebar
28
28
  # "<b>#{name}</b>"
29
29
  # end
@@ -0,0 +1,11 @@
1
+ class AddAddressesToUsers < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :users, :norm_address, :string
4
+ add_column :users, :sec_address, :string
5
+ end
6
+
7
+ def self.down
8
+ remove_column :users, :sec_address
9
+ remove_column :users, :norm_address
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class RemoveAddressFromUsers < ActiveRecord::Migration
2
+ def self.up
3
+ remove_column :users, :address
4
+ end
5
+
6
+ def self.down
7
+ add_column :users, :address, :string
8
+ end
9
+ end
@@ -10,11 +10,10 @@
10
10
  #
11
11
  # It's strongly recommended to check this file into your version control system.
12
12
 
13
- ActiveRecord::Schema.define(:version => 20110306182914) do
13
+ ActiveRecord::Schema.define(:version => 20110430083824) do
14
14
 
15
15
  create_table "users", :force => true do |t|
16
16
  t.string "name"
17
- t.string "address"
18
17
  t.text "description"
19
18
  t.string "picture"
20
19
  t.float "lat_test"
@@ -25,6 +24,8 @@ ActiveRecord::Schema.define(:version => 20110306182914) do
25
24
  t.boolean "bool_test"
26
25
  t.datetime "created_at"
27
26
  t.datetime "updated_at"
27
+ t.string "norm_address"
28
+ t.string "sec_address"
28
29
  end
29
30
 
30
31
  end
@@ -1,20 +1,25 @@
1
1
  Factory.define :user do |f|
2
2
  f.name "me"
3
- f.address "Toulon, France"
3
+ f.sec_address "Toulon, France"
4
4
  end
5
5
 
6
6
  Factory.define :user_paris, :parent => :user do |f|
7
7
  f.name "me"
8
- f.address "Paris, France"
8
+ f.sec_address "Paris, France"
9
9
  end
10
10
 
11
11
  Factory.define :user_with_pic, :parent => :user do |f|
12
12
  f.name "me"
13
- f.address "Toulon, France"
13
+ f.sec_address "Toulon, France"
14
14
  f.picture "http://www.blankdots.com/img/github-32x32.png"
15
15
  end
16
16
 
17
17
  Factory.define :invalid_user, :parent => :user do |f|
18
18
  f.name "me"
19
- f.address "home"
19
+ f.sec_address "home"
20
20
  end
21
+
22
+ Factory.define :user_incomplete_address, :parent => :user do |f|
23
+ f.name "me"
24
+ f.sec_address "Toulon, PACA, France"
25
+ end
@@ -12,13 +12,10 @@ describe "Acts as gmappable" do
12
12
  :check_process => true,
13
13
  :checker => "gmaps",
14
14
  :msg => "Address invalid",
15
- :validation => true
15
+ :validation => true,
16
+ :address => "gmaps4rails_address"
16
17
  }
17
18
  end
18
-
19
- def gmaps4rails_address
20
- address
21
- end
22
19
  end
23
20
  end
24
21
 
@@ -46,14 +43,14 @@ describe "Acts as gmappable" do
46
43
  end
47
44
 
48
45
  it "should not geocode again after address changes if checker is true" do
49
- @user.address = "paris, France"
46
+ @user.sec_address = "paris, France"
50
47
  @user.save
51
48
  @user.latitude.should == 43.1251606
52
49
  @user.longitude.should == 5.9311119
53
50
  end
54
51
 
55
52
  it "should geocode after address changes if checker is false" do
56
- @user.address = "paris, France"
53
+ @user.sec_address = "paris, France"
57
54
  @user.gmaps = false
58
55
  @user.save
59
56
  @user.latitude.should == 48.8566667
@@ -77,12 +74,69 @@ describe "Acts as gmappable" do
77
74
  end
78
75
 
79
76
 
80
- describe "model customization" do
77
+ describe "model customization" do
81
78
 
82
79
  it "should render a valid json even if there is no instance in the db" do
83
80
  User.all.to_gmaps4rails.should == "[]"
84
81
  end
85
82
 
83
+ it "should use indifferently a db column for address if passed in config" do
84
+ User.class_eval do
85
+ def gmaps4rails_options
86
+ {
87
+ :lat_column => "latitude",
88
+ :lng_column => "longitude",
89
+ :check_process => true,
90
+ :checker => "gmaps",
91
+ :msg => "Address invalid",
92
+ :validation => true,
93
+ :address => "sec_address"
94
+ }
95
+ end
96
+ end
97
+ @user = Factory(:user)
98
+ @user.latitude.should == 43.1251606
99
+ @user.longitude.should == 5.9311119
100
+ end
101
+
102
+ it "should save the normalized address if requested" do
103
+ User.class_eval do
104
+ def gmaps4rails_options
105
+ {
106
+ :lat_column => "latitude",
107
+ :lng_column => "longitude",
108
+ :check_process => true,
109
+ :checker => "gmaps",
110
+ :msg => "Address invalid",
111
+ :validation => true,
112
+ :normalized_address => "norm_address",
113
+ :address => "gmaps4rails_address"
114
+ }
115
+ end
116
+ end
117
+ @user = Factory(:user)
118
+ @user.norm_address.should == "Toulon, France"
119
+ end
120
+
121
+ it "should override user's address with normalized address if requested" do
122
+ User.class_eval do
123
+ def gmaps4rails_options
124
+ {
125
+ :lat_column => "latitude",
126
+ :lng_column => "longitude",
127
+ :check_process => true,
128
+ :checker => "gmaps",
129
+ :msg => "Custom Address invalid",
130
+ :validation => true,
131
+ :normalized_address => "sec_address",
132
+ :address => "gmaps4rails_address"
133
+ }
134
+ end
135
+ end
136
+ @user = Factory.build(:user)
137
+ @user.sec_address.should == "Toulon, France"
138
+ end
139
+
86
140
  it "should display the proper error message when address is invalid" do
87
141
  User.class_eval do
88
142
  def gmaps4rails_options
@@ -92,7 +146,8 @@ describe "Acts as gmappable" do
92
146
  :check_process => true,
93
147
  :checker => "gmaps",
94
148
  :msg => "Custom Address invalid",
95
- :validation => true
149
+ :validation => true,
150
+ :address => "gmaps4rails_address"
96
151
  }
97
152
  end
98
153
  end
@@ -109,7 +164,8 @@ describe "Acts as gmappable" do
109
164
  :check_process => true,
110
165
  :checker => "gmaps",
111
166
  :msg => "Address invalid",
112
- :validation => false
167
+ :validation => false,
168
+ :address => "gmaps4rails_address"
113
169
  }
114
170
  end
115
171
  end
@@ -126,7 +182,8 @@ describe "Acts as gmappable" do
126
182
  :check_process => true,
127
183
  :checker => "gmaps",
128
184
  :msg => "Address invalid",
129
- :validation => true
185
+ :validation => true,
186
+ :address => "gmaps4rails_address"
130
187
  }
131
188
  end
132
189
  end
@@ -146,7 +203,8 @@ describe "Acts as gmappable" do
146
203
  :check_process => false,
147
204
  :checker => "gmaps",
148
205
  :msg => "Address invalid",
149
- :validation => true
206
+ :validation => true,
207
+ :address => "gmaps4rails_address"
150
208
  }
151
209
  end
152
210
  end
@@ -163,12 +221,13 @@ describe "Acts as gmappable" do
163
221
  :check_process => false,
164
222
  :checker => "gmaps",
165
223
  :msg => "Address invalid",
166
- :validation => true
224
+ :validation => true,
225
+ :address => "gmaps4rails_address"
167
226
  }
168
227
  end
169
228
  end
170
229
  @user = Factory(:user)
171
- @user.address = "paris, France"
230
+ @user.sec_address = "paris, France"
172
231
  @user.save
173
232
  @user.latitude.should == 48.8566667
174
233
  @user.longitude.should == 2.3509871
@@ -183,7 +242,8 @@ describe "Acts as gmappable" do
183
242
  :check_process => true,
184
243
  :checker => "bool_test",
185
244
  :msg => "Address invalid",
186
- :validation => true
245
+ :validation => true,
246
+ :address => "gmaps4rails_address"
187
247
  }
188
248
  end
189
249
  end
@@ -1,23 +1,23 @@
1
- # require File.dirname(__FILE__) + '/../spec_helper'
2
- #
3
- # describe "list creation", :js => true do
4
- #
5
- # it "should create the sidebar with list of users" do
6
- #
7
- # #first setup what the list should display
8
- # User.class_eval do
9
- # def gmaps4rails_sidebar
10
- # name
11
- # end
12
- # end
13
- #
14
- # Factory(:user, :name => "User1")
15
- # Factory(:user, :name => "User2")
16
- #
17
- # visit test_list_path
18
- # page.should have_content("User1")
19
- # page.should have_content("User2")
20
- # end
21
- #
22
- # end
23
- #
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "list creation", :js => true do
4
+
5
+ it "should create the sidebar with list of users" do
6
+
7
+ #first setup what the list should display
8
+ User.class_eval do
9
+ def gmaps4rails_sidebar
10
+ name
11
+ end
12
+ end
13
+
14
+ Factory(:user, :name => "User1")
15
+ Factory(:user, :name => "User2")
16
+
17
+ visit test_list_path
18
+ page.should have_content("User1")
19
+ page.should have_content("User2")
20
+ end
21
+
22
+ end
23
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gmaps4rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 63
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 7
9
- - 9
10
- version: 0.7.9
8
+ - 8
9
+ - 0
10
+ version: 0.8.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Benjamin Roth
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-04-27 00:00:00 +02:00
19
+ date: 2011-04-30 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -43,10 +43,7 @@ extra_rdoc_files:
43
43
  - LICENSE.txt
44
44
  - README.rdoc
45
45
  files:
46
- - app/controllers/gmaps4rails/gmaps_controller.rb
47
- - app/helpers/gmaps4rails/gmaps_helper.rb
48
46
  - app/views/gmaps4rails/_gmaps4rails.html.erb
49
- - config/routes.rb
50
47
  - lib/acts_as_gmappable/base.rb
51
48
  - lib/application_helper.rb
52
49
  - lib/array.rb
@@ -81,6 +78,8 @@ files:
81
78
  - test/dummy/config/initializers/session_store.rb
82
79
  - test/dummy/config/routes.rb
83
80
  - test/dummy/db/migrate/20110306182914_create_users.rb
81
+ - test/dummy/db/migrate/20110430081624_add_addresses_to_users.rb
82
+ - test/dummy/db/migrate/20110430083824_remove_address_from_users.rb
84
83
  - test/dummy/db/schema.rb
85
84
  - test/dummy/db/seeds.rb
86
85
  - test/dummy/spec/base/base_spec.rb
@@ -142,6 +141,8 @@ test_files:
142
141
  - test/dummy/config/initializers/session_store.rb
143
142
  - test/dummy/config/routes.rb
144
143
  - test/dummy/db/migrate/20110306182914_create_users.rb
144
+ - test/dummy/db/migrate/20110430081624_add_addresses_to_users.rb
145
+ - test/dummy/db/migrate/20110430083824_remove_address_from_users.rb
145
146
  - test/dummy/db/schema.rb
146
147
  - test/dummy/db/seeds.rb
147
148
  - test/dummy/spec/base/base_spec.rb
@@ -1,22 +0,0 @@
1
- module Gmaps4rails
2
- class GmapsController < ApplicationController
3
- # unloadable
4
- #
5
- # def index
6
- # @model = params["model"]
7
- # @scope = params["scope"]
8
- # @model = @model.constantize
9
- #
10
- # # The split returns the array [scope_name, arg1, arg2, ...]
11
- # if @scope && !@scope.empty? && @model.gmaps4rails_trusted_scopes.include?(@scope.split(/\(| *, *|\)/)[0])
12
- # object = eval("#{@model}.#{@scope}") # Cannot use send with lambda scope
13
- # # because the arguments have to be separated
14
- # @objects = object.to_gmaps4rails
15
- # else
16
- # @objects = @model.all.to_gmaps4rails
17
- # end
18
- #
19
- # render :json => @objects
20
- # end
21
- end
22
- end
@@ -1,5 +0,0 @@
1
- module Gmaps4rails
2
- module GmapsHelper
3
-
4
- end
5
- end
data/config/routes.rb DELETED
@@ -1,5 +0,0 @@
1
- # Rails.application.routes.draw do
2
- #
3
- # resources :gmaps, :only => [ :index ], :controller => "gmaps4rails/gmaps"
4
- #
5
- # end