Sorting the table not working - ruby-on-rails

Following the #240 Railscasts i have a list of names like this
arnold
Arnold
Victor
And when I order this by that method, i get this in asc
Arnold
Victor
arnold
And this in desc
arnold
Victor
Arnold
In my controller I have:
def index
#alunos = Aluno.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => params[:npage], :page => params[:page])
end
def sort_column
Aluno.column_names.include?(params[:sort]) ? params[:sort] : "nome"
end
And this in my application_helper
module ApplicationHelper
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, params.merge(:sort => column.downcase, :direction => direction, :page => nil), {:class => css_class}
end
end
And I'm getting this (desc, in this example):
There is something that i must include in my controller or models to order in case INsensitive?
--edited
Im using sqlite3

If you're using SQL as the underlying datastore, you could do
#alunos = Aluno.search(params[:search]).order("LOWER(#{sort_column}) #{sort_direction}").paginate(:per_page => params[:npage], :page => params[:page])

Related

Testing helper method with using method in the controller

module ApplicationHelper
def sortable(column, title = nil)
title ||= column
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
sort_arrow = ""
if(column == sort_column)
if(direction == "desc")
sort_arrow = "▼"
elsif(direction == "asc")
sort_arrow = "▲"
end
end
link_to sort_arrow+title, {:sort => column, :direction => direction,
:search_form => #search_form.attributes
}, {:class => css_class}
end
end
this above is the method I would like to test, the problem is that the sort_column method is on the controller, therefore I get an error message method does not exist. How do I get rid of it or how do I engage the Controller to execute the method?
Edit:
Controller:
class ResultsController < ApplicationController
def sort_column
params[:sort] != nil ? params[:sort] : 'ID'
end
end
Easy way: move the sortable method to ResultsController with a helper_method declaration.
class ResultsController < ApplicationController
helper_method :sortable
def sort_column
params[:sort] != nil ? params[:sort] : 'ID'
end
def sortable(column, title = nil)
title ||= column
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
sort_arrow = ""
if(column == sort_column)
if(direction == "desc")
sort_arrow = "▼"
elsif(direction == "asc")
sort_arrow = "▲"
end
end
link_to sort_arrow + title,
{:sort => column, :direction => direction,
:search_form => #search_form.attributes},
{:class => css_class}
end
end

Railscast #228 Sortable Table Columns: doesn't change a direction

I followed Railscast's episode #228 to create a sortable table column in my app.
My issue: when I press on the column name first time - it shows orders in ascending order. Next time I click - it doesn't automatically show records in descending order; however, if I manually write desc: "http://localhost:3000/admin/users/2/records?direction=desc&sort=created_at" - it works perfectly; what's a problem?
My controller:
def records
#records = #user.records.paginate(page: params[:page], per_page: 20).order(sort_column + " " + sort_direction)
end
private
def sort_column
Record.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
My application_helper:
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, {:sort => column, :direction => direction}, {:class => css_class}
end
My view:
<tr>
<th><%= sortable "created_at" %></th>
</tr>
I had to write:
direction = sort_column && sort_direction == "asc" ? "desc" : "asc"
instead of:
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"

Getting results with a column value prioritized

Rails 3.2.8
I have the following:
def index
params[:direction] ||= "desc"
params[:sort] ||= "status"
#products = product.solr_search( include: [:customer] ){
fulltext params[:query] if params[:query].present?
with :status, (params[:status] || params[:filter]) if (params[:status] || params[:filter]).present?
order_by( params[:sort], params[:direction] ) if params[:sort].present? && params[:direction].present?
paginate page: params[:page], per_page: params[:per_page]
}.results
end
What I would like to do is do the initial sort with a priority when the status column = 'new'. In MySQL, it would be something like this:
SELECT * from product ORDER BY case when status = 'new'
Any idea what my syntax should be?

Rails - Sortable and Highlighted Columns

I am trying to highlight a column title in Rails, I have followed the following tutorial to create ascending and descending sortable columns (http://railscasts.com/episodes/228-sortable-table-columns). However I can't make the higlighting working.
Here is the code I ve been using :
for the view :
%table#movies<br/>
%thead<br/>
%tr<br/>
%th{:id => 'title_header'}= sortable "title", "Movie Title"<br/>
%th Rating<br/>
%th= sortable "release_date", "Release Date"<br/>
%th More Info<br/>
the application helper :
module ApplicationHelper
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "hilite" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, {:sort => column, :direction => direction}, {:class => css_class}
end
end
the controller :
def index
#movies = Movie.order(sort_column + " " + sort_direction)
end
and
def sort_column
Movie.column_names.include?(params[:sort]) ? params[:sort] : "title"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
and the stylesheet :
table#movies th.hilite {
background-color: yellow;
}
I can't find whats wrong, but when I click on the header it is only sorting and not highlighting in yellow... suspect that it has something to do with the *css_class*.
Thanks for your help!!
I believe it's because the class hilite isn't on the table header(<th>) element but on the anchor (<a>) element. Therefor in order to match the element, the CSS would need to be:
table#movies th a.hilite {
background-color: yellow;
}
So you either need to fix up your CSS or fix up which element you are putting the class on.
Thansks Link, got it, had to test the sorting before runing the sortable helper :
%table#movies
%thead
%tr
%th{params[:sort] == "title" ? {:class => "hilite"} : {}, :id => "title_header"}= sortable "title", "Movie Title"
%th Rating
%th{params[:sort] == "release_date" ? {:class => "hilite"} : {}, :id => 'release_date_header'}= sortable "release_date", "Release Date"
%th More Info
Thanks all for your answers!
Benjamin

will_paginate ordering - some columns default ASC, some DESC

I have the following in a controller to enable will_paginate sorting:
def sort_column
['name', 'scheduled', 'status'].include?(params[:sort]) ? params[:sort] : "scheduled"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end
These are supplemented by <%= sortable "status", "Status" %> at the top of a table column - clicking this will sort the table.
How can I make some columns default to ASC and some default to DESC?
Rationale
A text-based column works very well under ASC sorting. However, where I have columns with percentages or numbers, sometimes I want to show the best/highest first instead of requiring that the user click twice to get the column sorted the way they want.
Define a hash with the default sortings, grab the non-set sorting from there:
def sort_direction
sort_defaults = { 'name' => 'asc', 'age' => 'desc', 'scheduled' => 'asc' }
sort_defaults.has_value?(params[:direction]) ? params[:direction] : sort_defaults[sort_column]
end
Figured it out!
This was the original application_helper.rb code from Railscasts #228 and #240.
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
if direction == "desc"
direction = column == sort_column && sort_direction == "desc" ? "asc" : "desc"
else
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
end
link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
end
I changed it to (changes on lines 1 and 4):
def sortable(column, title = nil, direction = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction ||= column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
end
This enables me to now specify a default direction in a column heading:
<div class="col>
<%= sortable "priority_number", "Priority", "desc" %>
</div>

Resources