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:
committed by
voodoorai2000
parent
bb2ee6dd3c
commit
eb2e402a92
@@ -81,9 +81,8 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def load_investments
|
def load_investments
|
||||||
@investments = Budget::Investment.scoped_filter(params, @current_filter)
|
@investments = Budget::Investment.scoped_filter(params, @current_filter).order_filter(params)
|
||||||
.order_filter(params)
|
@investments = Kaminari.paginate_array(@investments) if @investments.kind_of?(Array)
|
||||||
|
|
||||||
@investments = @investments.page(params[:page]) unless request.format.csv?
|
@investments = @investments.page(params[:page]) unless request.format.csv?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
class Budget
|
class Budget
|
||||||
require "csv"
|
require "csv"
|
||||||
class Investment < ApplicationRecord
|
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 ActiveModel::Dirty
|
||||||
include Rails.application.routes.url_helpers
|
include Rails.application.routes.url_helpers
|
||||||
@@ -119,9 +119,7 @@ class Budget
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.sort_by_title
|
def self.sort_by_title
|
||||||
includes(:translations).
|
with_translation.sort_by(&:title)
|
||||||
with_locales(Globalize.fallbacks(I18n.locale)).
|
|
||||||
order("budget_investment_translations.title ASC")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.filter_params(params)
|
def self.filter_params(params)
|
||||||
@@ -169,10 +167,12 @@ class Budget
|
|||||||
def self.order_filter(params)
|
def self.order_filter(params)
|
||||||
sorting_key = params[:sort_by]&.downcase&.to_sym
|
sorting_key = params[:sort_by]&.downcase&.to_sym
|
||||||
allowed_sort_option = SORTING_OPTIONS[sorting_key]
|
allowed_sort_option = SORTING_OPTIONS[sorting_key]
|
||||||
|
direction = params[:direction] == "desc" ? "desc" : "asc"
|
||||||
|
|
||||||
if allowed_sort_option.present?
|
if allowed_sort_option.present?
|
||||||
direction = params[:direction] == "desc" ? "desc" : "asc"
|
|
||||||
order("#{allowed_sort_option} #{direction}")
|
order("#{allowed_sort_option} #{direction}")
|
||||||
|
elsif sorting_key == :title
|
||||||
|
direction == "asc" ? sort_by_title : sort_by_title.reverse
|
||||||
else
|
else
|
||||||
order(cached_votes_up: :desc).order(id: :desc)
|
order(cached_votes_up: :desc).order(id: :desc)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ module Globalizable
|
|||||||
translation_class.send :acts_as_paranoid, column: :hidden_at
|
translation_class.send :acts_as_paranoid, column: :hidden_at
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def searchable_globalized_values
|
def searchable_globalized_values
|
||||||
|
|||||||
@@ -579,12 +579,25 @@ describe Budget::Investment do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "sort_by_title" do
|
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
|
it "should take into consideration title fallbacks when there is no
|
||||||
translation for current locale" do
|
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
|
spanish_investment = Globalize.with_locale(:es) do
|
||||||
I18n.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
|
describe "search_by_title_or_id" do
|
||||||
before { create(:budget_investment) }
|
before { create(:budget_investment) }
|
||||||
|
|||||||
Reference in New Issue
Block a user