Skip to content

Commit

Permalink
rework order_by for tag page wiki pagination (publiclab#8422)
Browse files Browse the repository at this point in the history
* rework order_by for publiclab#7334

* adjust sort dropdown text

* rubocop/codeclimate formatting
  • Loading branch information
jywarren authored and manchere committed Feb 13, 2021
1 parent b1e1e35 commit e20eef6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
21 changes: 10 additions & 11 deletions app/controllers/tag_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,11 @@ def show
@node = @wiki # expose the wiki node in the @node variable so we get open graph meta tags in the layout

default_type = params[:id].match?('question:') ? 'questions' : 'note'
if params[:order].nil?
params[:order] = 'last_updated' # default ordering set
end

@node_type = params[:node_type] || default_type
@start = Time.parse(params[:start]) if params[:start]
@end = Time.parse(params[:end]) if params[:end]

order_by = if params[:order] == 'views'
'node.views DESC'
elsif params[:order] == 'likes'
'node.cached_likes DESC'
elsif params[:order] == 'last_updated'
'node_revisions.timestamp DESC'
end

node_type = if %w(questions note).include?(@node_type)
'note'
elsif @node_type == 'wiki'
Expand Down Expand Up @@ -127,6 +116,16 @@ def show
end
end

order_by = if params[:order] == 'views'
'node.views DESC'
elsif params[:order] == 'likes'
'node.cached_likes DESC'
elsif @node_type == 'wiki' # wiki sorting by timestamp isn't working; https://github.com/publiclab/plots2/issues/7334#issuecomment-696938352
'node.nid DESC'
else # params[:order] == 'last_updated'
'node_revisions.timestamp DESC'
end

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

Expand Down
16 changes: 13 additions & 3 deletions app/views/tag/show/_sort.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@
<button class="btn btn-outline-secondary dropdown-toggle" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" style="margin-left:14px;">Sort by</button>
<div class="dropdown-menu dropdown-menu-right mt-1" aria-labelledby="dropdownMenuButton">
<% if params[:action] == "show" %>
<a class="dropdown-item <% if params[:order] == 'last_updated' %> active<% end %>" href="/tag/<%= params[:id] %>"><i class="fa fa-clock-o"></i> <span class="d-lg-inline">by last updated</span></a>
<a class="dropdown-item <% if params[:order] == 'views' %> active<% end %>" href="?order=views"><i class="fa fa-eye"></i> <span class="d-lg-inline">by views</span></a>
<a class="dropdown-item <% if params[:order] == 'likes' %> active<% end %>" href="?order=likes"><i class="fa fa-star"></i> <span class="d-lg-inline">by likes</span></a>
<a class="dropdown-item <% if params[:order] == 'last_updated' %> active<% end %>" href="/tag/<%= params[:id] %>">
<% if @node_type == 'wiki' %>
<i class="fa fa-clock-o"></i> <span class="d-lg-inline">by created date</span>
<% else %>
<i class="fa fa-clock-o"></i> <span class="d-lg-inline">by last updated</span>
<% end %>
</a>
<a class="dropdown-item <% if params[:order] == 'views' %> active<% end %>" href="?order=views">
<i class="fa fa-eye"></i> <span class="d-lg-inline">by views</span>
</a>
<a class="dropdown-item <% if params[:order] == 'likes' %> active<% end %>" href="?order=likes">
<i class="fa fa-star"></i> <span class="d-lg-inline">by likes</span>
</a>
<% end %>
</div>
</div>

0 comments on commit e20eef6

Please sign in to comment.