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 1 commit
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
22 changes: 18 additions & 4 deletions app/controllers/tag_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def index

@title = I18n.t('tag_controller.tags')
@paginated = true

order_string = "count DESC"
Copy link

Choose a reason for hiding this comment

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

I would put this into a private method within the controller. e.g.

def order_string
  params[:order] === "asc" ? "count ASC" : "count DESC"
end
private :order_string

if params[:order] == "asc"
order_string = "count ASC"
end

if params[:search]
prefix = params[:search]
@tags = Tag.joins(:node_tag, :node)
Expand All @@ -19,31 +25,39 @@ 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"
order_string = "name DESC"

Choose a reason for hiding this comment

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

Yeah, if you do what I was saying, creating a function for defining order_string, this would be come more DRY. 🎉

if params[:order] == "asc"
order_string = "name ASC"
end
@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
order_string = "name DESC"

Choose a reason for hiding this comment

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

Same here

if params[:order] == "asc"
order_string = "name ASC"
end
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
8 changes: 6 additions & 2 deletions app/views/tag/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@

<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>
<% order_type = "desc" %>

Choose a reason for hiding this comment

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

Can we set order_type in the controller like we're setting @tags? I think the controller would be a better place to do that. In general, from my understanding, logic should be kept out as much as possible from the ERB template. It should primarily stay in the controller and model.

<% if params[:order] == "desc" %>
<% order_type = "asc" %>
<% end %>
<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