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

63 lines
1.2 KiB
Ruby
Raw Normal View History

2011-04-11 12:04:31 -04:00
module Ransack
class Visitor
def accept(object)
visit(object)
end
def can_accept?(object)
respond_to? DISPATCH[object.class]
end
def visit_Array(object)
2013-08-12 04:27:50 -04:00
object.map { |o| accept(o) }.compact
2011-04-11 12:04:31 -04:00
end
def visit_Ransack_Nodes_Condition(object)
object.arel_predicate if object.valid?
end
def visit_Ransack_Nodes_Grouping(object)
2014-11-21 14:03:20 -05:00
if object.combinator == Constants::OR
visit_or(object)
else
visit_and(object)
end
end
def visit_and(object)
2014-08-01 02:36:12 -04:00
raise "not implemented"
2011-04-11 12:04:31 -04:00
end
def visit_or(object)
2013-08-12 04:27:50 -04:00
nodes = object.values.map { |o| accept(o) }.compact
2011-04-11 12:04:31 -04:00
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
2011-04-11 12:04:31 -04:00
def quoted?(object)
2014-08-01 02:36:12 -04:00
raise "not implemented"
2011-04-11 12:04:31 -04:00
end
def visit(object)
send(DISPATCH[object.class], object)
end
2014-12-20 17:50:10 -05:00
DISPATCH = Hash.new do |hash, klass|
hash[klass] = "visit_#{
2014-11-21 14:03:20 -05:00
klass.name.gsub(Constants::TWO_COLONS, Constants::UNDERSCORE)
}"
2011-04-11 12:04:31 -04:00
end
end
2013-08-12 04:27:50 -04:00
end