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:
commit
cd0977283f
3 changed files with 47 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue