acts_as_geocodable 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  debug.log
2
- pkg
2
+ pkg
3
+ rdoc
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{acts_as_geocodable}
8
- s.version = "1.0.0"
8
+ s.version = "1.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Daniel Morrison", "Brandon Keepers"]
12
- s.date = %q{2009-10-21}
12
+ s.date = %q{2009-12-16}
13
13
  s.description = %q{Simple geocoding for Rails ActiveRecord models. See the README for more details.}
14
14
  s.email = %q{info@collectiveidea.com}
15
15
  s.extra_rdoc_files = [
@@ -135,12 +135,22 @@ module CollectiveIdea #:nodoc:
135
135
  end
136
136
  end
137
137
 
138
+ # Validate that the model can be geocoded
139
+ #
140
+ # Options:
141
+ # * <tt>:message</tt>: Added to errors base (Default: Address could not be geocoded.)
142
+ # * <tt>:allow_nil</tt>: If all the address attributes are blank, then don't try to validate the geocode (Default: false)
143
+ # * <tt>:precision</tt>: Require a certain geocoding precision
144
+ #
138
145
  def validates_as_geocodable(options = {})
139
146
  options = options.reverse_merge :message => "Address could not be geocoded.", :allow_nil => false
140
- validate do |geocodable|
141
- if !(options[:allow_nil] && geocodable.to_location.attributes.all?(&:blank?)) &&
142
- !Geocode.find_or_create_by_location(geocodable.to_location)
143
- geocodable.errors.add_to_base options[:message]
147
+ validate do |model|
148
+ is_blank = model.to_location.attributes.except(:precision).all?(&:blank?)
149
+ unless options[:allow_nil] && is_blank
150
+ geocode = model.send :attach_geocode
151
+ if !geocode || (options[:precision] && options[:precision].to_s != geocode.precision)
152
+ model.errors.add_to_base options[:message]
153
+ end
144
154
  end
145
155
  end
146
156
  end
@@ -247,12 +257,13 @@ module CollectiveIdea #:nodoc:
247
257
  elsif !new_geocode && self.geocoding
248
258
  self.geocoding.destroy
249
259
  end
260
+ new_geocode
250
261
  rescue Graticule::Error => e
251
262
  logger.warn e.message
252
263
  end
253
264
 
254
265
 
255
- def update_address(force = false)
266
+ def update_address(force = false) #:nodoc:
256
267
  unless self.geocode.blank?
257
268
  if self.acts_as_geocodable_options[:address].is_a? Symbol
258
269
  method = self.acts_as_geocodable_options[:address]
@@ -271,7 +282,7 @@ module CollectiveIdea #:nodoc:
271
282
  end
272
283
  end
273
284
 
274
- def geo_attribute(attr_key)
285
+ def geo_attribute(attr_key) #:nodoc:
275
286
  if self.acts_as_geocodable_options[:address].is_a? Symbol
276
287
  attr_name = self.acts_as_geocodable_options[:address]
277
288
  attr_key == :street ? self.send(attr_name) : nil
@@ -32,9 +32,7 @@ class ActsAsGeocodableTest < ActiveSupport::TestCase
32
32
  fixtures :vacations, :cities, :geocodes, :geocodings
33
33
 
34
34
  # enable Should macros
35
- def self.model_class
36
- Vacation
37
- end
35
+ subject { Vacation.new }
38
36
 
39
37
  should_have_one :geocoding
40
38
 
@@ -169,19 +167,40 @@ class ActsAsGeocodableTest < ActiveSupport::TestCase
169
167
 
170
168
  context "validates_as_geocodable" do
171
169
  setup do
172
- @vacation = ValidatedVacation.new :locality => "Grand Rapids", :region => "MI"
170
+ @model = Class.new(Vacation)
171
+ @vacation = @model.new :locality => "Grand Rapids", :region => "MI"
172
+ end
173
+
174
+ should "be valid with geocodable address" do
175
+ @model.validates_as_geocodable
176
+ assert @vacation.valid?
173
177
  end
174
178
 
175
179
  should "be invalid without geocodable address" do
180
+ @model.validates_as_geocodable
176
181
  Geocode.geocoder.expects(:locate).raises(Graticule::Error)
177
182
  assert !@vacation.valid?
178
183
  assert_equal 1, @vacation.errors.size
179
184
  assert_equal "Address could not be geocoded.", @vacation.errors.on(:base)
180
185
  end
181
186
 
182
- should "be valid with geocodable address" do
187
+ should "be valid with proper precision" do
188
+ @model.validates_as_geocodable :precision => :street
189
+ Geocode.geocoder.expects(:locate).returns(Graticule::Location.new(:precision => 'street'))
183
190
  assert @vacation.valid?
184
191
  end
192
+
193
+ should "be invalid without proper precision" do
194
+ @model.validates_as_geocodable :precision => :street
195
+ Geocode.geocoder.expects(:locate).returns(Graticule::Location.new(:precision => 'city'))
196
+ assert !@vacation.valid?
197
+ assert_equal "Address could not be geocoded.", @vacation.errors.on(:base)
198
+ end
199
+
200
+ should "allow nil" do
201
+ @model.validates_as_geocodable :allow_nil => true
202
+ assert @model.new.valid?
203
+ end
185
204
  end
186
205
 
187
206
  context "find with origin" do
data/test/test_helper.rb CHANGED
@@ -12,7 +12,7 @@ require 'shoulda'
12
12
  require 'matchy'
13
13
  require 'mocha'
14
14
 
15
- require plugin_test_dir + '/../init.rb'
15
+ require plugin_test_dir + '/../rails/init.rb'
16
16
 
17
17
  ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log")
18
18
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_geocodable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Morrison
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-10-21 00:00:00 +01:00
13
+ date: 2009-12-16 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies: []
16
16