Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework order_by for tag page wiki pagination #8422

Merged
merged 3 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>