Class: Ransack::Visitor

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

Constant Summary collapse

DISPATCH =
Hash.new do |hash, klass|
  hash[klass] = "visit_#{klass.name.gsub('::', '_')}"
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

Returns:

  • (Boolean)


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

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

#quoted?(object) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
# File 'lib/ransack/visitor.rb', line 50

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

#visit(object) ⇒ Object



59
60
61
# File 'lib/ransack/visitor.rb', line 59

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

#visit_and(object) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/ransack/visitor.rb', line 24

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

#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



35
36
37
38
39
40
41
42
43
44
# File 'lib/ransack/visitor.rb', line 35

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
# File 'lib/ransack/visitor.rb', line 20

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

#visit_Ransack_Nodes_Sort(object) ⇒ Object



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

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