diff --git a/app/assets/stylesheets/admin.scss b/app/assets/stylesheets/admin.scss index 30cfa01ec..bfa6aa445 100644 --- a/app/assets/stylesheets/admin.scss +++ b/app/assets/stylesheets/admin.scss @@ -227,6 +227,7 @@ $sidebar-active: #f4fcd0; a { color: inherit; + white-space: nowrap; } } diff --git a/app/controllers/admin/budget_investments_controller.rb b/app/controllers/admin/budget_investments_controller.rb index 60ab11d7c..7bdd7254d 100644 --- a/app/controllers/admin/budget_investments_controller.rb +++ b/app/controllers/admin/budget_investments_controller.rb @@ -77,7 +77,8 @@ class Admin::BudgetInvestmentsController < Admin::BaseController def load_investments @investments = Budget::Investment.scoped_filter(params, @current_filter) - .order_filter(params[:sort_by]) + .order_filter(params[:sort_by], params[:direction]) + @investments = @investments.page(params[:page]) unless request.format.csv? end diff --git a/app/helpers/budget_investments_helper.rb b/app/helpers/budget_investments_helper.rb index 6725fbf11..617fcc2fd 100644 --- a/app/helpers/budget_investments_helper.rb +++ b/app/helpers/budget_investments_helper.rb @@ -5,9 +5,15 @@ module BudgetInvestmentsHelper def link_to_investments_sorted_by(column) sorting_option = column.downcase + direction = sorting_option && params[:direction] == "asc" ? "desc" : "asc" + icon = direction == "asc" ? "icon-arrow-top" : "icon-arrow-down" + icon = sorting_option == params[:sort_by] ? icon : "" + + translation = t("admin.budget_investments.index.sort_by.#{sorting_option}") + link_to( - t("admin.budget_investments.index.sort_by.#{sorting_option}"), - admin_budget_budget_investments_path(sort_by: sorting_option) + "#{translation} ".html_safe, + admin_budget_budget_investments_path(sort_by: sorting_option, direction: direction) ) end diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 1b528a9cb..16a9dff0b 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -1,6 +1,6 @@ class Budget class Investment < ActiveRecord::Base - SORTING_OPTIONS = %w(id title supports).freeze + SORTING_OPTIONS = [{"id": "id"}, {"title": "title"}, {"supports": "cached_votes_up"}].freeze include Rails.application.routes.url_helpers include Measurable @@ -139,9 +139,12 @@ class Budget results.where("budget_investments.id IN (?)", ids) end - def self.order_filter(sorting_param) - if sorting_param.present? && SORTING_OPTIONS.include?(sorting_param) - send("sort_by_#{sorting_param}") + def self.order_filter(sorting_param, direction) + sorting_key = sorting_param.to_sym + available_option = SORTING_OPTIONS.select { |sp| sp[sorting_key]}.reduce + if sorting_param.present? && available_option.present? then + %w[asc desc].include?(direction) ? direction : "desc" + order("#{available_option[sorting_key]} #{direction}") else order(cached_votes_up: :desc).order(id: :desc) end