acts_as_geocodable 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.0.2
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{acts_as_geocodable}
8
- s.version = "1.0.1"
8
+ s.version = "1.0.2"
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-12-16}
12
+ s.date = %q{2010-02-04}
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 = [
@@ -67,3 +67,4 @@ Gem::Specification.new do |s|
67
67
  else
68
68
  end
69
69
  end
70
+
@@ -139,8 +139,16 @@ module CollectiveIdea #:nodoc:
139
139
  #
140
140
  # Options:
141
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
142
+ # * <tt>:allow_nil</tt>: If all the address attributes are blank, then don't try to
143
+ # validate the geocode (Default: false)
144
+ # * <tt>:precision</tt>: Require a minimum geocoding precision
145
+ #
146
+ # validates_as_geocodable also takes a block that you can use to performa additional
147
+ # checks on the geocode. If this block returns false, then validation will fail.
148
+ #
149
+ # validates_as_geocodable do |geocode|
150
+ # geocode.country == "US"
151
+ # end
144
152
  #
145
153
  def validates_as_geocodable(options = {})
146
154
  options = options.reverse_merge :message => "Address could not be geocoded.", :allow_nil => false
@@ -148,13 +156,15 @@ module CollectiveIdea #:nodoc:
148
156
  is_blank = model.to_location.attributes.except(:precision).all?(&:blank?)
149
157
  unless options[:allow_nil] && is_blank
150
158
  geocode = model.send :attach_geocode
151
- if !geocode || (options[:precision] && options[:precision].to_s != geocode.precision)
159
+ if !geocode ||
160
+ (options[:precision] && geocode.precision < options[:precision]) ||
161
+ (block_given? && yield(geocode) == false)
152
162
  model.errors.add_to_base options[:message]
153
163
  end
154
164
  end
155
165
  end
156
166
  end
157
-
167
+
158
168
  private
159
169
 
160
170
  def add_distance_to_select!(origin, options)
@@ -34,6 +34,10 @@ class Geocode < ActiveRecord::Base
34
34
  nil
35
35
  end
36
36
 
37
+ def precision
38
+ Graticule::Precision.new(self[:precision].to_s)
39
+ end
40
+
37
41
  def precision=(name)
38
42
  self[:precision] = name.to_s
39
43
  end
@@ -184,15 +184,21 @@ class ActsAsGeocodableTest < ActiveSupport::TestCase
184
184
  assert_equal "Address could not be geocoded.", @vacation.errors.on(:base)
185
185
  end
186
186
 
187
- should "be valid with proper precision" do
187
+ should "be valid with the same precision" do
188
188
  @model.validates_as_geocodable :precision => :street
189
189
  Geocode.geocoder.expects(:locate).returns(Graticule::Location.new(:precision => 'street'))
190
190
  assert @vacation.valid?
191
191
  end
192
192
 
193
- should "be invalid without proper precision" do
193
+ should "be valid with a higher precision" do
194
+ @model.validates_as_geocodable :precision => :region
195
+ Geocode.geocoder.expects(:locate).returns(Graticule::Location.new(:precision => 'street'))
196
+ assert @vacation.valid?
197
+ end
198
+
199
+ should "be invalid with a lower precision" do
194
200
  @model.validates_as_geocodable :precision => :street
195
- Geocode.geocoder.expects(:locate).returns(Graticule::Location.new(:precision => 'city'))
201
+ Geocode.geocoder.expects(:locate).returns(Graticule::Location.new(:precision => 'region'))
196
202
  assert !@vacation.valid?
197
203
  assert_equal "Address could not be geocoded.", @vacation.errors.on(:base)
198
204
  end
@@ -201,6 +207,23 @@ class ActsAsGeocodableTest < ActiveSupport::TestCase
201
207
  @model.validates_as_geocodable :allow_nil => true
202
208
  assert @model.new.valid?
203
209
  end
210
+
211
+ should "be invalid if block returns false" do
212
+ @model.validates_as_geocodable(:allow_nil => false) do |geocode|
213
+ ["USA", "US"].include?(geocode.country)
214
+ end
215
+ Geocode.geocoder.expects(:locate).returns(Graticule::Location.new(:country => 'CA'))
216
+ assert !@vacation.valid?
217
+ end
218
+
219
+ should "be valid if block returns true" do
220
+ @model.validates_as_geocodable(:allow_nil => false) do |geocode|
221
+ ["USA", "US"].include?(geocode.country)
222
+ end
223
+ Geocode.geocoder.expects(:locate).returns(Graticule::Location.new(:country => 'US'))
224
+
225
+ assert @vacation.valid?
226
+ end
204
227
  end
205
228
 
206
229
  context "find with origin" do
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.1
4
+ version: 1.0.2
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-12-16 00:00:00 +00:00
13
+ date: 2010-02-04 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies: []
16
16