drifter-rails 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/README +72 -0
- data/README.rdoc +72 -0
- data/Rakefile +38 -0
- data/drifter-rails.gemspec +27 -0
- data/lib/drifter-rails.rb +27 -0
- data/lib/drifter-rails/class_methods.rb +49 -0
- data/lib/drifter-rails/instance_methods.rb +47 -0
- data/lib/drifter-rails/version.rb +5 -0
- data/test/acts_as_drifter_test.rb +34 -0
- data/test/database.yml +12 -0
- data/test/near_test.rb +75 -0
- data/test/test_helper.rb +120 -0
- metadata +167 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
== drifter-rails
|
2
|
+
|
3
|
+
drifter-rails is a simple gem that provides distance calculations and queries
|
4
|
+
for ActiveRecord. It currently supports postgresql and mysql on rails 3.
|
5
|
+
|
6
|
+
=== Installation
|
7
|
+
|
8
|
+
# in Gemfile
|
9
|
+
config.gem 'drifter-rails'
|
10
|
+
|
11
|
+
# optionally, in config/intiailizers/drifter.rb, one of these:
|
12
|
+
Drifter.default_units = :miles
|
13
|
+
Drifter.default_units = :km
|
14
|
+
|
15
|
+
# in your model
|
16
|
+
class Place < ActiveRecord::Base
|
17
|
+
acts_as_drifter
|
18
|
+
end
|
19
|
+
|
20
|
+
=== Usage
|
21
|
+
|
22
|
+
To return a calculated 'distance' value along with your queries:
|
23
|
+
|
24
|
+
>> origin = [52.123, -2.5432]
|
25
|
+
>> results = Place.near(origin)
|
26
|
+
>> results.first.distance
|
27
|
+
=> 12.33228393
|
28
|
+
|
29
|
+
origin can be a two item [lat,lng] array, any acts_as_drifter object or any object that
|
30
|
+
responds to lat() and lng().
|
31
|
+
|
32
|
+
To return objects within a specific distance of a point of origin:
|
33
|
+
|
34
|
+
>> origin = [52.123, -2.5432]
|
35
|
+
>> Place.near(origin, :within => 10)
|
36
|
+
>> Place.near(origin, :within => 10, :units => :km)
|
37
|
+
=> "#<ActiveRecord::Relation:0xafb1e18>"
|
38
|
+
|
39
|
+
The near() method returns an ActiveRecord::Relation that you can use the same as any
|
40
|
+
other scope, including ordering by distance:
|
41
|
+
|
42
|
+
>> Place.near(origin, :within => 10).includes(:visitors).order("distance ASC")
|
43
|
+
=> "#<ActiveRecord::Relation:0xafb1e18>"
|
44
|
+
|
45
|
+
Each acts_as_drifter object also gets a distance_to() method that can calculate the
|
46
|
+
distance to another location:
|
47
|
+
|
48
|
+
>> place = Place.first
|
49
|
+
>> place.distance_to(other_place)
|
50
|
+
>> place.distance_to( [10.321, -5.432] )
|
51
|
+
>> place.distance_to( [10.321, -5.432], :units => :km )
|
52
|
+
|
53
|
+
The location() and location=() methods provide a shortcut for setting lat() and lng():
|
54
|
+
|
55
|
+
>> place = Place.new
|
56
|
+
>> place.location = [2.222, 3.333]
|
57
|
+
|
58
|
+
>> place.lat
|
59
|
+
=> 2.222
|
60
|
+
>> place.lng
|
61
|
+
=> 3.333
|
62
|
+
|
63
|
+
Finally, you can use custom names for the lat and lng columns:
|
64
|
+
|
65
|
+
class Place < ActiveRecord::Base
|
66
|
+
acts_as_drifter :lat_column => :latitude, :lng_column => :longitude
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
=== License
|
71
|
+
|
72
|
+
MIT License. Copyright 2011 Ahmed Adam (http://github.com/ahmedrb)
|
data/README.rdoc
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
== drifter-rails
|
2
|
+
|
3
|
+
drifter-rails is a simple gem that provides distance calculations and queries
|
4
|
+
for ActiveRecord. It currently supports postgresql and mysql on rails 3.
|
5
|
+
|
6
|
+
=== Installation
|
7
|
+
|
8
|
+
# in Gemfile
|
9
|
+
config.gem 'drifter-rails'
|
10
|
+
|
11
|
+
# optionally, in config/intiailizers/drifter.rb, one of these:
|
12
|
+
Drifter.default_units = :miles
|
13
|
+
Drifter.default_units = :km
|
14
|
+
|
15
|
+
# in your model
|
16
|
+
class Place < ActiveRecord::Base
|
17
|
+
acts_as_drifter
|
18
|
+
end
|
19
|
+
|
20
|
+
=== Usage
|
21
|
+
|
22
|
+
To return a calculated 'distance' value along with your queries:
|
23
|
+
|
24
|
+
>> origin = [52.123, -2.5432]
|
25
|
+
>> results = Place.near(origin)
|
26
|
+
>> results.first.distance
|
27
|
+
=> 12.33228393
|
28
|
+
|
29
|
+
origin can be a two item [lat,lng] array, any acts_as_drifter object or any object that
|
30
|
+
responds to lat() and lng().
|
31
|
+
|
32
|
+
To return objects within a specific distance of a point of origin:
|
33
|
+
|
34
|
+
>> origin = [52.123, -2.5432]
|
35
|
+
>> Place.near(origin, :within => 10)
|
36
|
+
>> Place.near(origin, :within => 10, :units => :km)
|
37
|
+
=> "#<ActiveRecord::Relation:0xafb1e18>"
|
38
|
+
|
39
|
+
The near() method returns an ActiveRecord::Relation that you can use the same as any
|
40
|
+
other scope, including ordering by distance:
|
41
|
+
|
42
|
+
>> Place.near(origin, :within => 10).includes(:visitors).order("distance ASC")
|
43
|
+
=> "#<ActiveRecord::Relation:0xafb1e18>"
|
44
|
+
|
45
|
+
Each acts_as_drifter object also gets a distance_to() method that can calculate the
|
46
|
+
distance to another location:
|
47
|
+
|
48
|
+
>> place = Place.first
|
49
|
+
>> place.distance_to(other_place)
|
50
|
+
>> place.distance_to( [10.321, -5.432] )
|
51
|
+
>> place.distance_to( [10.321, -5.432], :units => :km )
|
52
|
+
|
53
|
+
The location() and location=() methods provide a shortcut for setting lat() and lng():
|
54
|
+
|
55
|
+
>> place = Place.new
|
56
|
+
>> place.location = [2.222, 3.333]
|
57
|
+
|
58
|
+
>> place.lat
|
59
|
+
=> 2.222
|
60
|
+
>> place.lng
|
61
|
+
=> 3.333
|
62
|
+
|
63
|
+
Finally, you can use custom names for the lat and lng columns:
|
64
|
+
|
65
|
+
class Place < ActiveRecord::Base
|
66
|
+
acts_as_drifter :lat_column => :latitude, :lng_column => :longitude
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
=== License
|
71
|
+
|
72
|
+
MIT License. Copyright 2011 Ahmed Adam (http://github.com/ahmedrb)
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
|
5
|
+
|
6
|
+
def run_tests(name)
|
7
|
+
task_name = "test_#{name}"
|
8
|
+
Rake::TestTask.new(task_name) do |t|
|
9
|
+
t.test_files = FileList['test/*_test.rb']
|
10
|
+
t.verbose = true
|
11
|
+
end
|
12
|
+
Rake::Task[task_name].invoke
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
namespace :test do
|
17
|
+
|
18
|
+
desc "Run tests using the mysql adapater"
|
19
|
+
task :mysql do
|
20
|
+
puts "\nRunning test using mysql\n\n"
|
21
|
+
ENV['DB'] = 'mysql'
|
22
|
+
run_tests :mysql
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Run tests using the postgresql adapater"
|
26
|
+
task :postgresql do
|
27
|
+
puts "\nRunning test using postgresql\n\n"
|
28
|
+
ENV['DB'] = 'postgresql'
|
29
|
+
run_tests :postgresql
|
30
|
+
end
|
31
|
+
|
32
|
+
desc "Run tests using each db adapter in succession"
|
33
|
+
task :all do
|
34
|
+
Rake::Task["test:mysql"].invoke
|
35
|
+
Rake::Task["test:postgresql"].invoke
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "drifter-rails/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "drifter-rails"
|
7
|
+
s.version = Drifter::Rails::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Ahmed Adam"]
|
10
|
+
s.homepage = "http://github.com/ahmedrb/drifter-rails"
|
11
|
+
s.summary = %q{Distance queries for ActiveRecord 3}
|
12
|
+
|
13
|
+
s.rubyforge_project = "drifter-rails"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency 'drifter', '0.1.0'
|
21
|
+
s.add_dependency 'activerecord', '~> 3.0.0'
|
22
|
+
|
23
|
+
s.add_development_dependency 'mysql'
|
24
|
+
s.add_development_dependency 'pg'
|
25
|
+
s.add_development_dependency 'ruby-debug'
|
26
|
+
s.add_development_dependency 'yaml'
|
27
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'drifter'
|
3
|
+
require 'drifter-rails/class_methods'
|
4
|
+
require 'drifter-rails/instance_methods'
|
5
|
+
|
6
|
+
module Drifter
|
7
|
+
module Rails
|
8
|
+
|
9
|
+
# supported_options:
|
10
|
+
# :lat_column - the name of the column holding latitude values (default is lat)
|
11
|
+
# :lng_column - the name of the column holding longitude values (default is lng)
|
12
|
+
def acts_as_drifter(options={})
|
13
|
+
# determine the names of the lat and lng columns
|
14
|
+
lat_col = options[:lat_column] || :lat
|
15
|
+
lng_col = options[:lng_column] || :lng
|
16
|
+
|
17
|
+
eval("def self.lat_column_name; :#{lat_col}; end")
|
18
|
+
eval("def self.lng_column_name; :#{lng_col}; end")
|
19
|
+
|
20
|
+
extend Drifter::Rails::ClassMethods
|
21
|
+
include Drifter::Rails::InstanceMethods
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
ActiveRecord::Base.send :extend, Drifter::Rails
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Drifter
|
2
|
+
module Rails
|
3
|
+
module ClassMethods
|
4
|
+
|
5
|
+
# used by InstanceMethods to determine if lat/lng accessors need to be defined
|
6
|
+
def has_method?(name)
|
7
|
+
return true if column_names.include?(name.to_s)
|
8
|
+
return true if instance_methods.include?(name.to_s)
|
9
|
+
return false
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
# nodoc
|
14
|
+
def near(origin, options={})
|
15
|
+
options[:origin] = origin
|
16
|
+
options[:lat_column] = lat_column_name
|
17
|
+
options[:lng_column] = lng_column_name
|
18
|
+
|
19
|
+
adapter = self.connection.class.to_s.split('::').last
|
20
|
+
method_name = case adapter
|
21
|
+
when 'PostgreSQLAdapter' then :to_postgresql
|
22
|
+
when 'MysqlAdapter' then :to_mysql
|
23
|
+
else raise ArgumentError, "#{adapter} is not supported by drifter-rails"
|
24
|
+
end
|
25
|
+
|
26
|
+
distance_sql = Drifter::Distance::Haversine.send(method_name, options)
|
27
|
+
sql = "#{table_name}.*, (#{distance_sql}) AS distance"
|
28
|
+
|
29
|
+
scope = select(sql)
|
30
|
+
scope = add_conditions_for_within(scope, options, distance_sql)
|
31
|
+
return scope
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
|
38
|
+
# TODO: 'distance' column not visible to the where clause (postgres and mysql)
|
39
|
+
# so the calculation has to be repeated in the where clause. Can that be fixed?
|
40
|
+
def add_conditions_for_within(scope, options, distance_sql)
|
41
|
+
max_distance = options[:within].to_i
|
42
|
+
return scope unless max_distance > 0
|
43
|
+
return scope.where("(#{distance_sql}) < #{max_distance}")
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Drifter
|
2
|
+
module Rails
|
3
|
+
module InstanceMethods
|
4
|
+
|
5
|
+
# nodoc
|
6
|
+
def self.included(base)
|
7
|
+
base.send :include, Drifter::Location::Locatable
|
8
|
+
base.send :include, LatAccessors unless base.has_method?(:lat)
|
9
|
+
base.send :include, LngAccessors unless base.has_method?(:lng)
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
# nodoc
|
14
|
+
module LatAccessors
|
15
|
+
|
16
|
+
# nodoc
|
17
|
+
def lat
|
18
|
+
self.send self.class.lat_column_name
|
19
|
+
end
|
20
|
+
|
21
|
+
# nodoc
|
22
|
+
def lat=(value)
|
23
|
+
writer = "#{self.class.lat_column_name}="
|
24
|
+
self.send writer, value
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
# nodoc
|
30
|
+
module LngAccessors
|
31
|
+
|
32
|
+
# nodoc
|
33
|
+
def lng
|
34
|
+
self.send self.class.lng_column_name
|
35
|
+
end
|
36
|
+
|
37
|
+
# nodoc
|
38
|
+
def lng=(value)
|
39
|
+
writer = "#{self.class.lng_column_name}="
|
40
|
+
self.send writer, value
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
class ActsAsDrifterTest < Test::Unit::TestCase
|
3
|
+
|
4
|
+
|
5
|
+
def test_lat_column_id_method
|
6
|
+
Place.send :acts_as_drifter
|
7
|
+
assert_equal :lat, Place.lat_column_name
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_lat_lng_accessors_with_default_column_names
|
11
|
+
Place.send :acts_as_drifter
|
12
|
+
i = Place.new :lat => 1, :lng => 2
|
13
|
+
assert_equal 1, i.lat
|
14
|
+
assert_equal 2, i.lng
|
15
|
+
|
16
|
+
i.lat= 10
|
17
|
+
i.lng= 20
|
18
|
+
assert_equal 10, i.lat
|
19
|
+
assert_equal 20, i.lng
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_lat_lng_accessors_with_custom_column_names
|
23
|
+
Store.send :acts_as_drifter, :lat_column => :latitude, :lng_column => :longitude
|
24
|
+
i = Store.new :latitude => 1, :longitude => 2
|
25
|
+
assert_equal 1, i.lat
|
26
|
+
assert_equal 2, i.lng
|
27
|
+
|
28
|
+
i.lat= 10
|
29
|
+
i.lng= 20
|
30
|
+
assert_equal 10, i.latitude
|
31
|
+
assert_equal 20, i.longitude
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/test/database.yml
ADDED
data/test/near_test.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
# TODO: better fixtures
|
4
|
+
class NearTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
# request places in ascending/descending order and checks that the order is correct
|
7
|
+
# TODO: this is tied very closely to the fixtures, too brittle
|
8
|
+
def check_order(order, klass)
|
9
|
+
order_by = "distance ASC"
|
10
|
+
origin = [53.57, -2.4333] # nr bolton
|
11
|
+
expected = ["Bolton", "Manchester", "Chester", "Nottingham", "London"]
|
12
|
+
|
13
|
+
if order == :desc
|
14
|
+
order_by = "distance DESC"
|
15
|
+
expected.reverse!
|
16
|
+
end
|
17
|
+
|
18
|
+
results = klass.near(origin).order(order_by)
|
19
|
+
names = results.collect { |p| p.name }
|
20
|
+
assert_equal expected, names
|
21
|
+
|
22
|
+
# check the distances too
|
23
|
+
distances = results.collect { |p| p.distance.to_f }
|
24
|
+
expected = distances.sort
|
25
|
+
expected = distances.sort.reverse if order == :desc
|
26
|
+
assert_equal expected, distances
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
# check the 'distance' column isn't dropped
|
31
|
+
def test_distance_attribute_with_includes_option
|
32
|
+
Place.send :acts_as_drifter
|
33
|
+
origin = [53.5, -2.4]
|
34
|
+
place = Place.near(origin).first
|
35
|
+
|
36
|
+
assert_not_nil place.distance
|
37
|
+
assert place.distance.to_f > 1
|
38
|
+
|
39
|
+
other = Place.near(origin).includes(:visitors).first
|
40
|
+
assert_equal place.distance, other.distance
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def test_order_by_distance_using_default_column_names
|
45
|
+
Place.send :acts_as_drifter
|
46
|
+
check_order :asc, Place
|
47
|
+
check_order :desc, Place
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
def test_order_by_distance_using_custom_column_names
|
52
|
+
Store.send :acts_as_drifter, :lat_column => :latitude, :lng_column => :longitude
|
53
|
+
check_order :asc, Store
|
54
|
+
check_order :desc, Store
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
def test_near_plus_within_using_default_column_names
|
59
|
+
Place.send :acts_as_drifter
|
60
|
+
origin = [53.57, -2.4333] # nr bolton
|
61
|
+
results = Place.near(origin, :within => 50)
|
62
|
+
assert results.count == 3
|
63
|
+
results.each { |r| assert r.distance.to_f <= 50 }
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
def test_near_plus_within_using_custom_column_names
|
68
|
+
Store.send :acts_as_drifter, :lat_column => :latitude, :lng_column => :longitude
|
69
|
+
origin = [53.57, -2.4333] # nr bolton
|
70
|
+
results = Store.near(origin, :within => 50)
|
71
|
+
assert results.count == 3
|
72
|
+
results.each { |r| assert r.distance.to_f <= 50 }
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
# these library's will be loaded when the drifter-rails gem is loaded
|
2
|
+
require 'rubygems'
|
3
|
+
require 'active_record'
|
4
|
+
require 'drifter'
|
5
|
+
require 'pg'
|
6
|
+
require 'yaml'
|
7
|
+
|
8
|
+
# these are for testing only
|
9
|
+
require 'ruby-debug'
|
10
|
+
require 'test/unit'
|
11
|
+
|
12
|
+
|
13
|
+
# AR::Base classes used for the tests, this one has lat, lng columns
|
14
|
+
class Place < ActiveRecord::Base
|
15
|
+
has_many :visitors
|
16
|
+
end
|
17
|
+
|
18
|
+
# same as Place but with latitude and longitude columns
|
19
|
+
class Store < ActiveRecord::Base
|
20
|
+
has_many :visitors
|
21
|
+
end
|
22
|
+
|
23
|
+
class Visitor < ActiveRecord::Base
|
24
|
+
belongs_to :place
|
25
|
+
belongs_to :visitor
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
# this migration is used to prepare the database for tests
|
30
|
+
class PrepareDB < ActiveRecord::Migration
|
31
|
+
|
32
|
+
def self.up
|
33
|
+
|
34
|
+
drop_table :places if ActiveRecord::Base.connection.tables.include?("places")
|
35
|
+
drop_table :stores if ActiveRecord::Base.connection.tables.include?("stores")
|
36
|
+
drop_table :visitors if ActiveRecord::Base.connection.tables.include?("visitors")
|
37
|
+
|
38
|
+
create_table :places do |t|
|
39
|
+
t.string :name
|
40
|
+
t.decimal :lat, :precision => 16, :scale => 12
|
41
|
+
t.decimal :lng, :precision => 16, :scale => 12
|
42
|
+
end
|
43
|
+
create_table :stores do |t|
|
44
|
+
t.string :name
|
45
|
+
t.decimal :latitude, :precision => 16, :scale => 12
|
46
|
+
t.decimal :longitude, :precision => 16, :scale => 12
|
47
|
+
end
|
48
|
+
create_table :visitors do |t|
|
49
|
+
t.integer :place_id
|
50
|
+
t.integer :store_id
|
51
|
+
t.string :name
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
# connects the given class to the given database. Connections params should be defined in database.yml
|
59
|
+
def connect_to(db)
|
60
|
+
data = YAML.load_file(File.dirname(__FILE__) + '/database.yml')
|
61
|
+
db = data[db]
|
62
|
+
ActiveRecord::Base.remove_connection if ActiveRecord::Base.connected?
|
63
|
+
ActiveRecord::Base.establish_connection(
|
64
|
+
:adapter => db[:adapter],
|
65
|
+
:database => db[:name],
|
66
|
+
:username => db[:username],
|
67
|
+
:password => db[:password]
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
def places_attributes(lat=:lat, lng =:lng)
|
72
|
+
attrs = [
|
73
|
+
{ :name => "Bolton", lat => 53.57845, lng => -2.426664 },
|
74
|
+
{ :name => "Chester", lat => 53.18813, lng => -2.894369 },
|
75
|
+
{ :name => "Manchester", lat => 53.417439, lng => -2.23156 },
|
76
|
+
{ :name => "Nottingham", lat => 52.951199, lng => -1.13087 },
|
77
|
+
{ :name => "London", lat => 51.5001524, lng => -0.1262362 }
|
78
|
+
]
|
79
|
+
end
|
80
|
+
|
81
|
+
def add_places(attrs=false)
|
82
|
+
Place.delete_all
|
83
|
+
Place.create! places_attributes
|
84
|
+
end
|
85
|
+
|
86
|
+
def add_stores
|
87
|
+
attrs = places_attributes(:latitude, :longitude)
|
88
|
+
Store.delete_all
|
89
|
+
Store.create! attrs
|
90
|
+
end
|
91
|
+
|
92
|
+
def add_visitors
|
93
|
+
places = Place.all
|
94
|
+
stores = Store.all
|
95
|
+
%W(john mike jane emma paul).each do |name|
|
96
|
+
place_id = places[rand(places.size)].id
|
97
|
+
store_id = stores[rand(stores.size)].id
|
98
|
+
Visitor.create! :name => name, :place_id => place_id, :store_id => store_id
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# the first time this file is loaded it connects to a db and runs the migration
|
103
|
+
# specify the db using the DB environment variable ['mysql', 'postgresql']
|
104
|
+
unless ActiveRecord::Base.connected?
|
105
|
+
db = ENV['DB'] || :postgresql
|
106
|
+
connect_to db.to_s.intern
|
107
|
+
PrepareDB.verbose = false
|
108
|
+
puts "Running migrations..."
|
109
|
+
PrepareDB.migrate :up
|
110
|
+
add_places
|
111
|
+
add_stores
|
112
|
+
add_visitors
|
113
|
+
end
|
114
|
+
|
115
|
+
# this will be set when loaded as a gem. Only needed for tests
|
116
|
+
$LOAD_PATH << File.expand_path('../../lib/', __FILE__)
|
117
|
+
|
118
|
+
# acts_as_drifter queries column names so this has to come after the connection
|
119
|
+
# is established
|
120
|
+
require 'drifter-rails'
|
metadata
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: drifter-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ahmed Adam
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-22 00:00:00 +00:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: drifter
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - "="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 27
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 1
|
33
|
+
- 0
|
34
|
+
version: 0.1.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: activerecord
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 7
|
46
|
+
segments:
|
47
|
+
- 3
|
48
|
+
- 0
|
49
|
+
- 0
|
50
|
+
version: 3.0.0
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: mysql
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
type: :development
|
66
|
+
version_requirements: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: pg
|
69
|
+
prerelease: false
|
70
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
type: :development
|
80
|
+
version_requirements: *id004
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: ruby-debug
|
83
|
+
prerelease: false
|
84
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
type: :development
|
94
|
+
version_requirements: *id005
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: yaml
|
97
|
+
prerelease: false
|
98
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
hash: 3
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
version: "0"
|
107
|
+
type: :development
|
108
|
+
version_requirements: *id006
|
109
|
+
description:
|
110
|
+
email:
|
111
|
+
executables: []
|
112
|
+
|
113
|
+
extensions: []
|
114
|
+
|
115
|
+
extra_rdoc_files: []
|
116
|
+
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- Gemfile
|
120
|
+
- README
|
121
|
+
- README.rdoc
|
122
|
+
- Rakefile
|
123
|
+
- drifter-rails.gemspec
|
124
|
+
- lib/drifter-rails.rb
|
125
|
+
- lib/drifter-rails/class_methods.rb
|
126
|
+
- lib/drifter-rails/instance_methods.rb
|
127
|
+
- lib/drifter-rails/version.rb
|
128
|
+
- test/acts_as_drifter_test.rb
|
129
|
+
- test/database.yml
|
130
|
+
- test/near_test.rb
|
131
|
+
- test/test_helper.rb
|
132
|
+
has_rdoc: true
|
133
|
+
homepage: http://github.com/ahmedrb/drifter-rails
|
134
|
+
licenses: []
|
135
|
+
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options: []
|
138
|
+
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
none: false
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
hash: 3
|
147
|
+
segments:
|
148
|
+
- 0
|
149
|
+
version: "0"
|
150
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
|
+
none: false
|
152
|
+
requirements:
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
hash: 3
|
156
|
+
segments:
|
157
|
+
- 0
|
158
|
+
version: "0"
|
159
|
+
requirements: []
|
160
|
+
|
161
|
+
rubyforge_project: drifter-rails
|
162
|
+
rubygems_version: 1.3.7
|
163
|
+
signing_key:
|
164
|
+
specification_version: 3
|
165
|
+
summary: Distance queries for ActiveRecord 3
|
166
|
+
test_files: []
|
167
|
+
|