From eb2e402a924020533e4d06a8acef6def93cb3b69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Thu, 21 Feb 2019 16:24:15 +0100 Subject: [PATCH] Fix budget investments sorting by title As we cannot order budget investments by any translatable field through AR queries we are doing the same using ruby Array sort method and doing array pagination manually with Kaminari 'paginate_array' helper method. --- .../admin/budget_investments_controller.rb | 5 ++--- app/models/budget/investment.rb | 10 +++++----- app/models/concerns/globalizable.rb | 2 ++ spec/models/budget/investment_spec.rb | 17 +++++++++++++++-- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/controllers/admin/budget_investments_controller.rb b/app/controllers/admin/budget_investments_controller.rb index c1bfbcf35..31f7575df 100644 --- a/app/controllers/admin/budget_investments_controller.rb +++ b/app/controllers/admin/budget_investments_controller.rb @@ -81,9 +81,8 @@ class Admin::BudgetInvestmentsController < Admin::BaseController end def load_investments - @investments = Budget::Investment.scoped_filter(params, @current_filter) - .order_filter(params) - + @investments = Budget::Investment.scoped_filter(params, @current_filter).order_filter(params) + @investments = Kaminari.paginate_array(@investments) if @investments.kind_of?(Array) @investments = @investments.page(params[:page]) unless request.format.csv? end diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index cf25dc47d..15bb3a2f4 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -1,7 +1,7 @@ class Budget require "csv" class Investment < ApplicationRecord - SORTING_OPTIONS = {id: "id", title: "title", supports: "cached_votes_up"}.freeze + SORTING_OPTIONS = { id: "id", supports: "cached_votes_up" }.freeze include ActiveModel::Dirty include Rails.application.routes.url_helpers @@ -119,9 +119,7 @@ class Budget end def self.sort_by_title - includes(:translations). - with_locales(Globalize.fallbacks(I18n.locale)). - order("budget_investment_translations.title ASC") + with_translation.sort_by(&:title) end def self.filter_params(params) @@ -169,10 +167,12 @@ class Budget def self.order_filter(params) sorting_key = params[:sort_by]&.downcase&.to_sym allowed_sort_option = SORTING_OPTIONS[sorting_key] + direction = params[:direction] == "desc" ? "desc" : "asc" if allowed_sort_option.present? - direction = params[:direction] == "desc" ? "desc" : "asc" order("#{allowed_sort_option} #{direction}") + elsif sorting_key == :title + direction == "asc" ? sort_by_title : sort_by_title.reverse else order(cached_votes_up: :desc).order(id: :desc) end diff --git a/app/models/concerns/globalizable.rb b/app/models/concerns/globalizable.rb index 15db83919..e511e5cd5 100644 --- a/app/models/concerns/globalizable.rb +++ b/app/models/concerns/globalizable.rb @@ -17,6 +17,8 @@ module Globalizable translation_class.send :acts_as_paranoid, column: :hidden_at end + scope :with_translation, -> { joins("LEFT OUTER JOIN #{translations_table_name} ON #{table_name}.id = #{translations_table_name}.#{reflections["translations"].foreign_key} AND #{translations_table_name}.locale='#{I18n.locale }'") } + private def searchable_globalized_values diff --git a/spec/models/budget/investment_spec.rb b/spec/models/budget/investment_spec.rb index 579476b55..de83e4404 100644 --- a/spec/models/budget/investment_spec.rb +++ b/spec/models/budget/investment_spec.rb @@ -579,12 +579,25 @@ describe Budget::Investment do end describe "sort_by_title" do + it "sorts using the title in the current locale" do + create(:budget_investment, title_en: "CCCC", title_es: "BBBB", description_en: "CCCC", description_es: "BBBB") + create(:budget_investment, title_en: "DDDD", title_es: "AAAA", description_en: "DDDD", description_es: "AAAA") + + expect(described_class.sort_by_title.map(&:title)).to eq %w[CCCC DDDD] + end + it "should take into consideration title fallbacks when there is no translation for current locale" do - english_investment = create(:budget_investment, :selected, title: "English title") + english_investment = create(:budget_investment, title: "BBBB") spanish_investment = Globalize.with_locale(:es) do I18n.with_locale(:es) do - create(:budget_investment, :selected, title: "Título en español") + create(:budget_investment, title: "AAAA") + end + end + + expect(described_class.sort_by_title.map(&:title)).to eq %w[AAAA BBBB] + end + end describe "search_by_title_or_id" do before { create(:budget_investment) }