class Ransack::Adapters::ActiveRecord::Context

def evaluate(search, opts = {})

def evaluate(search, opts = {})
  viz = Visitor.new
  relation = @object.where(viz.accept(search.base))
  if search.sorts.any?
    relation = relation.except(:order)
    # Rather than applying all of the search's sorts in one fell swoop,
    # as the original implementation does, we apply one at a time.
    #
    # If the sort (returned by the Visitor above) is a symbol, we know
    # that it represents a scope on the model and we can apply that
    # scope.
    #
    # Otherwise, we fall back to the applying the sort with the "order"
    # method as the original implementation did. Actually the original
    # implementation used "reorder," which was overkill since we already
    # have a clean slate after "relation.except(:order)" above.
    viz.accept(search.sorts).each do |scope_or_sort|
      if scope_or_sort.is_a?(Symbol)
        relation = relation.send(scope_or_sort)
      else
        case Ransack.options[:postgres_fields_sort_option]
        when :nulls_first
          scope_or_sort = scope_or_sort.direction == :asc ? Arel.sql("#{scope_or_sort.to_sql} NULLS FIRST") : Arel.sql("#{scope_or_sort.to_sql} NULLS LAST")
        when :nulls_last
          scope_or_sort = scope_or_sort.direction == :asc ? Arel.sql("#{scope_or_sort.to_sql} NULLS LAST") : Arel.sql("#{scope_or_sort.to_sql} NULLS FIRST")
        when :nulls_always_first
          scope_or_sort = Arel.sql("#{scope_or_sort.to_sql} NULLS FIRST")
        when :nulls_always_last
          scope_or_sort = Arel.sql("#{scope_or_sort.to_sql} NULLS LAST")
        end
        relation = relation.order(scope_or_sort)
      end
    end
  end
  opts[:distinct] ? relation.distinct : relation
end