]> git.openstreetmap.org Git - rails.git/commitdiff
Add pagination to Issues page
authornertc <davidtsiklauri7@gmail.com>
Mon, 19 Aug 2024 18:09:06 +0000 (22:09 +0400)
committernertc <davidtsiklauri7@gmail.com>
Mon, 26 Aug 2024 09:57:44 +0000 (13:57 +0400)
app/controllers/issues_controller.rb
app/views/issues/_page.html.erb [new file with mode: 0644]
app/views/issues/index.html.erb
config/locales/en.yml
test/system/issues_test.rb

index c24054f77ff3adf4ab44073cea134c9748403b51..1baf533f414aabc4f97ff0b9c3b40f2f803faf5a 100644 (file)
@@ -1,4 +1,6 @@
 class IssuesController < ApplicationController
 class IssuesController < ApplicationController
+  include PaginationMethods
+
   layout "site"
 
   before_action :authorize_web
   layout "site"
 
   before_action :authorize_web
@@ -11,6 +13,8 @@ class IssuesController < ApplicationController
   before_action :check_database_writable, :only => [:resolve, :ignore, :reopen]
 
   def index
   before_action :check_database_writable, :only => [:resolve, :ignore, :reopen]
 
   def index
+    @params = params.permit(:before, :after, :limit, :status, :search_by_user, :issue_type, :last_updated_by)
+    @params[:limit] ||= 50
     @title = t ".title"
 
     @issue_types = []
     @title = t ".title"
 
     @issue_types = []
@@ -18,7 +22,7 @@ class IssuesController < ApplicationController
     @issue_types.push("DiaryEntry", "DiaryComment", "User") if current_user.administrator?
 
     @users = User.joins(:roles).where(:user_roles => { :role => current_user.roles.map(&:role) }).distinct
     @issue_types.push("DiaryEntry", "DiaryComment", "User") if current_user.administrator?
 
     @users = User.joins(:roles).where(:user_roles => { :role => current_user.roles.map(&:role) }).distinct
-    @issues = Issue.visible_to(current_user).order(:updated_at => :desc)
+    @issues = Issue.visible_to(current_user)
 
     # If search
     if params[:search_by_user].present?
 
     # If search
     if params[:search_by_user].present?
@@ -39,6 +43,8 @@ class IssuesController < ApplicationController
       last_updated_by = params[:last_updated_by].to_s == "nil" ? nil : params[:last_updated_by].to_i
       @issues = @issues.where(:updated_by => last_updated_by)
     end
       last_updated_by = params[:last_updated_by].to_s == "nil" ? nil : params[:last_updated_by].to_i
       @issues = @issues.where(:updated_by => last_updated_by)
     end
+
+    @issues, @newer_issues_id, @older_issues_id = get_page_items(@issues, :limit => @params[:limit])
   end
 
   def show
   end
 
   def show
diff --git a/app/views/issues/_page.html.erb b/app/views/issues/_page.html.erb
new file mode 100644 (file)
index 0000000..ed23fb8
--- /dev/null
@@ -0,0 +1,34 @@
+<table class="table table-sm">
+  <thead>
+    <tr>
+      <th><%= t ".status" %></th>
+      <th><%= t ".reports" %></th>
+      <th><%= t ".reported_item" %></th>
+      <th><%= t ".reported_user" %></th>
+      <th><%= t ".last_updated" %></th>
+    </tr>
+  </thead>
+  <tbody>
+    <% @issues.each do |issue| %>
+      <tr>
+        <td><%= t ".states.#{issue.status}" %></td>
+        <td class="text-nowrap"><%= link_to t(".reports_count", :count => issue.reports_count), issue %></td>
+        <td><%= link_to reportable_title(issue.reportable), reportable_url(issue.reportable) %></td>
+        <td><%= link_to issue.reported_user.display_name, issue.reported_user if issue.reported_user %></td>
+        <td>
+          <% if issue.user_updated %>
+            <%= t ".last_updated_time_ago_user_html", :user => link_to(issue.user_updated.display_name, issue.user_updated),
+                                                      :time_ago => friendly_date_ago(issue.updated_at) %>
+          <% else %>
+            <%= friendly_date_ago(issue.updated_at) %>
+          <% end %>
+        </td>
+      </tr>
+    <% end %>
+  </tbody>
+</table>
+<%= render "shared/pagination",
+           :newer_key => "issues.page.newer_issues",
+           :older_key => "issues.page.older_issues",
+           :newer_id => @newer_issues_id,
+           :older_id => @older_issues_id %>
index 523f90846a1a35a4e459dddce6a445c72fca0ad9..6234f755f52b7c836999c15ce9873798965bed41 100644 (file)
 <% if @issues.length == 0 %>
   <p><%= t ".issues_not_found" %></p>
 <% else %>
 <% if @issues.length == 0 %>
   <p><%= t ".issues_not_found" %></p>
 <% else %>
