Testing helper method with using method in the controller - ruby-on-rails

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

Related

How to query nested attributed with MongoDB and Rails

in he next code does not work the line
filter[:category_fields][:name] = params[:filter_category]
it does not take part in querying, and only second filter works:
if params[:commit] == 'Filter' && (params[:filter_category] != '' || params[:filter_status] != '')
if !params[:filter_category].blank?
filter[:category_fields][:name] = params[:filter_category]
end
if !params[:filter_status].blank?
filter[:status] = params[:filter_status]
end
#articles = Article.where(filter).order('created_at DESC')
else
#articles = Article.all.reverse
end
May be I should not add all parameters in one "filter" hash?
My Mongodb looks like:
{
"_id" : ObjectId("57d115b3bf5918085c455ac3"),
"views" : 0,
"status" : "published",
"title" : "sdfSDffd",
"description" : "gsdfg",
"content" : "sdfg",
"category_id" : ObjectId("57d16aeebf59181133d3c35d"),
"updated_at" : ISODate("2016-09-08T14:28:34.257Z"),
"created_at" : ISODate("2016-09-08T07:39:31.757Z"),
"category_fields" : {
"name" : "sdfg"
},
"published_on" : ISODate("2016-09-08T11:56:48.407Z")
}
Thanks!
strange, but written in another form hash assignment helped:
filter.store(:category_fields, {:name => params[:filter_category]})
Finally my index looks like:
def index
filter = Hash.new
if params[:commit] == 'Filter' && (params[:filter_category] != '' || params[:filter_status] != '')
if !params[:filter_category].blank?
filter.store(:category_fields, {:name => Category.find(params[:filter_category]).name})
end
if !params[:filter_status].blank?
filter.store(:status, params[:filter_status])
end
#articles = Article.where(filter).order('created_at DESC')
else
#articles = Article.all.reverse
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"

Grouping 2 lines of code in a helper method

I have a method in my application_helper and 2 lines of code (the last ones in a method) that won't work if they both are active. How to group them in the proper way? (Choosing a code line depends on what page user opens).
def sortable(column, title = nil)
title ||= column.titleize
css_class = sort_column ? "current #{sort_direction}" : nil
direction = sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, params.merge(sort: column, controller: 'analyze/consumptions', action: 'grid_report' , direction: direction), {class: css_class, remote: true, method: 'post'}
link_to title, params.merge(sort: column, controller: 'admin/users', action: 'records' , direction: direction), {class: css_class}
end
Only the return value from your sortable helper method will be added to the buffer.
You can explicitly concatenate all values you wish to include (both link_to's), and return the entire string.
def sortable(column, title = nil)
title ||= column.titleize
css_class = sort_column ? "current #{sort_direction}" : nil
direction = sort_column && sort_direction == "asc" ? "desc" : "asc"
"".html_safe.tap do |buffer| # Be sure you do not have unsafe content when using html_safe
buffer << link_to(title, params.merge(sort: column, controller: 'analyze/consumptions', action: 'grid_report' , direction: direction), {class: css_class, remote: true, method: 'post'})
buffer << link_to(title, params.merge(sort: column, controller: 'admin/users', action: 'records' , direction: direction), {class: css_class})
end # The entire buffer will be returned.
end
Note: It seems to me like it would be better to separate sortable into two different methods, one for each link you wish to render.

Sorting the table not working

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])

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