paged_scopes 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -193,6 +193,60 @@ page_articles GET /pagina/:page_id/articles(.:format) {:controller=>"articles",
193
193
 
194
194
  (This is likely only to be useful in rare situations. One example would be paginating more than one collection in a single view.)
195
195
 
196
+ A more complex example:
197
+
198
+ <pre>
199
+ map.resources :articles, :collection => { :published => :get }, :paged => true
200
+ </pre>
201
+
202
+ Which would produce these paged routes:
203
+
204
+ <pre>
205
+ published_page_articles GET /pages/:page_id/articles/published(.:format) {:controller=>"articles", :action=>"published"}
206
+ page_articles GET /pages/:page_id/articles(.:format) {:controller=>"articles", :action=>"index"}
207
+ </pre>
208
+
209
+ By default paged routes are generated for the <code>:index</code> action and any <code>:collection</code> actions that specify the <code>:get</code> method. To override the default behaviour, simply be explicit about which paged routes are required:
210
+
211
+ <pre>
212
+ map.resources :articles, :collection => { :published => :get }, :paged => { :published => true }
213
+ </pre>
214
+
215
+ Which would produce only this paged route:
216
+
217
+ <pre>
218
+ published_page_articles GET /pages/:page_id/articles/published(.:format) {:controller=>"articles", :action=>"published"}
219
+ </pre>
220
+
221
+ Another complex example:
222
+
223
+ <pre>
224
+ map.resources :articles, :collection => { :published => :get }, :paged => { :index => true, :published => { :as => :pagina } }
225
+ </pre>
226
+
227
+ Which would produce these paged routes:
228
+
229
+ <pre>
230
+ published_page_articles GET /pagina/:page_id/articles/published(.:format) {:controller=>"articles", :action=>"published"}
231
+ page_articles GET /pages/:page_id/articles(.:format) {:controller=>"articles", :action=>"index"}
232
+ </pre>
233
+
234
+ In fact, the <code>:as</code> and <code>:name</code> options specified in the root of the <code>:paged</code> will be used for all paged routes where an alternative is not specified.
235
+
236
+ <pre>
237
+ map.resources :users, :collection => { :active => :get, :blocked => :get, :online => :get }, :paged => { :as => "pagina", :index => true, :active => { :name => "group" }, :blocked => { :as => "blacklist" } }
238
+ </pre>
239
+
240
+ Which would produce these three paged routes:
241
+
242
+ <pre>
243
+ blocked_page_users GET /blacklist/:page_id/users/blocked(.:format) {:controller=>"users", :action=>"blocked"}
244
+ active_group_users GET /pagina/:group_id/users/active(.:format) {:controller=>"users", :action=>"active"}
245
+ page_users GET /pagina/:page_id/users(.:format) {:controller=>"users", :action=>"index"}
246
+ </pre>
247
+
248
+ Notice that there is no paged route for the <code>:online</code> collection action.
249
+
196
250
  h2. Controller Methods
197
251
 
198
252
  OK, so we have our pages represented in our article index route. Let's turn to the articles controller next.
@@ -456,14 +510,14 @@ h2. Get It!
456
510
  You can install the PagedScopes gem as follows:
457
511
 
458
512
  <pre>
459
- gem sources -a http://gems.github.com # just once
460
- sudo gem install mholling-paged_scopes
513
+ gem sources -a http://gemcutter.org # just once
514
+ sudo gem install paged_scopes
461
515
  </pre>
462
516
 
463
517
  And in your <code>config/environment.rb</code>, if you're on Rails:
464
518
 
465
519
  <pre>
466
- config.gem "mholling-paged_scopes", :lib => "paged_scopes", :source => "http://gems.github.com"
520
+ config.gem "paged_scopes", :source => "http://gemcutter.org"
467
521
  </pre>
468
522
 
469
523
  Peruse the code at "GitHub":http://github.com/mholling/paged_scopes.
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 0
3
2
  :major: 0
4
3
  :minor: 1
4
+ :patch: 1
@@ -0,0 +1,5 @@
1
+ PagedScopes Revision History
2
+
3
+ Version 0.1.1
4
+ * Added this revision history.
5
+ * Added ability to generate paged routes for other collection actions (Matthew Gibbons, Matthew Hollingworth).
@@ -2,16 +2,36 @@ module PagedScopes
2
2
  module Resources
3
3
  def resources_with_paged(*entities, &block)
4
4
  options = entities.extract_options!
5
- if page_options = options.delete(:paged)
6
- resources_without_paged(*(entities.dup << options), &block)
7
- page_options = {} unless page_options.is_a? Hash
8
- page_name = page_options.delete(:name)
9
- page_options.slice!(:as, :name)
10
- page_options.merge!(:only => :none)
11
- preserved_options = ActionController::Resources::INHERITABLE_OPTIONS + [ :name_prefix, :path_prefix ]
12
- with_options(options.slice(*preserved_options)) do |map|
13
- map.resources_without_paged(page_name || :pages, page_options) do |page|
14
- page.resources(*(entities.dup << { :only => :index }))
5
+
6
+ if paged_options = options.delete(:paged)
7
+ resources_without_paged(*(entities.dup << options), &block)
8
+
9
+ paged_options = {} unless paged_options.is_a? Hash
10
+ paged_as = paged_options.delete(:as)
11
+ paged_name = paged_options.delete(:name)
12
+
13
+ if paged_options.empty?
14
+ unless (options[:only] && ![ options[:only] ].flatten.include?(:index)) || (options[:except] && [ options[:except] ].flatten.include?(:index))
15
+ paged_options[:index] = true
16
+ end
17
+ options[:collection].each { |collection, methods| paged_options[collection] = true if [ methods ].flatten.include? :get } if options[:collection]
18
+ end
19
+
20
+ paged_options.each_pair do |action, page_options|
21
+ page_options = {} unless page_options.is_a? Hash
22
+ page_options.reverse_merge! :name => paged_name unless paged_name.blank?
23
+ page_options.reverse_merge! :as => paged_as unless paged_as.blank?
24
+ page_options.merge! :only => :none
25
+
26
+ preserved_options = ActionController::Resources::INHERITABLE_OPTIONS + [ :name_prefix, :path_prefix ]
27
+
28
+ with_options(options.slice(*preserved_options)) do |map|
29
+ map.resources_without_paged(page_options.delete(:name) || :pages, page_options) do |page|
30
+ entities_options = action == :index ?
31
+ { :only => :index, :as => options[:as] } :
32
+ { :only => :none, :as => options[:as], :collection => { action => :get } }
33
+ page.resources(*(entities.dup << entities_options))
34
+ end
15
35
  end
16
36
  end
17
37
  else
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{paged_scopes}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Matthew Hollingworth"]
12
- s.date = %q{2009-10-10}
12
+ s.date = %q{2009-10-23}
13
13
  s.email = %q{mdholling@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  "README.textile",
23
23
  "Rakefile",
24
24
  "VERSION.yml",
25
+ "history.txt",
25
26
  "lib/paged_scopes.rb",
26
27
  "lib/paged_scopes/collection.rb",
27
28
  "lib/paged_scopes/context.rb",
@@ -17,6 +17,16 @@ describe "Resources" do
17
17
  drawing_routes { |map| map.resources :articles, :paged => true }.should change { number_of_routes }.by(7+1)
18
18
  end
19
19
 
20
+ it "should not add a paged index route if the index action is excluded by an :only option" do
21
+ drawing_routes { |map| map.resources :articles, :paged => true, :only => :show }.should change { number_of_routes }.by(1+0)
22
+ drawing_routes { |map| map.resources :memberships, :paged => true, :only => [ :show, :edit ] }.should change { number_of_routes }.by(2+0)
23
+ end
24
+
25
+ it "should not add a paged index route if the index action is excluded by an :except option" do
26
+ drawing_routes { |map| map.resources :articles, :paged => true, :except => :index }.should change { number_of_routes }.by(6+0)
27
+ drawing_routes { |map| map.resources :memberships, :paged => true, :except => [ :index, :new ] }.should change { number_of_routes }.by(5+0)
28
+ end
29
+
20
30
  context "with a :paged options" do
21
31
  it "should map a paged index route for GET only" do
22
32
  draw_routes { |map| map.resources :articles, :paged => true }
@@ -51,7 +61,7 @@ describe "Resources" do
51
61
  recognise_path(:get, "/groups/1/articles").should == { :controller => "articles", :action => "index", :group_id => "1" }
52
62
  end
53
63
 
54
- it "should accept a :path_prefix hash as the :paged option" do
64
+ it "should accept a :name_prefix option in the paged route" do
55
65
  draw_routes { |map| map.resources :articles, :paged => true, :name_prefix => "baz_" }
56
66
  named_routes.names.should include(:baz_page_articles)
57
67
  end
@@ -69,4 +79,83 @@ describe "Resources" do
69
79
  end
70
80
  end
71
81
  end
82
+
83
+ context "with extra collection routes" do
84
+ # TODO: test for :index in :except...?
85
+ context "and an overall :paged option" do
86
+ it "should map a paged route if the collection action is GET" do
87
+ drawing_routes { |map| map.resources :memberships, :collection => { :paid => :get }, :paged => true }.should change { number_of_routes }.by(8+2)
88
+ end
89
+
90
+ it "should map a paged route if the collection action includes GET" do
91
+ drawing_routes { |map| map.resources :memberships, :collection => { :paid => [ :get, :put ] }, :paged => true }.should change { number_of_routes }.by(9+2)
92
+ end
93
+
94
+ it "should not map a paged route if the collection action is not GET" do
95
+ drawing_routes { |map| map.resources :memberships, :collection => { :paid => :put }, :paged => true }.should change { number_of_routes }.by(8+1)
96
+ end
97
+
98
+ it "should not map a paged route if the collection action does not include GET" do
99
+ drawing_routes { |map| map.resources :memberships, :collection => { :paid => [ :put, :delete ] }, :paged => true }.should change { number_of_routes }.by(9+1)
100
+ end
101
+
102
+ it "should map a paged collection route for a GET method only" do
103
+ draw_routes { |map| map.resources :memberships, :collection => { :paid => :get }, :paged => true }
104
+ recognise_path( :get, "/pages/1/memberships/paid").should == { :controller => "memberships", :action => "paid", :page_id => "1" }
105
+ recognise_path( :put, "/pages/1/memberships/paid").should be_nil
106
+ recognise_path( :post, "/pages/1/memberships/paid").should be_nil
107
+ recognise_path(:delete, "/pages/1/memberships/paid").should be_nil
108
+ end
109
+
110
+ it "should add a named route for the paged collection route" do
111
+ draw_routes { |map| map.resources :memberships, :collection => { :paid => :get }, :paged => true }
112
+ named_routes.names.should include(:paid_page_memberships)
113
+ # TODO: this should maybe be page_paid_memberships?
114
+ end
115
+
116
+ it "should observe the :path_prefix option in the paged route" do
117
+ draw_routes { |map| map.resources :memberships, :collection => { :paid => :get }, :paged => true, :path_prefix => "foo" }
118
+ recognise_path(:get, "/foo/pages/1/memberships/paid").should == { :controller => "memberships", :action => "paid", :page_id => "1" }
119
+ end
120
+
121
+ it "should observe a :namespace option in the paged route" do
122
+ draw_routes { |map| map.resources :memberships, :collection => { :paid => :get }, :paged => true, :namespace => "bar/" }
123
+ recognise_path(:get, "/pages/1/memberships/paid").should == { :controller => "bar/memberships", :action => "paid", :page_id => "1" }
124
+ end
125
+
126
+ it "should accept an :as option in the :paged option" do
127
+ draw_routes { |map| map.resources :memberships, :collection => { :paid => :get }, :paged => { :as => "page" } }
128
+ recognise_path(:get, "/page/1/memberships/paid").should == { :controller => "memberships", :action => "paid", :page_id => "1" }
129
+ end
130
+
131
+ it "should accept a :name option in the :paged option" do
132
+ draw_routes { |map| map.resources :memberships, :collection => { :paid => :get }, :paged => { :name => :groups } }
133
+ recognise_path(:get, "/groups/1/memberships/paid").should == { :controller => "memberships", :action => "paid", :group_id => "1" }
134
+ end
135
+
136
+ it "should accept a :name_prefix option in the paged route" do
137
+ draw_routes { |map| map.resources :memberships, :collection => { :paid => :get }, :paged => true, :name_prefix => "baz_" }
138
+ named_routes.names.should include(:paid_baz_page_memberships)
139
+ # TODO: this should maybe be baz_page_paid_memberships?
140
+ end
141
+ end
142
+
143
+ context "and a per-collection :paged options hash" do
144
+ it "should add a paged route for each :get collection with a corresponding entry in the :paged hash" do
145
+ drawing_routes { |map| map.resources :memberships, :collection => { :paid => :get, :unpaid => :get }, :paged => { :index => true, :paid => true } }.should change { number_of_routes }.by(9+2)
146
+ end
147
+
148
+ it "should accept an :as option in the :paged options hash" do
149
+ draw_routes { |map| map.resources :memberships, :collection => { :paid => :get }, :paged => { :index => true, :paid => { :as => "page" } } }
150
+ recognise_path(:get, "/pages/1/memberships").should == { :controller => "memberships", :action => "index", :page_id => "1" }
151
+ recognise_path(:get, "/page/1/memberships/paid").should == { :controller => "memberships", :action => "paid", :page_id => "1" }
152
+ end
153
+
154
+ it "should accept a :name option in the :paged options hash" do
155
+ draw_routes { |map| map.resources :memberships, :collection => { :paid => :get }, :paged => { :index => true, :paid => { :name => :groups } } }
156
+ recognise_path(:get, "/pages/1/memberships").should == { :controller => "memberships", :action => "index", :page_id => "1" }
157
+ recognise_path(:get, "/groups/1/memberships/paid").should == { :controller => "memberships", :action => "paid", :group_id => "1" }
158
+ end
159
+ end
160
+ end
72
161
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paged_scopes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Hollingworth
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-10 00:00:00 +11:00
12
+ date: 2009-10-23 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -38,6 +38,7 @@ files:
38
38
  - README.textile
39
39
  - Rakefile
40
40
  - VERSION.yml
41
+ - history.txt
41
42
  - lib/paged_scopes.rb
42
43
  - lib/paged_scopes/collection.rb
43
44
  - lib/paged_scopes/context.rb