will_paginate_couchrest 0.2.0 → 0.2.2

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.2
@@ -110,36 +110,34 @@ module CouchRest
110
110
  paginated_view(:all, options)
111
111
  end
112
112
 
113
- protected
114
-
115
- ##
116
- # Return a WillPaginate collection suitable for usage
117
- #
118
- def paginated_view(view_name, options = {})
119
- raise "Missing per_page parameter" if options[:per_page].nil?
113
+ ##
114
+ # Return a WillPaginate collection suitable for usage
115
+ #
116
+ def paginated_view(view_name, options = {})
117
+ raise "Missing per_page parameter" if options[:per_page].nil?
120
118
 
121
- options[:page] ||= 1
119
+ options[:page] ||= 1
122
120
 
123
- ::WillPaginate::Collection.create( options[:page], options[:per_page] ) do |pager|
124
- # perform view count first (should create designs if missing)
125
- if view_name.to_sym == :all
126
- pager.total_entries = count()
127
- else
128
- total = view( view_name, options.update(:reduce => true) )['rows'].pop
129
- pager.total_entries = total ? total['value'] : 0
130
- end
131
- p_options = options.merge(
132
- :design_doc => self.to_s, :view_name => view_name,
133
- :include_docs => true
134
- )
135
- # Only provide the reduce parameter when necessary. This is when the view has
136
- # been set with a reduce method and requires the reduce boolean parameter
137
- # to be either true or false on all requests.
138
- p_options[:reduce] = false unless view_name.to_sym == :all
139
- results = paginate(p_options)
140
- pager.replace( results )
121
+ ::WillPaginate::Collection.create( options[:page], options[:per_page] ) do |pager|
122
+ # perform view count first (should create designs if missing)
123
+ if view_name.to_sym == :all
124
+ pager.total_entries = count({:database => options[:database]})
125
+ else
126
+ total = view( view_name, options.dup.update(:reduce => true) )['rows'].pop
127
+ pager.total_entries = total ? total['value'] : 0
141
128
  end
129
+ p_options = options.merge(
130
+ :design_doc => self.to_s, :view_name => view_name,
131
+ :include_docs => true
132
+ )
133
+ # Only provide the reduce parameter when necessary. This is when the view has
134
+ # been set with a reduce method and requires the reduce boolean parameter
135
+ # to be either true or false on all requests.
136
+ p_options[:reduce] = false unless view_name.to_sym == :all
137
+ results = paginate(p_options)
138
+ pager.replace( results )
142
139
  end
140
+ end
143
141
  end
144
142
 
145
143
  end
@@ -5,12 +5,23 @@ module CouchRest
5
5
 
6
6
  def method_missing(m, *args, &block)
7
7
  if m.to_s =~ /^paginate_(.+)/ && @klass.respond_to?(m)
8
- @klass.send(m, *args)
8
+ view_name = $1 # view name
9
+ opts = args.shift || {}
10
+ paginated_view(view_name, opts)
9
11
  else
10
12
  super
11
13
  end
12
14
  end
13
15
 
16
+ def paginated_view(view_name, opts = {})
17
+ opts = {
18
+ :database => @database
19
+ }.merge(opts)
20
+ result = @klass.paginated_view(view_name, opts)
21
+ result.each{|doc| doc.database = @database if respond_to?(:database) } if result
22
+ result
23
+ end
24
+
14
25
  end
15
26
  end
16
27
  end
@@ -80,14 +80,19 @@ describe CouchRest::Mixins::WillPaginate do
80
80
 
81
81
 
82
82
  describe "using pagination via proxy class" do
83
- before(:all) do
83
+ before(:each) do
84
84
  @proxy = SomeDoc.on(SPEC_COUCH)
85
85
  end
86
86
 
87
- it "should allow paginate call on proxy" do
88
- SomeDoc.should_receive(:paginate_by_name)
87
+ it "should allow paginate call on proxy with database" do
88
+ SomeDoc.should_receive(:paginated_view).with('by_name', {:key => 'foo', :database => SPEC_COUCH})
89
89
  @proxy.paginate_by_name :key => 'foo'
90
90
  end
91
+
92
+ it "should call paginate parent with database" do
93
+ SomeDoc.should_receive(:paginate).with(hash_including(:database => SPEC_COUCH)).and_return([])
94
+ @proxy.paginate_by_name :key => 'foo', :per_page => 10
95
+ end
91
96
  end
92
97
  end
93
98
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 2
9
+ version: 0.2.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - samlown