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.
This commit is contained in:
Senén Rodero Rodríguez
2019-02-21 16:24:15 +01:00
committed by voodoorai2000
parent bb2ee6dd3c
commit eb2e402a92
4 changed files with 24 additions and 10 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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) }