mongoid_spacial 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -138,9 +138,8 @@ River.geo_near([-73.99756,40.73083], :page => 1)
138
138
  #
139
139
  # both return a GeoNearResults, which is really just a modified Array
140
140
  # #per really just #page but just moves the options around
141
- rivers = River.geo_near([-73.99756,40.73083]).sort_by!{|r| r.modified_distance}
141
+ rivers = River.geo_near([-73.99756,40.73083]).sort_by!{|r| r.geo[:distance] * r.multiplier }
142
142
  rivers = rivers.per(25).page(1)
143
- rivers = rivers.page(5, :original => 1) # by adding the original option you can re-paginate to any area.
144
143
  rivers.reset! # resets the object to it is original state right after query.
145
144
  ```
146
145
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.7
1
+ 0.2.8
@@ -1,18 +1,17 @@
1
1
  module Mongoid
2
2
  module Spacial
3
3
  class GeoNearResults < Array
4
- attr_reader :stats, :document, :_original_array
4
+ attr_reader :stats, :document, :_original_array, :_original_opts
5
5
  attr_accessor :opts
6
6
 
7
7
  def initialize(document,results,opts = {})
8
8
  raise "class must include Mongoid::Spacial::Document" unless document.respond_to?(:spacial_fields_indexed)
9
9
  @document = document
10
10
  @opts = opts
11
+ @_original_opts = opts.clone
11
12
  @stats = results['stats'] || {}
12
13
  @opts[:skip] ||= 0
13
14
  @opts[:total_entries] = opts[:query]["num"] || @stats['nscanned']
14
- @limit_value = opts[:per_page]
15
- @current_page = opts[:page]
16
15
 
17
16
  @_original_array = results['results'].collect do |result|
18
17
  res = Mongoid::Factory.from_db(@document, result.delete('obj'))
@@ -40,10 +39,10 @@ module Mongoid
40
39
  end
41
40
  res
42
41
  end
43
-
44
42
  if @opts[:page]
45
43
  start = (@opts[:page]-1)*@opts[:per_page] # assuming current_page is 1 based.
46
- super(@_original_array[@opts[:skip]+start, @opts[:per_page]] || [])
44
+ @_paginated_array = @_original_array.clone
45
+ super(@_paginated_array[@opts[:skip]+start, @opts[:per_page]] || [])
47
46
  else
48
47
  super(@_original_array[@opts[:skip]..-1] || [])
49
48
  end
@@ -51,36 +50,20 @@ module Mongoid
51
50
 
52
51
  def page(page, options = {})
53
52
  new_collection = self.clone
53
+ original = options.delete(:original)
54
+ new_collection.opts.merge!(options)
55
+ new_collection.opts[:paginator] ||= Mongoid::Spacial.paginator
54
56
 
55
- options = self.opts.merge(options)
56
-
57
- options[:page] = (page) ? page.to_i : 1
58
-
59
- options[:paginator] ||= Mongoid::Spacial.paginator()
60
-
61
- options[:per_page] ||= case options[:paginator]
62
- when :will_paginate
63
- @document.per_page
64
- when :kaminari
65
- Kaminari.config.default_per_page
66
- else
67
- Mongoid::Spacial.default_per_page
68
- end
69
-
70
- options[:per_page] = options[:per_page].to_i
57
+ start = (new_collection.current_page-1)*new_collection.limit_value # assuming current_page is 1 based.
71
58
 
72
- start = (options[:page]-1)*options[:per_page] # assuming current_page is 1 based.
73
-
74
- if options[:original]
75
- new_collection.replace(@_original_array[@opts[:skip]+start, options[:per_page]] || [])
59
+ if original
60
+ @_paginated_array = @_original_array.clone
61
+ new_collection.replace(@_original_array[new_collection.opts[:skip]+start, new_collection.limit_value] || [])
76
62
  else
77
- new_collection.slice!(@opts[:skip]+start, options[:per_page])
63
+ @_paginated_array ||= self.to_a
64
+ new_collection.replace(@_paginated_array[new_collection.opts[:skip]+start, new_collection.limit_value])
78
65
  end
79
66
 
80
- new_collection.opts[:page] = options[:page]
81
- new_collection.opts[:paginator] = options[:paginator]
82
- new_collection.opts[:per_page] = options[:per_page]
83
-
84
67
  new_collection
85
68
  end
86
69
 
@@ -90,10 +73,9 @@ module Mongoid
90
73
 
91
74
  def reset!
92
75
  self.replace(@_original_array)
93
- new_collection.opts[:page] = nil
94
- new_collection.opts[:paginator] = nil
95
- new_collection.opts[:per_page] = nil
96
- self
76
+ @opts = @_original_opts
77
+ @_paginated_array = nil
78
+ true
97
79
  end
98
80
 
99
81
  def reset
@@ -107,11 +89,23 @@ module Mongoid
107
89
  end
108
90
 
109
91
  def current_page
110
- @opts[:page]
92
+ page = (@opts[:page]) ? @opts[:page].to_i.abs : 1
93
+ (@opts[:page] < 1) ? 1 : page
111
94
  end
112
95
 
113
96
  def limit_value
114
- @opts[:per_page]
97
+ if @opts[:per_page]
98
+ @opts[:per_page] = @opts[:per_page].to_i.abs
99
+ else
100
+ @opts[:per_page] = case new_collection.opts[:paginator]
101
+ when :will_paginate
102
+ @document.per_page
103
+ when :kaminari
104
+ Kaminari.config.default_per_page
105
+ else
106
+ Mongoid::Spacial.default_per_page
107
+ end
108
+ end
115
109
  end
116
110
  alias_method :per_page, :limit_value
117
111
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid_spacial}
8
- s.version = "0.2.7"
8
+ s.version = "0.2.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Ryan Ong}]
12
- s.date = %q{2011-07-11}
12
+ s.date = %q{2011-07-18}
13
13
  s.description = %q{A Mongoid Extention that simplifies and adds support for MongoDB Geo Spacial Calculations.}
14
14
  s.email = %q{ryanong@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -5,29 +5,52 @@ describe Mongoid::Spacial::GeoNearResults do
5
5
  Bar.delete_all
6
6
  Bar.create_indexes
7
7
 
8
- 50.times do
9
- Bar.create({:location => [rand(360)-180,rand(360)-180]})
8
+ 50.times do |i|
9
+ Bar.create(:name => i.to_s, :location => [rand(358)-179,rand(358)-179])
10
10
  end
11
+ end
12
+
13
+ before(:each) do
11
14
  while Bar.count < 50
12
15
  end
13
16
  end
17
+
14
18
  context ":paginator :array" do
15
- [nil,1,2].each do |page|
19
+ let!(:bars) { Bar.geo_near([1,1]) }
20
+ let!(:sorted_bars) { Bar.geo_near([1,1]).sort_by {|b| b.name.to_i}}
21
+ [nil,1,2].each do |page|
16
22
  it "page=#{page} should have 25" do
17
- Bar.geo_near([1,1]).page(page).size.should == 25
23
+ bars.page(page).size.should == 25
24
+ end
25
+ end
26
+
27
+ [1,2].each do |page|
28
+ it "modified result should keep order after pagination" do
29
+ sorted_bars.page(page).should == sorted_bars.slice((page-1)*25,25)
30
+ end
31
+ end
32
+
33
+ { nil => 25, 20 => 20 , 30 => 20, 50 => 0}.each do |per, total|
34
+ it "page=2 per=#{per} should have #{total}" do
35
+ bars.per(per).page(2).size.should == total
36
+ bars.page(2).per(per).size.should == total
18
37
  end
19
38
  end
20
39
 
21
40
  it "page=3 should have 0" do
22
- Bar.geo_near([1,1]).page(3).size.should == 0
41
+ bars.page(3).size.should == 0
23
42
  end
24
43
 
25
44
  it "per=5" do
26
- Bar.geo_near([1,1]).per(5).size.should == 5
45
+ bars.per(5).size.should == 5
46
+ end
47
+
48
+ it "page=10 per=5" do
49
+ bars.per(5).page(10).should == bars[45..50]
27
50
  end
28
51
  end
29
52
 
30
- context ":paginator :kaminari" do
53
+ context ":paginator :kaminari" do
31
54
  let!(:near) {Bar.geo_near([1,1]).page(1)}
32
55
  it "should have current_page" do
33
56
  near.current_page.should == 1
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mongoid_spacial
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.7
5
+ version: 0.2.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ryan Ong
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-11 00:00:00 Z
13
+ date: 2011-07-18 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mongoid
@@ -264,7 +264,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
264
264
  requirements:
265
265
  - - ">="
266
266
  - !ruby/object:Gem::Version
267
- hash: 2152929037743712871
267
+ hash: -3478690375054845577
268
268
  segments:
269
269
  - 0
270
270
  version: "0"