Sha256: 4cfe52913970b8e9caa55aa4ec0005df0c6ac387899a81279cc250d8cff854ce

Contents?: true

Size: 1.36 KB

Versions: 7

Compression:

Stored size: 1.36 KB

Contents

module Ransack
  class Visitor

    def accept(object)
      visit(object)
    end

    def can_accept?(object)
      respond_to? DISPATCH[object.class]
    end

    def visit_Array(object)
      object.map { |o| accept(o) }.compact
    end

    def visit_Ransack_Nodes_Condition(object)
      object.arel_predicate if object.valid?
    end

    def visit_Ransack_Nodes_Grouping(object)
      object.combinator == 'or' ? visit_or(object) : visit_and(object)
    end

    def visit_and(object)
      nodes = object.values.map { |o| accept(o) }.compact
      return nil unless nodes.size > 0

      if nodes.size > 1
        Arel::Nodes::Grouping.new(Arel::Nodes::And.new(nodes))
      else
        nodes.first
      end
    end

    def visit_or(object)
      nodes = object.values.map { |o| accept(o) }.compact
      return nil unless nodes.size > 0

      if nodes.size > 1
        nodes.inject(&:or)
      else
        nodes.first
      end
    end

    def visit_Ransack_Nodes_Sort(object)
      object.attr.send(object.dir) if object.valid?
    end

    def quoted?(object)
      case object
      when Arel::Nodes::SqlLiteral, Bignum, Fixnum
        false
      else
        true
      end
    end

    def visit(object)
      send(DISPATCH[object.class], object)
    end

    DISPATCH = Hash.new do |hash, klass|
      hash[klass] = "visit_#{klass.name.gsub('::', '_')}"
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ransack-1.4.1 lib/ransack/visitor.rb
ransack-1.4.0 lib/ransack/visitor.rb
ransack-1.3.0 lib/ransack/visitor.rb
ransack-1.2.3 lib/ransack/visitor.rb
ransack-1.2.2 lib/ransack/visitor.rb
ransack-1.2.1 lib/ransack/visitor.rb
ransack-1.2.0 lib/ransack/visitor.rb