How can I sort a column using translation file

180 Views Asked by At

I have two tables in my database. Eu tenho duas tabelas no banco de dados. 1º test_case_statuses 2º test_case_runs

The first table has the columns: id and identifier. The contents of this table is this: id identifier 1 new
2 approved 3 reproved

The second table has columns id e status_id.

My application is in portuguese, so I'm using a translation file. This file has the following lines:

labels
   ...
   test_cases_statuses:
     new: "Novo"
     approved: "Aprovado"
  reproved: "Reprovado"
...

The problem is that I need to sort the contents of the table in alphabetical order. For other columns I'm using the following code:

view/test_plan_cases

...

<%= sortable_th @search, :test_case_number, {}, :remote => true %>
...

app/helpers/

def sortable_th(builder = nil, attribute = nil, *args) 

# th_classes = ['green']
th_classes = []
content = nil

unless builder.nil? || attribute.nil?
  th_classes << 'header'
  content = sort_link(builder, attribute, *args)

  if content.include? 'sort_link asc'
    th_classes << 'headerSortDown'
  elsif content.include? 'sort_link desc'
    th_classes << 'headerSortUp'
  end
end

th_classes = th_classes.join(' ')
content_tag :th, content, class: th_classes
 end



def sortable_teste(builder = nil, attribute = nil, *args) 
# th_classes = ['green']
th_classes = []
content = nil

unless builder.nil? || attribute.nil?
  th_classes << 'header'
  content = sort_teste(builder, attribute, *args)

  if content.include? 'sort_teste asc'
    th_classes << 'headerSortDown'
  elsif content.include? 'sort_teste desc'
    th_classes << 'headerSortUp'
  end
end

th_classes = th_classes.join(' ')
content_tag :th, content, class: th_classes

 end

controller/test_case_runs_controller

  @search = @project.test_plan_cases.allowed_for(@release, current_user).search search_values

Need guidance and help to do this. What I'm thinking of doing is to use an enumerator type to save the contents of the translation file. Would look like this:

[1 'Novo',

2 'Aprovado',

3'Reprovado']

After that I lose myself, should create another form of ordering or would like to adapt the above code? I appreciate any help =]

1

There are 1 best solutions below

2
On

One way would be to issue a sort on a vitual field calculated with SQL. You have to build your field definition this way:

CASE status_id
 WHEN 1 THEN ‘Novo′
 WHEN 2 THEN ‘Aprovado′
 WHEN 3 THEN ‘Reprovado′
END

You can then use this as an order clause:

@search = @project.test_plan_cases.some_other_scope.order("#{case_above} ASC")

and similar queries.