mongomodel 0.2.15 → 0.2.16

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
- gem "bson_ext", '= 1.1.2'
4
+ gem "bson_ext", '~> 1.1.5'
@@ -12,7 +12,7 @@ module MongoModel
12
12
 
13
13
  # Returns the attributes as they were before any changes were made to the document.
14
14
  def original_attributes
15
- {}.merge(attributes).merge(changed_attributes)
15
+ {}.with_indifferent_access.merge(attributes).merge(changed_attributes)
16
16
  end
17
17
 
18
18
  protected
@@ -1,3 +1,4 @@
1
+ require 'active_support/core_ext/class/attribute_accessors'
1
2
  require 'active_support/core_ext/hash/reverse_merge'
2
3
  require 'active_support/core_ext/hash/deep_merge'
3
4
  require 'active_support/core_ext/string/inflections'
@@ -15,5 +16,8 @@ module MongoModel
15
16
  include DocumentExtensions::Callbacks
16
17
 
17
18
  self.abstract_class = true
19
+
20
+ cattr_accessor :per_page
21
+ self.per_page = 20
18
22
  end
19
23
  end
@@ -13,7 +13,7 @@ module MongoModel
13
13
  module ClassMethods
14
14
  delegate :find, :first, :last, :all, :exists?, :count, :to => :scoped
15
15
  delegate :update, :update_all, :delete, :delete_all, :destroy, :destroy_all, :to => :scoped
16
- delegate :select, :order, :where, :limit, :offset, :from, :to => :scoped
16
+ delegate :select, :order, :where, :limit, :offset, :from, :paginate, :to => :scoped
17
17
 
18
18
  def unscoped
19
19
  @unscoped ||= MongoModel::Scope.new(self)
@@ -9,10 +9,11 @@ module MongoModel
9
9
  autoload :QueryMethods, 'mongomodel/support/scope/query_methods'
10
10
  autoload :FinderMethods, 'mongomodel/support/scope/finder_methods'
11
11
  autoload :DynamicFinders, 'mongomodel/support/scope/dynamic_finders'
12
+ autoload :Pagination, 'mongomodel/support/scope/pagination'
12
13
 
13
- include DynamicFinders, FinderMethods, QueryMethods, SpawnMethods
14
+ include Pagination, DynamicFinders, FinderMethods, QueryMethods, SpawnMethods
14
15
 
15
- delegate :inspect, :to => :to_a
16
+ delegate :inspect, :as_json, :to => :to_a
16
17
 
17
18
  attr_reader :klass
18
19
 
@@ -0,0 +1,17 @@
1
+ require 'will_paginate/collection'
2
+
3
+ module MongoModel
4
+ class Scope
5
+ module Pagination
6
+ def paginate(options={})
7
+ page = options[:page] || 1
8
+ per_page = options[:per_page] || klass.per_page
9
+
10
+ WillPaginate::Collection.create(page, per_page) do |pager|
11
+ pager.replace offset(pager.offset).limit(pager.per_page)
12
+ pager.total_entries ||= count
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module MongoModel
2
- VERSION = "0.2.15"
2
+ VERSION = "0.2.16"
3
3
  end
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.add_dependency "activesupport", "~> 3.0.3"
18
18
  s.add_dependency "activemodel", "~> 3.0.3"
19
19
  s.add_dependency "mongo", "~> 1.1.2"
20
+ s.add_dependency "will_paginate", "~> 2.3.15"
20
21
 
21
22
  s.add_development_dependency "bundler", ">= 1.0.0"
22
23
  s.add_development_dependency "rspec", "= 1.3.0"
@@ -142,7 +142,7 @@ module MongoModel
142
142
 
143
143
  context "without changes" do
144
144
  it "should return the attributes hash" do
145
- subject.original_attributes.should == subject.attributes
145
+ subject.original_attributes.symbolize_keys.should == subject.attributes
146
146
  end
147
147
  end
148
148
  end
@@ -23,8 +23,8 @@ describe MongoModel do
23
23
  database = MongoModel.database
24
24
  connection = database.connection
25
25
 
26
- connection.host.should == '127.0.0.1'
27
- connection.port.should == 27017
26
+ connection.primary_pool.host.should == '127.0.0.1'
27
+ connection.primary_pool.port.should == 27017
28
28
  database.name.should == 'mydb'
29
29
  end
30
30
  end
@@ -57,6 +57,12 @@ module MongoModel
57
57
  end
58
58
  end
59
59
 
