##// END OF EJS Templates
Don't generate pagination links with params....
Jean-Philippe Lang -
r15245:5d31eb2495c2
parent child
Show More
@@ -1,249 +1,249
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2016 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 module Redmine
21 21 module Pagination
22 22 class Paginator
23 23 attr_reader :item_count, :per_page, :page, :page_param
24 24
25 25 def initialize(*args)
26 26 if args.first.is_a?(ActionController::Base)
27 27 args.shift
28 28 ActiveSupport::Deprecation.warn "Paginator no longer takes a controller instance as the first argument. Remove it from #new arguments."
29 29 end
30 30 item_count, per_page, page, page_param = *args
31 31
32 32 @item_count = item_count
33 33 @per_page = per_page
34 34 page = (page || 1).to_i
35 35 if page < 1
36 36 page = 1
37 37 end
38 38 @page = page
39 39 @page_param = page_param || :page
40 40 end
41 41
42 42 def offset
43 43 (page - 1) * per_page
44 44 end
45 45
46 46 def first_page
47 47 if item_count > 0
48 48 1
49 49 end
50 50 end
51 51
52 52 def previous_page
53 53 if page > 1
54 54 page - 1
55 55 end
56 56 end
57 57
58 58 def next_page
59 59 if last_item < item_count
60 60 page + 1
61 61 end
62 62 end
63 63
64 64 def last_page
65 65 if item_count > 0
66 66 (item_count - 1) / per_page + 1
67 67 end
68 68 end
69 69
70 70 def multiple_pages?
71 71 per_page < item_count
72 72 end
73 73
74 74 def first_item
75 75 item_count == 0 ? 0 : (offset + 1)
76 76 end
77 77
78 78 def last_item
79 79 l = first_item + per_page - 1
80 80 l > item_count ? item_count : l
81 81 end
82 82
83 83 def linked_pages
84 84 pages = []
85 85 if item_count > 0
86 86 pages += [first_page, page, last_page]
87 87 pages += ((page-2)..(page+2)).to_a.select {|p| p > first_page && p < last_page}
88 88 end
89 89 pages = pages.compact.uniq.sort
90 90 if pages.size > 1
91 91 pages
92 92 else
93 93 []
94 94 end
95 95 end
96 96
97 97 def items_per_page
98 98 ActiveSupport::Deprecation.warn "Paginator#items_per_page will be removed. Use #per_page instead."
99 99 per_page
100 100 end
101 101
102 102 def current
103 103 ActiveSupport::Deprecation.warn "Paginator#current will be removed. Use .offset instead of .current.offset."
104 104 self
105 105 end
106 106 end
107 107
108 108 # Paginates the given scope or model. Returns a Paginator instance and
109 109 # the collection of objects for the current page.
110 110 #
111 111 # Options:
112 112 # :parameter name of the page parameter
113 113 #
114 114 # Examples:
115 115 # @user_pages, @users = paginate User.where(:status => 1)
116 116 #
117 117 def paginate(scope, options={})
118 118 options = options.dup
119 119
120 120 paginator = paginator(scope.count, options)
121 121 collection = scope.limit(paginator.per_page).offset(paginator.offset).to_a
122 122
123 123 return paginator, collection
124 124 end
125 125
126 126 def paginator(item_count, options={})
127 127 options.assert_valid_keys :parameter, :per_page
128 128
129 129 page_param = options[:parameter] || :page
130 130 page = (params[page_param] || 1).to_i
131 131 per_page = options[:per_page] || per_page_option
132 132 Paginator.new(item_count, per_page, page, page_param)
133 133 end
134 134
135 135 module Helper
136 136 include Redmine::I18n
137 137
138 138 # Renders the pagination links for the given paginator.
139 139 #
140 140 # Options:
141 141 # :per_page_links if set to false, the "Per page" links are not rendered
142 142 #
143 143 def pagination_links_full(*args)
144 144 pagination_links_each(*args) do |text, parameters, options|
145 145 if block_given?
146 146 yield text, parameters, options
147 147 else
148 link_to text, params.merge(parameters), options
148 link_to text, {:params => request.query_parameters.merge(parameters)}, options
149 149 end
150 150 end
151 151 end
152 152
153 153 # Yields the given block with the text and parameters
154 154 # for each pagination link and returns a string that represents the links
155 155 def pagination_links_each(paginator, count=nil, options={}, &block)
156 156 options.assert_valid_keys :per_page_links
157 157
158 158 per_page_links = options.delete(:per_page_links)
159 159 per_page_links = false if count.nil?
160 160 page_param = paginator.page_param
161 161
162 162 html = '<ul class="pages">'
163 163
164 164 if paginator.multiple_pages?
165 165 # \xc2\xab(utf-8) = &#171;
166 166 text = "\xc2\xab " + l(:label_previous)
167 167 if paginator.previous_page
168 168 html << content_tag('li',
169 169 yield(text, {page_param => paginator.previous_page},
170 170 :accesskey => accesskey(:previous)),
171 171 :class => 'previous page')
172 172 else
173 173 html << content_tag('li', content_tag('span', text), :class => 'previous')
174 174 end
175 175 end
176 176
177 177 previous = nil
178 178 paginator.linked_pages.each do |page|
179 179 if previous && previous != page - 1
180 180 html << content_tag('li', content_tag('span', '&hellip;'.html_safe), :class => 'spacer')
181 181 end
182 182 if page == paginator.page
183 183 html << content_tag('li', content_tag('span', page.to_s), :class => 'current')
184 184 else
185 185 html << content_tag('li',
186 186 yield(page.to_s, {page_param => page}),
187 187 :class => 'page')
188 188 end
189 189 previous = page
190 190 end
191 191
192 192 if paginator.multiple_pages?
193 193 # \xc2\xbb(utf-8) = &#187;
194 194 text = l(:label_next) + " \xc2\xbb"
195 195 if paginator.next_page
196 196 html << content_tag('li',
197 197 yield(text, {page_param => paginator.next_page},
198 198 :accesskey => accesskey(:next)),
199 199 :class => 'next page')
200 200 else
201 201 html << content_tag('li', content_tag('span', text), :class => 'next')
202 202 end
203 203 end
204 204 html << '</ul>'
205 205
206 206 info = ''.html_safe
207 207 info << content_tag('span', "(#{paginator.first_item}-#{paginator.last_item}/#{paginator.item_count})", :class => 'items') + ' '
208 208 if per_page_links != false && links = per_page_links(paginator, &block)
209 209 info << content_tag('span', links.to_s, :class => 'per-page')
210 210 end
211 211 html << content_tag('span', info)
212 212
213 213 html.html_safe
214 214 end
215 215
216 216 # Renders the "Per page" links.
217 217 def per_page_links(paginator, &block)
218 218 values = per_page_options(paginator.per_page, paginator.item_count)
219 219 if values.any?
220 220 links = values.collect do |n|
221 221 if n == paginator.per_page
222 222 content_tag('span', n.to_s, :class => 'selected')
223 223 else
224 224 yield(n, :per_page => n, paginator.page_param => nil)
225 225 end
226 226 end
227 227 l(:label_display_per_page, links.join(', ')).html_safe
228 228 end
229 229 end
230 230
231 231 def per_page_options(selected=nil, item_count=nil)
232 232 options = Setting.per_page_options_array
233 233 if item_count && options.any?
234 234 if item_count > options.first
235 235 max = options.detect {|value| value >= item_count} || item_count
236 236 else
237 237 max = item_count
238 238 end
239 239 options = options.select {|value| value <= max || value == selected}
240 240 end
241 241 if options.empty? || (options.size == 1 && options.first == selected)
242 242 []
243 243 else
244 244 options
245 245 end
246 246 end
247 247 end
248 248 end
249 249 end
@@ -1,218 +1,218
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class IssuesTest < Redmine::IntegrationTest
21 21 fixtures :projects,
22 22 :users, :email_addresses,
23 23 :roles,
24 24 :members,
25 25 :member_roles,
26 26 :trackers,
27 27 :projects_trackers,
28 28 :enabled_modules,
29 29 :issue_statuses,
30 30 :issues,
31 31 :enumerations,
32 32 :custom_fields,
33 33 :custom_values,
34 34 :custom_fields_trackers,
35 35 :attachments
36 36
37 37 # create an issue
38 38 def test_add_issue
39 39 log_user('jsmith', 'jsmith')
40 40
41 41 get '/projects/ecookbook/issues/new'
42 42 assert_response :success
43 43 assert_template 'issues/new'
44 44
45 45 issue = new_record(Issue) do
46 46 post '/projects/ecookbook/issues',
47 47 :issue => { :tracker_id => "1",
48 48 :start_date => "2006-12-26",
49 49 :priority_id => "4",
50 50 :subject => "new test issue",
51 51 :category_id => "",
52 52 :description => "new issue",
53 53 :done_ratio => "0",
54 54 :due_date => "",
55 55 :assigned_to_id => "" },
56 56 :custom_fields => {'2' => 'Value for field 2'}
57 57 end
58 58 # check redirection
59 59 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
60 60 follow_redirect!
61 61 assert_equal issue, assigns(:issue)
62 62
63 63 # check issue attributes
64 64 assert_equal 'jsmith', issue.author.login
65 65 assert_equal 1, issue.project.id
66 66 assert_equal 1, issue.status.id
67 67 end
68 68
69 69 def test_create_issue_by_anonymous_without_permission_should_fail
70 70 Role.anonymous.remove_permission! :add_issues
71 71
72 72 assert_no_difference 'Issue.count' do
73 73 post '/projects/1/issues', :tracker_id => "1", :issue => {:subject => "new test issue"}
74 74 end
75 75 assert_response 302
76 76 end
77 77
78 78 def test_create_issue_by_anonymous_with_custom_permission_should_succeed
79 79 Role.anonymous.remove_permission! :add_issues
80 80 Member.create!(:project_id => 1, :principal => Group.anonymous, :role_ids => [3])
81 81
82 82 issue = new_record(Issue) do
83 83 post '/projects/1/issues', :tracker_id => "1", :issue => {:subject => "new test issue"}
84 84 assert_response 302
85 85 end
86 86 assert_equal User.anonymous, issue.author
87 87 end
88 88
89 89 # add then remove 2 attachments to an issue
90 90 def test_issue_attachments
91 91 log_user('jsmith', 'jsmith')
92 92 set_tmp_attachments_directory
93 93
94 94 attachment = new_record(Attachment) do
95 95 put '/issues/1',
96 96 :notes => 'Some notes',
97 97 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'This is an attachment'}}
98 98 assert_redirected_to "/issues/1"
99 99 end
100 100
101 101 assert_equal Issue.find(1), attachment.container
102 102 assert_equal 'testfile.txt', attachment.filename
103 103 assert_equal 'This is an attachment', attachment.description
104 104 # verify the size of the attachment stored in db
105 105 #assert_equal file_data_1.length, attachment.filesize
106 106 # verify that the attachment was written to disk
107 107 assert File.exist?(attachment.diskfile)
108 108
109 109 # remove the attachments
110 110 Issue.find(1).attachments.each(&:destroy)
111 111 assert_equal 0, Issue.find(1).attachments.length
112 112 end
113 113
114 114 def test_other_formats_links_on_index
115 115 get '/projects/ecookbook/issues'
116 116
117 117 %w(Atom PDF CSV).each do |format|
118 118 assert_select 'a[rel=nofollow][href=?]', "/projects/ecookbook/issues.#{format.downcase}", :text => format
119 119 end
120 120 end
121 121
122 122 def test_other_formats_links_on_index_without_project_id_in_url
123 123 get '/issues', :project_id => 'ecookbook'
124 124
125 125 %w(Atom PDF CSV).each do |format|
126 126 assert_select 'a[rel=nofollow][href=?]', "/projects/ecookbook/issues.#{format.downcase}", :text => format
127 127 end
128 128 end
129 129
130 130 def test_pagination_links_on_index
131 131 with_settings :per_page_options => '2' do
132 132 get '/projects/ecookbook/issues'
133 133
134 134 assert_select 'a[href=?]', '/projects/ecookbook/issues?page=2', :text => '2'
135 135 end
136 136 end
137 137
138 def test_pagination_links_on_index_without_project_id_in_url
138 def test_pagination_links_should_preserve_query_parameters
139 139 with_settings :per_page_options => '2' do
140 get '/issues', :project_id => 'ecookbook'
141
142 assert_select 'a[href=?]', '/projects/ecookbook/issues?page=2', :text => '2'
140 get '/projects/ecookbook/issues?foo=bar'
141
142 assert_select 'a[href=?]', '/projects/ecookbook/issues?foo=bar&page=2', :text => '2'
143 143 end
144 144 end
145 145
146 146 def test_issue_with_user_custom_field
147 147 @field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true, :trackers => Tracker.all)
148 148 Role.anonymous.add_permission! :add_issues, :edit_issues
149 149 users = Project.find(1).users.uniq.sort
150 150 tester = users.first
151 151
152 152 # Issue form
153 153 get '/projects/ecookbook/issues/new'
154 154 assert_response :success
155 155 assert_select 'select[name=?]', "issue[custom_field_values][#{@field.id}]" do
156 156 assert_select 'option', users.size + 1 # +1 for blank value
157 157 assert_select 'option[value=?]', tester.id.to_s, :text => tester.name
158 158 end
159 159
160 160 # Create issue
161 161 issue = new_record(Issue) do
162 162 post '/projects/ecookbook/issues',
163 163 :issue => {
164 164 :tracker_id => '1',
165 165 :priority_id => '4',
166 166 :subject => 'Issue with user custom field',
167 167 :custom_field_values => {@field.id.to_s => users.first.id.to_s}
168 168 }
169 169 assert_response 302
170 170 end
171 171
172 172 # Issue view
173 173 follow_redirect!
174 174 assert_select ".cf_#{@field.id}" do
175 175 assert_select '.label', :text => 'Tester:'
176 176 assert_select '.value', :text => tester.name
177 177 end
178 178 assert_select 'select[name=?]', "issue[custom_field_values][#{@field.id}]" do
179 179 assert_select 'option', users.size + 1 # +1 for blank value
180 180 assert_select 'option[value=?][selected=selected]', tester.id.to_s, :text => tester.name
181 181 end
182 182
183 183 new_tester = users[1]
184 184 with_settings :default_language => 'en' do
185 185 # Update issue
186 186 assert_difference 'Journal.count' do
187 187 put "/issues/#{issue.id}",
188 188 :notes => 'Updating custom field',
189 189 :issue => {
190 190 :custom_field_values => {@field.id.to_s => new_tester.id.to_s}
191 191 }
192 192 assert_redirected_to "/issues/#{issue.id}"
193 193 end
194 194 # Issue view
195 195 follow_redirect!
196 196 assert_select 'ul.details li', :text => "Tester changed from #{tester} to #{new_tester}"
197 197 end
198 198 end
199 199
200 200 def test_update_using_invalid_http_verbs
201 201 subject = 'Updated by an invalid http verb'
202 202
203 203 get '/issues/update/1', {:issue => {:subject => subject}}, credentials('jsmith')
204 204 assert_response 404
205 205 assert_not_equal subject, Issue.find(1).subject
206 206
207 207 post '/issues/1', {:issue => {:subject => subject}}, credentials('jsmith')
208 208 assert_response 404
209 209 assert_not_equal subject, Issue.find(1).subject
210 210 end
211 211
212 212 def test_get_watch_should_be_invalid
213 213 assert_no_difference 'Watcher.count' do
214 214 get '/watchers/watch?object_type=issue&object_id=1', {}, credentials('jsmith')
215 215 assert_response 404
216 216 end
217 217 end
218 218 end
General Comments 0
You need to be logged in to leave comments. Login now