Class: Ransack::Visitor

Inherits:
Object
  • Object
show all
Defined in:
lib/ransack/visitor.rb,
lib/ransack/adapters/mongoid/ransack/visitor.rb,
lib/ransack/adapters/active_record/3.0/compat.rb,
lib/ransack/adapters/active_record/ransack/visitor.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#accept(object) ⇒ Object


4
5
6
# File 'lib/ransack/visitor.rb', line 4

def accept(object)
  visit(object)
end

#can_accept?(object) ⇒ Boolean


8
9
10
# File 'lib/ransack/visitor.rb', line 8

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

#quoted?(object) ⇒ Boolean


47
48
49
# File 'lib/ransack/visitor.rb', line 47

def quoted?(object)
  raise "not implemented"
end

#visit(object) ⇒ Object


51
52
53
# File 'lib/ransack/visitor.rb', line 51

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

#visit_and(object) ⇒ Object


28
29
30
# File 'lib/ransack/visitor.rb', line 28

def visit_and(object)
  raise "not implemented"
end

#visit_Array(object) ⇒ Object


12
13
14
# File 'lib/ransack/visitor.rb', line 12

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

#visit_or(object) ⇒ Object


32
33
34
35
36
37
38
39
40
41
# File 'lib/ransack/visitor.rb', line 32

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

#visit_Ransack_Nodes_And(object) ⇒ Object


4
5
6
7
8
9
10
11
12
13
# File 'lib/ransack/adapters/active_record/3.0/compat.rb', line 4

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

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

#visit_Ransack_Nodes_Condition(object) ⇒ Object


16
17
18
# File 'lib/ransack/visitor.rb', line 16

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

#visit_Ransack_Nodes_Grouping(object) ⇒ Object


20
21
22
23
24
25
26
# File 'lib/ransack/visitor.rb', line 20

def visit_Ransack_Nodes_Grouping(object)
  if object.combinator == Constants::OR
    visit_or(object)
  else
    visit_and(object)
  end
end

#visit_Ransack_Nodes_Sort(object) ⇒ Object


43
44
45
# File 'lib/ransack/visitor.rb', line 43

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