Skip to content

Commit

Permalink
Fixed: Rails 4.2: NoMethodError: undefined method asc for #<Arel::Nod…
Browse files Browse the repository at this point in the history
…es::NamedFunction... activerecord-hackery#483
  • Loading branch information
roman-franko committed Jan 8, 2018
1 parent be07738 commit c06120b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/ransack/adapters/active_record/ransack/visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,24 @@ def quoted?(object)
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
24 changes: 24 additions & 0 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,30 @@ def self.simple_escaping?
/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
Expand Down
4 changes: 4 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c06120b

Please sign in to comment.