Fix sorting by name on explore projects page

This commit is contained in:
Douglas Barbosa Alexandre 2018-06-25 16:46:45 -03:00
parent 45453c27ae
commit e8edf620f1
No known key found for this signature in database
GPG Key ID: F1E98EF6393565A0
2 changed files with 16 additions and 6 deletions

View File

@ -12,8 +12,8 @@ module Sortable
scope :order_created_asc, -> { reorder(created_at: :asc) } scope :order_created_asc, -> { reorder(created_at: :asc) }
scope :order_updated_desc, -> { reorder(updated_at: :desc) } scope :order_updated_desc, -> { reorder(updated_at: :desc) }
scope :order_updated_asc, -> { reorder(updated_at: :asc) } scope :order_updated_asc, -> { reorder(updated_at: :asc) }
scope :order_name_asc, -> { reorder("lower(name) asc") } scope :order_name_asc, -> { reorder(Arel::Nodes::Ascending.new(arel_table[:name].lower)) }
scope :order_name_desc, -> { reorder("lower(name) desc") } scope :order_name_desc, -> { reorder(Arel::Nodes::Descending.new(arel_table[:name].lower)) }
end end
module ClassMethods module ClassMethods

View File

@ -40,15 +40,25 @@ describe Sortable do
describe 'ordering by name' do describe 'ordering by name' do
it 'ascending' do it 'ascending' do
expect(relation).to receive(:reorder).with("lower(name) asc") expect(relation).to receive(:reorder).once.and_call_original
relation.order_by('name_asc') table = Regexp.escape(ActiveRecord::Base.connection.quote_table_name(:namespaces))
column = Regexp.escape(ActiveRecord::Base.connection.quote_column_name(:name))
sql = relation.order_by('name_asc').to_sql
expect(sql).to match /.+ORDER BY LOWER\(#{table}.#{column}\) ASC\z/
end end
it 'descending' do it 'descending' do
expect(relation).to receive(:reorder).with("lower(name) desc") expect(relation).to receive(:reorder).once.and_call_original
relation.order_by('name_desc') table = Regexp.escape(ActiveRecord::Base.connection.quote_table_name(:namespaces))
column = Regexp.escape(ActiveRecord::Base.connection.quote_column_name(:name))
sql = relation.order_by('name_desc').to_sql
expect(sql).to match /.+ORDER BY LOWER\(#{table}.#{column}\) DESC\z/
end end
end end