Skip to content

Commit

Permalink
initial pagy install for tag page pagination, to solve will_paginate …
Browse files Browse the repository at this point in the history
…issues (publiclab#8326)

* initial pagy install

* pagy basic functionality

* other node types

* reorder
  • Loading branch information
jywarren authored and wichanart committed Oct 26, 2021
1 parent 5d6f50d commit 7411013
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 9 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ gem 'unicode-emoji'
gem 'whenever', require: false
gem 'will_paginate', '>= 3.0.6'
gem 'will_paginate-bootstrap4'
gem 'pagy', '>=3.8.3'
gem 'jquery-atwho-rails'
gem 'lemmatizer', '~> 0.2.2'
# To implement incoming mail processing microframework
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ GEM
crack (0.4.3)
safe_yaml (~> 1.0.0)
crass (1.0.6)
critical-path-css-rails (3.1.0)
declarative (0.0.20)
declarative-option (0.1.0)
diff-lcs (1.3)
Expand Down Expand Up @@ -265,6 +266,7 @@ GEM
listen (3.2.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
loadcss-rails (2.0.1)
loofah (2.6.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand Down Expand Up @@ -343,6 +345,7 @@ GEM
openssl (2.2.0)
options (2.3.2)
os (1.1.0)
pagy (3.8.3)
paperclip (6.1.0)
activemodel (>= 4.2.0)
activesupport (>= 4.2.0)
Expand Down Expand Up @@ -595,6 +598,7 @@ DEPENDENCIES
codecov
coffee-rails (~> 5.0.0)
composite_primary_keys
critical-path-css-rails (~> 3.1.0)
execjs
figaro
font-awesome-rails
Expand Down Expand Up @@ -626,6 +630,7 @@ DEPENDENCIES
less-rails (~> 5.0)
letter_opener
listen (~> 3.2.1)
loadcss-rails (~> 2.0.1)
mailman
minitest-reporters (~> 1.4.2)
mocha (~> 1.11)
Expand All @@ -638,6 +643,7 @@ DEPENDENCIES
omniauth-google-oauth2
omniauth-twitter
openssl (~> 2.2.0)
pagy (>= 3.8.3)
paperclip (~> 6.1.0)
passenger
phantomjs
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/tag_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class TagController < ApplicationController
respond_to :html, :xml, :json, :ics
before_action :require_user, only: %i(create delete add_parent)
include Pagy::Backend

def index
@toggle = params[:sort] || "uses"
Expand Down Expand Up @@ -117,9 +118,6 @@ def show
nodes = Node.for_tagname_and_type(params[:id], node_type, question: (@node_type == 'questions'))
end

nodes = nodes.paginate(page: params[:page], per_page: 24).order(order_by)
@paginated = true

if @start && @end
nodes = nodes.where(created: @start.to_i..@end.to_i)
else
Expand All @@ -129,6 +127,9 @@ def show
end
end

@pagy, nodes = pagy(nodes.order(order_by), items: 24)
@paginated = true

@qids = Node.questions.where(status: 1)
.collect(&:nid)
if @qids.empty?
Expand All @@ -140,7 +141,7 @@ def show
end

@answered_questions = []
@questions&.each { |question| @answered_questions << question if question.answers.any?(&:accepted) }
@questions&.each { |question| @answered_questions << question if question.answers.any?(&:accepted) } # TODO: remove this upon refactor to remove answers code
@wikis = nodes if @node_type == 'wiki'
@wikis ||= []
@nodes = nodes if @node_type == 'maps'
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module ApplicationHelper
include Pagy::Frontend

# returns true if user is logged in and has any of the roles given, as ['admin','moderator']
def logged_in_as(roles)
return false unless current_user
Expand Down
7 changes: 5 additions & 2 deletions app/views/notes/_notes.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
<% end %>
</div>
</div>
<%= will_paginate notes, renderer: WillPaginate::ActionView::BootstrapLinkRenderer unless @unpaginated || (unpaginated ||= false) %>

<% if @pagy %>
<%== pagy_bootstrap_nav @pagy %>
<% else %>
<%= will_paginate notes, renderer: WillPaginate::ActionView::BootstrapLinkRenderer unless @unpaginated || (unpaginated ||= false) %>
<% end %>
9 changes: 8 additions & 1 deletion app/views/questions/_new_question.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@
</div>
</div>
</div>
<div class="text-center"><%= will_paginate questions, :renderer => WillPaginate::ActionView::BootstrapLinkRenderer unless @unpaginated || questions.empty? %></div>
<div class="text-center">
<% if @pagy %>
<%== pagy_bootstrap_nav @pagy %>
<% else %>
<%= will_paginate questions, :renderer => WillPaginate::ActionView::BootstrapLinkRenderer unless @unpaginated || questions.empty? %>
<% end %>
</div>

<style>

Expand Down
9 changes: 8 additions & 1 deletion app/views/users/_answered.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@
<% end %>
</div>
</div>
<div class="text-center"><%= will_paginate @questions, :renderer => WillPaginate::ActionView::BootstrapLinkRenderer unless @unpaginated || @questions.empty? %></div>
<div class="text-center">
<% if @pagy %>
<%== pagy_bootstrap_nav @pagy %>
<% else %>
<%= will_paginate @questions, :renderer => WillPaginate::ActionView::BootstrapLinkRenderer unless @unpaginated || @questions.empty? %>
<% end %>
</div>
7 changes: 6 additions & 1 deletion app/views/wiki/_wikis.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@
<% end %>
</table>
<% end %>
<div class="text-center"><%= will_paginate wikis, :renderer => WillPaginate::ActionView::BootstrapLinkRenderer if @paginated %></div>
<%= @pagy.inspect %>
<% if @pagy %>
<%== pagy_bootstrap_nav @pagy %>
<% else %>
<%= will_paginate wikis, :renderer => WillPaginate::ActionView::BootstrapLinkRenderer if @paginated %>
<% end %>
1 change: 1 addition & 0 deletions config/initializers/pagy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'pagy/extras/bootstrap'

0 comments on commit 7411013

Please sign in to comment.