]> git.openstreetmap.org Git - rails.git/commitdiff
Use before/after pagination on users page
authorAnton Khorev <tony29@yandex.ru>
Tue, 26 Mar 2024 01:27:55 +0000 (04:27 +0300)
committerAnton Khorev <tony29@yandex.ru>
Tue, 26 Mar 2024 02:44:35 +0000 (05:44 +0300)
app/controllers/users_controller.rb
app/views/users/index.html.erb
config/locales/en.yml
test/controllers/users_controller_test.rb

index fbf49ecbe6835cb1ba99ce52193d96c2f835a8e3..b9f1af807f7e09ede8fc1eeea3865cf8736a6f52 100644 (file)
@@ -2,6 +2,7 @@ class UsersController < ApplicationController
   include EmailMethods
   include SessionMethods
   include UserMethods
+  include PaginationMethods
 
   layout "site"
 
@@ -29,16 +30,13 @@ class UsersController < ApplicationController
 
       redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
     else
-      @params = params.permit(:status, :ip)
+      @params = params.permit(:status, :ip, :before, :after)
 
-      conditions = {}
-      conditions[:status] = @params[:status] if @params[:status]
-      conditions[:creation_ip] = @params[:ip] if @params[:ip]
+      users = User.all
+      users = users.where(:status => @params[:status]) if @params[:status]
+      users = users.where(:creation_ip => @params[:ip]) if @params[:ip]
 
-      @user_pages, @users = paginate(:users,
-                                     :conditions => conditions,
-                                     :order => :id,
-                                     :per_page => 50)
+      @users, @newer_users_id, @older_users_id = get_page_items(users, :limit => 50)
     end
   end
 
index 12d5ae7af232edb801b0f5340d86435a1a2c2d39..22a916c8bb0b0a793ceb1287bb9d63a4ed80dd8b 100644 (file)
 
 <% unless @users.empty? %>
   <%= form_tag do %>
+    <%= render "shared/pagination",
+               :newer_key => "users.index.newer",
+               :older_key => "users.index.older",
+               :newer_id => @newer_users_id,
+               :older_id => @older_users_id %>
+
     <%= hidden_field_tag :status, params[:status] if params[:status] %>
     <%= hidden_field_tag :ip, params[:ip] if params[:ip] %>
     <%= hidden_field_tag :page, params[:page] if params[:page] %>
       <thead>
         <tr>
           <td colspan="2">
-            <%= t ".showing",
-                  :page => @user_pages.current_page.number,
-                  :first_item => @user_pages.current_page.first_item,
-                  :last_item => @user_pages.current_page.last_item,
-                  :items => @user_pages.item_count,
-                  :count => @user_pages.current_page.last_item - @user_pages.current_page.first_item + 1 %>
-            <% if @user_pages.page_count > 1 %>
-            | <%= raw pagination_links_each(@user_pages, {}) { |n| link_to n, @params.merge(:page => n) } %>
-            <% end %>
           </td>
           <td>
             <%= check_box_tag "user_all", "1", false %>
       <%= render @users %>
     </table>
 
+    <%= render "shared/pagination",
+               :newer_key => "users.index.newer",
+               :older_key => "users.index.older",
+               :newer_id => @newer_users_id,
+               :older_id => @older_users_id %>
+
     <div>
       <%= submit_tag t(".confirm"), :name => "confirm", :class => "btn btn-primary" %>
       <%= submit_tag t(".hide"), :name => "hide", :class => "btn btn-primary" %>
index 9af89978ef5d917975a2047fa2bd4ea101ee600d..a173a7e4f5ae714e8d929c9c154cf2a763fd73d1 100644 (file)
@@ -2832,9 +2832,8 @@ en:
     index:
       title: Users
       heading: Users
-      showing:
-        one: Page %{page} (%{first_item} of %{items})
-        other: Page %{page} (%{first_item}-%{last_item} of %{items})
+      older: "Older Users"
+      newer: "Newer Users"
       summary_html: "%{name} created from %{ip_address} on %{date}"
       summary_no_ip_html: "%{name} created on %{date}"
       confirm: Confirm Selected Users
index 4ff9e76f6763bf4e967bc5b80b9ecd6f0eac68e9..62bb34279eafa50c69a465ff65d5a2442768e693 100644 (file)
@@ -644,23 +644,44 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
 
     # 100 examples, an administrator, and a granter for the admin.
     assert_equal 102, User.count
+    next_path = users_path
 
-    get users_path
+    get next_path
     assert_response :success
     assert_template :index
     assert_select "table#user_list tbody tr", :count => 50
+    check_no_page_link "Newer Users"
+    next_path = check_page_link "Older Users"
 
-    get users_path, :params => { :page => 2 }
+    get next_path
     assert_response :success
     assert_template :index
     assert_select "table#user_list tbody tr", :count => 50
+    check_page_link "Newer Users"
+    next_path = check_page_link "Older Users"
 
-    get users_path, :params => { :page => 3 }
+    get next_path
     assert_response :success
     assert_template :index
     assert_select "table#user_list tbody tr", :count => 2
+    check_page_link "Newer Users"
+    check_no_page_link "Older Users"
+  end
+
+  private
+
+  def check_no_page_link(name)
+    assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
   end
 
+  def check_page_link(name)
+    assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
+      return buttons.first.attributes["href"].value
+    end
+  end
+
+  public
+
   def test_index_post_confirm
     inactive_user = create(:user, :pending)
     suspended_user = create(:user, :suspended)