cb_mongoid_spacial 0.2.16
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +49 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +20 -0
- data/README.md +180 -0
- data/Rakefile +18 -0
- data/lib/mongoid_spacial.rb +11 -0
- data/lib/mongoid_spacial/contexts/mongo.rb +115 -0
- data/lib/mongoid_spacial/criteria.rb +5 -0
- data/lib/mongoid_spacial/criterion.rb +3 -0
- data/lib/mongoid_spacial/criterion/complex.rb +19 -0
- data/lib/mongoid_spacial/criterion/inclusion.rb +14 -0
- data/lib/mongoid_spacial/criterion/near_spacial.rb +50 -0
- data/lib/mongoid_spacial/criterion/within_spacial.rb +60 -0
- data/lib/mongoid_spacial/extentions/hash/criteria_helpers.rb +22 -0
- data/lib/mongoid_spacial/extentions/symbol/inflections.rb +46 -0
- data/lib/mongoid_spacial/field_option.rb +38 -0
- data/lib/mongoid_spacial/finders.rb +5 -0
- data/lib/mongoid_spacial/spacial.rb +56 -0
- data/lib/mongoid_spacial/spacial/core_ext.rb +27 -0
- data/lib/mongoid_spacial/spacial/document.rb +29 -0
- data/lib/mongoid_spacial/spacial/formulas.rb +52 -0
- data/lib/mongoid_spacial/spacial/geo_near_results.rb +140 -0
- data/lib/mongoid_spacial/spacial/version.rb +5 -0
- data/mongoid_spacial.gemspec +27 -0
- data/spec/config/mongod.conf +3 -0
- data/spec/config/mongoid.yml +18 -0
- data/spec/functional/mongoid/contexts/mongo_spec.rb +125 -0
- data/spec/functional/mongoid/criterion/inclusion_spec.rb +356 -0
- data/spec/functional/mongoid/spacial/geo_near_results_spec.rb +73 -0
- data/spec/functional/mongoid/spacial_spec.rb +18 -0
- data/spec/models/account.rb +19 -0
- data/spec/models/acolyte.rb +9 -0
- data/spec/models/address.rb +62 -0
- data/spec/models/address_component.rb +5 -0
- data/spec/models/agent.rb +10 -0
- data/spec/models/alert.rb +5 -0
- data/spec/models/animal.rb +21 -0
- data/spec/models/answer.rb +4 -0
- data/spec/models/bar.rb +9 -0
- data/spec/models/birthday.rb +13 -0
- data/spec/models/book.rb +5 -0
- data/spec/models/business.rb +7 -0
- data/spec/models/callbacks.rb +57 -0
- data/spec/models/category.rb +13 -0
- data/spec/models/circus.rb +7 -0
- data/spec/models/comment.rb +13 -0
- data/spec/models/country_code.rb +6 -0
- data/spec/models/description.rb +11 -0
- data/spec/models/division.rb +5 -0
- data/spec/models/drug.rb +5 -0
- data/spec/models/employer.rb +5 -0
- data/spec/models/entry.rb +6 -0
- data/spec/models/event.rb +20 -0
- data/spec/models/favorite.rb +6 -0
- data/spec/models/fruits.rb +11 -0
- data/spec/models/game.rb +18 -0
- data/spec/models/ghost.rb +7 -0
- data/spec/models/house.rb +4 -0
- data/spec/models/inheritance.rb +90 -0
- data/spec/models/league.rb +5 -0
- data/spec/models/location.rb +5 -0
- data/spec/models/login.rb +6 -0
- data/spec/models/membership.rb +4 -0
- data/spec/models/mixed_drink.rb +4 -0
- data/spec/models/name.rb +13 -0
- data/spec/models/namespacing.rb +11 -0
- data/spec/models/observed.rb +41 -0
- data/spec/models/override.rb +16 -0
- data/spec/models/owner.rb +6 -0
- data/spec/models/page.rb +5 -0
- data/spec/models/page_question.rb +4 -0
- data/spec/models/paranoid_post.rb +18 -0
- data/spec/models/parents.rb +32 -0
- data/spec/models/patient.rb +15 -0
- data/spec/models/person.rb +146 -0
- data/spec/models/pet.rb +7 -0
- data/spec/models/pet_owner.rb +6 -0
- data/spec/models/phone.rb +7 -0
- data/spec/models/player.rb +23 -0
- data/spec/models/post.rb +26 -0
- data/spec/models/preference.rb +9 -0
- data/spec/models/question.rb +8 -0
- data/spec/models/quiz.rb +6 -0
- data/spec/models/rating.rb +8 -0
- data/spec/models/river.rb +20 -0
- data/spec/models/role.rb +5 -0
- data/spec/models/service.rb +6 -0
- data/spec/models/shelf.rb +5 -0
- data/spec/models/slave_address_numbers.rb +14 -0
- data/spec/models/survey.rb +5 -0
- data/spec/models/tag.rb +6 -0
- data/spec/models/tracking_id_validation_history.rb +25 -0
- data/spec/models/translation.rb +5 -0
- data/spec/models/tree.rb +9 -0
- data/spec/models/user.rb +9 -0
- data/spec/models/user_account.rb +10 -0
- data/spec/models/vet_visit.rb +5 -0
- data/spec/models/video.rb +9 -0
- data/spec/models/wiki_page.rb +6 -0
- data/spec/spec_helper.rb +43 -0
- data/spec/support/authentication.rb +29 -0
- data/spec/unit/mongoid/criterion/complex_spec.rb +15 -0
- data/spec/unit/mongoid/criterion/inclusion_spec.rb +0 -0
- data/spec/unit/mongoid/criterion/near_spacial_spec.rb +39 -0
- data/spec/unit/mongoid/criterion/within_spacial_spec.rb +52 -0
- data/spec/unit/mongoid/spacial/formulas_spec.rb +37 -0
- data/spec/unit/mongoid/spacial_spec.rb +6 -0
- metadata +374 -0
@@ -0,0 +1,73 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Spacial::GeoNearResults do
|
4
|
+
before(:all) do
|
5
|
+
Bar.delete_all
|
6
|
+
Bar.create_indexes
|
7
|
+
|
8
|
+
50.times do |i|
|
9
|
+
Bar.create(:name => i.to_s, :location => [rand(358)-179,rand(358)-179])
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
before(:each) do
|
14
|
+
while Bar.count < 50
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context ":paginator :array" do
|
19
|
+
let(:bars) { Bar.geo_near([1,1]) }
|
20
|
+
let(:sorted_bars) {
|
21
|
+
bars = Bar.geo_near([1,1])
|
22
|
+
bars.sort_by! {|b| b.name.to_i}
|
23
|
+
bars
|
24
|
+
}
|
25
|
+
[nil,1,2].each do |page|
|
26
|
+
it "page=#{page} should have 25" do
|
27
|
+
bars.page(page).size.should == 25
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
[1,2].each do |page|
|
32
|
+
it "modified result should keep order after pagination" do
|
33
|
+
sorted_bars.page(page).should == sorted_bars.slice((page-1)*25,25)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
{ nil => 25, 20 => 20 , 30 => 20, 50 => 0}.each do |per, total|
|
38
|
+
it "page=2 per=#{per} should have #{total}" do
|
39
|
+
bars.per(per).page(2).size.should == total
|
40
|
+
bars.page(2).per(per).size.should == total
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it "page=3 should have 0" do
|
45
|
+
bars.page(3).size.should == 0
|
46
|
+
end
|
47
|
+
|
48
|
+
it "per=5" do
|
49
|
+
bars.per(5).size.should == 5
|
50
|
+
end
|
51
|
+
|
52
|
+
it "page=10 per=5" do
|
53
|
+
bars.per(5).page(10).should == bars[45..50]
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
context ":paginator :kaminari" do
|
59
|
+
let!(:near) {Bar.geo_near([1,1]).page(1)}
|
60
|
+
it "should have current_page" do
|
61
|
+
near.current_page.should == 1
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should have num_pages" do
|
65
|
+
near.total_entries.should == 50
|
66
|
+
near.num_pages.should == 2
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should have limit_value" do
|
70
|
+
near.limit_value.should == 25
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Spacial do
|
4
|
+
describe '#distance' do
|
5
|
+
it "should calculate 2d by default" do
|
6
|
+
Mongoid::Spacial.distance([0,0],[3,4]).should == 5
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should calculate 2d distances using degrees" do
|
10
|
+
Mongoid::Spacial.distance([0,0],[3,4], :unit=>:mi).should == 5*Mongoid::Spacial::EARTH_RADIUS[:mi]*Mongoid::Spacial::RAD_PER_DEG
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should calculate 3d distances by default" do
|
14
|
+
Mongoid::Spacial.distance([-73.77694444, 40.63861111 ],[-118.40, 33.94],:unit=>:mi, :spherical => true).to_i.should be_within(1).of(2469)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Account
|
2
|
+
include Mongoid::Document
|
3
|
+
field :number, :type => String
|
4
|
+
field :balance, :type => String
|
5
|
+
field :nickname, :type => String
|
6
|
+
field :name, :type => String
|
7
|
+
key :name
|
8
|
+
|
9
|
+
embeds_many :memberships
|
10
|
+
referenced_in :creator, :class_name => "User", :foreign_key => :creator_id
|
11
|
+
referenced_in :person
|
12
|
+
references_many :alerts
|
13
|
+
references_and_referenced_in_many :agents
|
14
|
+
|
15
|
+
attr_accessible :nickname, :name
|
16
|
+
|
17
|
+
validates_presence_of :name
|
18
|
+
validates_length_of :name, :maximum => 10, :on => :create
|
19
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
class Address
|
2
|
+
include Mongoid::Document
|
3
|
+
|
4
|
+
attr_accessor :mode
|
5
|
+
|
6
|
+
field :address_type
|
7
|
+
field :number, :type => Integer
|
8
|
+
field :street
|
9
|
+
field :city
|
10
|
+
field :state
|
11
|
+
field :post_code
|
12
|
+
field :parent_title
|
13
|
+
field :services, :type => Array
|
14
|
+
field :latlng, :type => Array
|
15
|
+
key :street
|
16
|
+
embeds_many :locations
|
17
|
+
|
18
|
+
embedded_in :addressable, :polymorphic => true do
|
19
|
+
def extension
|
20
|
+
"Testing"
|
21
|
+
end
|
22
|
+
def doctor?
|
23
|
+
title == "Dr"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
accepts_nested_attributes_for :locations
|
28
|
+
|
29
|
+
referenced_in :account
|
30
|
+
|
31
|
+
scope :without_postcode, where(:postcode => nil)
|
32
|
+
named_scope :rodeo, where(:street => "Rodeo Dr") do
|
33
|
+
def mansion?
|
34
|
+
all? { |address| address.street == "Rodeo Dr" }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
validates_presence_of :street, :on => :update
|
39
|
+
validates_format_of :street, :with => /\D/, :allow_nil => true
|
40
|
+
|
41
|
+
def set_parent=(set = false)
|
42
|
+
self.parent_title = addressable.title if set
|
43
|
+
end
|
44
|
+
|
45
|
+
def <=>(other)
|
46
|
+
street <=> other.street
|
47
|
+
end
|
48
|
+
|
49
|
+
class << self
|
50
|
+
def california
|
51
|
+
where(:state => "CA")
|
52
|
+
end
|
53
|
+
|
54
|
+
def homes
|
55
|
+
where(:address_type => "Home")
|
56
|
+
end
|
57
|
+
|
58
|
+
def streets
|
59
|
+
all.map(&:street)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Animal
|
2
|
+
include Mongoid::Document
|
3
|
+
field :name
|
4
|
+
field :tags, :type => Array
|
5
|
+
key :name
|
6
|
+
|
7
|
+
embedded_in :person
|
8
|
+
embedded_in :circus
|
9
|
+
|
10
|
+
validates_format_of :name, :without => /\$\$\$/
|
11
|
+
|
12
|
+
accepts_nested_attributes_for :person
|
13
|
+
|
14
|
+
def tag_list
|
15
|
+
tags.join(", ")
|
16
|
+
end
|
17
|
+
|
18
|
+
def tag_list=(_tag_list)
|
19
|
+
self.tags = _tag_list.split(",").map(&:strip)
|
20
|
+
end
|
21
|
+
end
|
data/spec/models/bar.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
class Birthday
|
2
|
+
include Mongoid::Document
|
3
|
+
field :title
|
4
|
+
field :date, :type => Date
|
5
|
+
embedded_in :owner, :inverse_of => :birthdays
|
6
|
+
|
7
|
+
def self.each_day(start_date, end_date)
|
8
|
+
groups = only(:date).asc(:date).where(:date.gte => start_date, :date.lte => end_date).group
|
9
|
+
groups.each do |date, group|
|
10
|
+
yield(date, group)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/spec/models/book.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
class Artist
|
2
|
+
include Mongoid::Document
|
3
|
+
field :name
|
4
|
+
embeds_many :songs
|
5
|
+
embeds_many :labels
|
6
|
+
|
7
|
+
before_create :before_create_stub
|
8
|
+
after_create :create_songs
|
9
|
+
before_save :before_save_stub
|
10
|
+
before_destroy :before_destroy_stub
|
11
|
+
|
12
|
+
protected
|
13
|
+
def before_create_stub
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
def before_save_stub
|
18
|
+
true
|
19
|
+
end
|
20
|
+
|
21
|
+
def before_destroy_stub
|
22
|
+
true
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_songs
|
26
|
+
2.times { |n| songs.create!(:title => "#{n}") }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Song
|
31
|
+
include Mongoid::Document
|
32
|
+
field :title
|
33
|
+
embedded_in :artist
|
34
|
+
end
|
35
|
+
|
36
|
+
class Label
|
37
|
+
include Mongoid::Document
|
38
|
+
field :name
|
39
|
+
embedded_in :artist
|
40
|
+
before_validation :cleanup
|
41
|
+
|
42
|
+
private
|
43
|
+
def cleanup
|
44
|
+
self.name = self.name.downcase.capitalize
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class ValidationCallback
|
49
|
+
include Mongoid::Document
|
50
|
+
field :history, :type => Array, :default => []
|
51
|
+
validate do
|
52
|
+
self.history << :validate
|
53
|
+
end
|
54
|
+
|
55
|
+
before_validation { self.history << :before_validation }
|
56
|
+
after_validation { self.history << :after_validation }
|
57
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Comment
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::Versioning
|
4
|
+
include Mongoid::Timestamps
|
5
|
+
field :title, :type => String
|
6
|
+
field :text, :type => String
|
7
|
+
key :text, :type => String
|
8
|
+
|
9
|
+
referenced_in :movie
|
10
|
+
referenced_in :rating
|
11
|
+
validates :title, :presence => true
|
12
|
+
validates :movie, :rating, :associated => true
|
13
|
+
end
|
data/spec/models/drug.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
class Event
|
2
|
+
include Mongoid::Document
|
3
|
+
|
4
|
+
field :title
|
5
|
+
field :date, :type => Date
|
6
|
+
references_and_referenced_in_many \
|
7
|
+
:administrators,
|
8
|
+
:class_name => 'Person',
|
9
|
+
:inverse_of => :administrated_events,
|
10
|
+
:dependent => :nullify
|
11
|
+
referenced_in :owner
|
12
|
+
|
13
|
+
def self.each_day(start_date, end_date)
|
14
|
+
groups = only(:date).asc(:date).where(:date.gte => start_date, :date.lte => end_date).group
|
15
|
+
groups.each do |hash|
|
16
|
+
yield(hash["date"], hash["group"])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|