1
0
Fork 0
mirror of https://github.com/activerecord-hackery/ransack.git synced 2022-11-09 13:47:45 -05:00

Merge pull request #876 from roman-franko/master

Fixed: Rails 4.2: NoMethodError: undefined method asc for #<Arel::Nod…
This commit is contained in:
Sean 2018-02-07 12:43:37 +01:00 committed by GitHub
commit cd0977283f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 0 deletions

View file

@ -20,5 +20,24 @@ module Ransack
end
end
def visit_Ransack_Nodes_Sort(object)
return unless object.valid?
if object.attr.is_a?(Arel::Attributes::Attribute)
object.attr.send(object.dir)
else
ordered(object)
end
end
private
def ordered(object)
case object.dir
when 'asc'.freeze
Arel::Nodes::Ascending.new(object.attr)
when 'desc'.freeze
Arel::Nodes::Descending.new(object.attr)
end
end
end
end

View file

@ -544,6 +544,30 @@ module Ransack
/BETWEEN 2 AND 6 GROUP BY articles.person_id \) DESC/
)
end
context 'case insensitive sorting' do
it 'allows sort by desc' do
search = Person.search(sorts: ['name_case_insensitive desc'])
expect(search.result.to_sql).to match /ORDER BY LOWER(.*) DESC/
end
it 'allows sort by asc' do
search = Person.search(sorts: ['name_case_insensitive asc'])
expect(search.result.to_sql).to match /ORDER BY LOWER(.*) ASC/
end
end
context 'regular sorting' do
it 'allows sort by desc' do
search = Person.search(sorts: ['name desc'])
expect(search.result.to_sql).to match /ORDER BY .* DESC/
end
it 'allows sort by asc' do
search = Person.search(sorts: ['name asc'])
expect(search.result.to_sql).to match /ORDER BY .* ASC/
end
end
end
describe '#ransackable_attributes' do

View file

@ -96,6 +96,10 @@ class Person < ActiveRecord::Base
Arel.sql('people.id')
end
ransacker :name_case_insensitive, type: :string do
arel_table[:name].lower
end
ransacker :with_arguments, args: [:parent, :ransacker_args] do |parent, args|
min, max = args
query = <<-SQL