-  <table class="table table-sm">
-    <thead>
-      <tr>
-        <th><%= t ".status" %></th>
-        <th><%= t ".reports" %></th>
-        <th><%= t ".reported_item" %></th>
-        <th><%= t ".reported_user" %></th>
-        <th><%= t ".last_updated" %></th>
-      </tr>
-    </thead>
-    <tbody>
-      <% @issues.each do |issue| %>
-        <tr>
-          <td><%= t ".states.#{issue.status}" %></td>
-          <td class="text-nowrap"><%= link_to t(".reports_count", :count => issue.reports_count), issue %></td>
-          <td><%= link_to reportable_title(issue.reportable), reportable_url(issue.reportable) %></td>
-          <td><%= link_to issue.reported_user.display_name, issue.reported_user if issue.reported_user %></td>
-          <td>
-            <% if issue.user_updated %>
-              <%= t ".last_updated_time_ago_user_html", :user => link_to(issue.user_updated.display_name, issue.user_updated),
-                                                        :time_ago => friendly_date_ago(issue.updated_at) %>
-            <% else %>
-              <%= friendly_date_ago(issue.updated_at) %>
-            <% end %>
-          </td>
-        </tr>
-      <% end %>
-    </tbody>
-  </table>
+  <%= render :partial => "page" %>
 <% end %>
 <% end %>
index 36698f02ce024e27dadba4f9699dbf3225b8023a..a544fe30762e40989c999db20a20e43174594de5 100644 (file)
@@ -1469,11 +1469,17 @@ en:
       search_guidance: "Search Issues:"
       user_not_found: User does not exist
       issues_not_found: No such issues found
       search_guidance: "Search Issues:"
       user_not_found: User does not exist
       issues_not_found: No such issues found
+      link_to_reports: View Reports
+      states:
+        ignored: Ignored
+        open: Open
+        resolved: Resolved
+    page:
+      reported_user: Reported User
       status: Status
       reports: Reports
       last_updated: Last Updated
       last_updated_time_ago_user_html: "%{time_ago} by %{user}"
       status: Status
       reports: Reports
       last_updated: Last Updated
       last_updated_time_ago_user_html: "%{time_ago} by %{user}"
-      link_to_reports: View Reports
       reports_count:
         one: "%{count} Report"
         other: "%{count} Reports"
       reports_count:
         one: "%{count} Report"
         other: "%{count} Reports"
@@ -1482,6 +1488,8 @@ en:
         ignored: Ignored
         open: Open
         resolved: Resolved
         ignored: Ignored
         open: Open
         resolved: Resolved
+      older_issues: Older Issues
+      newer_issues: Newer Issues
     show:
       title: "%{status} Issue #%{issue_id}"
       reports:
     show:
       title: "%{status} Issue #%{issue_id}"
       reports:
index b9b989c075132a9604d01c911dc2017f2613538f..4086a4fe4868c306a55b3bafadd8538fecd97730 100644 (file)
@@ -158,7 +158,35 @@ class IssuesTest < ApplicationSystemTestCase
 
     visit issues_path
 
 
     visit issues_path
 
-    assert_link I18n.t("issues.index.reports_count", :count => issue1.reports_count), :href => issue_path(issue1)
-    assert_link I18n.t("issues.index.reports_count", :count => issue2.reports_count), :href => issue_path(issue2)
+    assert_link I18n.t("issues.page.reports_count", :count => issue1.reports_count), :href => issue_path(issue1)
+    assert_link I18n.t("issues.page.reports_count", :count => issue2.reports_count), :href => issue_path(issue2)
+  end
+
+  def test_issues_pagination
+    1.upto(80).each do
+      user = create(:user)
+      create(:issue, :reportable => user, :reported_user => user, :assigned_role => "administrator")
+    end
+
+    sign_in_as(create(:administrator_user))
+
+    visit issues_path
+
+    # First Page
+    assert_no_content I18n.t("issues.index.user_not_found")
+    assert_no_content I18n.t("issues.index.issues_not_found")
+    assert_css "tr", :count => 51
+
+    # Second Page
+    click_on I18n.t("issues.page.older_issues")
+    assert_no_content I18n.t("issues.index.user_not_found")
+    assert_no_content I18n.t("issues.index.issues_not_found")
+    assert_css "tr", :count => 31, :wait => 1
+
+    # Back to First Page
+    click_on I18n.t("issues.page.newer_issues")
+    assert_no_content I18n.t("issues.index.user_not_found")
+    assert_no_content I18n.t("issues.index.issues_not_found")
+    assert_css "tr", :count => 51, :wait => 1
   end
 end
   end
 end