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

Fixed issue#2445 added both direction sort order #2557

Merged
merged 3 commits into from
Jun 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 16 additions & 4 deletions app/controllers/tag_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def index

@title = I18n.t('tag_controller.tags')
@paginated = true
@order_type = params[:order] == "desc" ? "asc" : "desc"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little bit confused... if the order param is desc, we want to set the @order_type to desc...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shauncarland The logic behind this is that, we have to invert the order when a user click on sorting header again. So lets say if current shown order is according to "name descending" then we want front end to request on url with order type be ascending (opposite of descending). I hope you got my point 😄

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 🎉 🎉 🎉 🎉 🎉


if params[:search]
prefix = params[:search]
@tags = Tag.joins(:node_tag, :node)
Expand All @@ -19,31 +21,31 @@ def index
.where('community_tags.date > ?', (DateTime.now - 1.month).to_i)
.where("name LIKE :prefix", prefix: "#{prefix}%")
.group(:name)
.order('count DESC')
.order(order_string)
.paginate(page: params[:page], per_page: 24)
elsif @toggle == "uses"
@tags = Tag.joins(:node_tag, :node)
.select('node.nid, node.status, term_data.*, community_tags.*')
.where('node.status = ?', 1)
.where('community_tags.date > ?', (DateTime.now - 1.month).to_i)
.group(:name)
.order('count DESC')
.order(order_string)
.paginate(page: params[:page], per_page: 24)
elsif @toggle == "name"
@tags = Tag.joins(:node_tag, :node)
.select('node.nid, node.status, term_data.*, community_tags.*')
.where('node.status = ?', 1)
.where('community_tags.date > ?', (DateTime.now - 1.month).to_i)
.group(:name)
.order('name')
.order(order_string)
.paginate(page: params[:page], per_page: 24)
else
tags = Tag.joins(:node_tag, :node)
.select('node.nid, node.status, term_data.*, community_tags.*')
.where('node.status = ?', 1)
.where('community_tags.date > ?', (DateTime.now - 1.month).to_i)
.group(:name)
.order('name')
.order(order_string)

followed = []
not_followed = []
Expand Down Expand Up @@ -436,4 +438,14 @@ def location_modal
def gridsEmbed
render layout: false
end

private

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just do private :order_string at the end of the order_string method. That will make order_string private. The way you have it now, anything method below this line will be private. Let's just make order_string private so no future developer needs to worry about it. :)

Copy link
Contributor Author

@sukhbir-singh sukhbir-singh Apr 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But all other controllers in this project have used same pattern to specify private methods in a class. Please check out the home_controller.rb, application_controller.rb files, etc. So i think its better to do it the same way as other classes have used. 😅


def order_string
if params[:search] || @toggle == "uses"
params[:order] == "asc" ? "count ASC" : "count DESC"
else
params[:order] == "asc" ? "name ASC" : "name DESC"
end
end
end
4 changes: 2 additions & 2 deletions app/views/tag/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

<table class="table">
<tr>
<th><a href = "<%= tags_path %>?sort=name"><%= t('tag.index.tag') %></a> <i class="fa fa-arrows-v"></i></th>
<th><a href = "<%= tags_path %>?sort=uses"><%= t('tag.index.number_of_uses') %></a> <i class="fa fa-arrows-v"></i></th>
<th><a href = "<%= tags_path %>?sort=name&order=<%= @order_type %>"><%= t('tag.index.tag') %></a> <i class="fa fa-arrows-v"></i></th>
<th><a href = "<%= tags_path %>?sort=uses&order=<%= @order_type %>"><%= t('tag.index.number_of_uses') %></a> <i class="fa fa-arrows-v"></i></th>
<th><%= t('tag.index.number_of_subscriptions')%></th>
<% if current_user %>
<th><a href = "<%= tags_path %>?sort=subscribers"><%= t('tag.index.subscriptions') %></a> <i class="fa fa-arrows-v"></i></th>
Expand Down