60
+ describe "#as_json" do
61
+ it "should delegate to #to_a" do
62
+ subject.as_json.should == subject.to_a.as_json
63
+ end
64
+ end
65
+
60
66
  describe "#count" do
61
67
  it "should count documents matching conditions and return the result" do
62
68
  model.should_count(finder_options, 4) do
@@ -728,6 +734,47 @@ module MongoModel
728
734
  end
729
735
  end
730
736
  end
737
+
738
+ describe "#paginate" do
739
+ it "should load the first page of results by default" do
740
+ model.should_find(finder_options.merge(:offset => 0, :limit => 20), posts) {
741
+ subject.paginate
742
+ }
743
+ end
744
+
745
+ it "should load a specified page of results" do
746
+ model.should_find(finder_options.merge(:offset => 40, :limit => 20), posts) {
747
+ subject.paginate(:page => 3)
748
+ }
749
+ end
750
+
751
+ it "should allow the per_page option to be set" do
752
+ model.should_find(finder_options.merge(:offset => 7, :limit => 7), posts) {
753
+ subject.paginate(:per_page => 7, :page => 2)
754
+ }
755
+ end
756
+
757
+ it "should auto-detect total entries where possible" do
758
+ paginator = nil
759
+
760
+ model.should_find(finder_options.merge(:offset => 0, :limit => 20), posts) {
761
+ paginator = subject.paginate
762
+ }
763
+
764
+ paginator.total_entries.should == 5
765
+ end
766
+
767
+ it "should load total entries using count when auto-detection not possible" do
768
+ paginator = nil
769
+
770
+ subject.stub!(:count).and_return(57)
771
+ model.should_find(finder_options.merge(:offset => 0, :limit => 5), posts) {
772
+ paginator = subject.paginate(:per_page => 5)
773
+ }
774
+
775
+ paginator.total_entries.should == 57
776
+ end
777
+ end
731
778
  end
732
779
 
733
780
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongomodel
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
5
- prerelease: false
4
+ hash: 55
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 15
10
- version: 0.2.15
9
+ - 16
10
+ version: 0.2.16
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sam Pohlenz
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-23 00:00:00 +10:30
18
+ date: 2011-01-04 00:00:00 +10:30
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -67,9 +67,25 @@ dependencies:
67
67
  type: :runtime
68
68
  version_requirements: *id003
69
69
  - !ruby/object:Gem::Dependency
70
- name: bundler
70
+ name: will_paginate
71
71
  prerelease: false
72
72
  requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ hash: 29
78
+ segments:
79
+ - 2
80
+ - 3
81
+ - 15
82
+ version: 2.3.15
83
+ type: :runtime
84
+ version_requirements: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ name: bundler
87
+ prerelease: false
88
+ requirement: &id005 !ruby/object:Gem::Requirement
73
89
  none: false
74
90
  requirements:
75
91
  - - ">="
@@ -81,11 +97,11 @@ dependencies:
81
97
  - 0
82
98
  version: 1.0.0
83
99
  type: :development
84
- version_requirements: *id004
100
+ version_requirements: *id005
85
101
  - !ruby/object:Gem::Dependency
86
102
  name: rspec
87
103
  prerelease: false
88
- requirement: &id005 !ruby/object:Gem::Requirement
104
+ requirement: &id006 !ruby/object:Gem::Requirement
89
105
  none: false
90
106
  requirements:
91
107
  - - "="
@@ -97,7 +113,7 @@ dependencies:
97
113
  - 0
98
114
  version: 1.3.0
99
115
  type: :development
100
- version_requirements: *id005
116
+ version_requirements: *id006
101
117
  description: MongoModel is a MongoDB ORM for Ruby/Rails similar to ActiveRecord and DataMapper.
102
118
  email:
103
119
  - sam@sampohlenz.com
@@ -174,6 +190,7 @@ files:
174
190
  - lib/mongomodel/support/scope.rb
175
191
  - lib/mongomodel/support/scope/dynamic_finders.rb
176
192
  - lib/mongomodel/support/scope/finder_methods.rb
193
+ - lib/mongomodel/support/scope/pagination.rb
177
194
  - lib/mongomodel/support/scope/query_methods.rb
178
195
  - lib/mongomodel/support/scope/spawn_methods.rb
179
196
  - lib/mongomodel/support/types.rb
@@ -281,7 +298,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
281
298
  requirements: []
282
299
 
283
300
  rubyforge_project: mongomodel
284
- rubygems_version: 1.3.7
301
+ rubygems_version: 1.4.1
285
302
  signing_key:
286
303
  specification_version: 3
287
304
  summary: MongoDB ORM for Ruby/Rails