Class: Ransack::Visitor

Inherits:
Object
  • Object
show all
Defined in:
lib/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

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)


44
45
46
47
48
49
50
51
# File 'lib/ransack/visitor.rb', line 44

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

#visit(object) ⇒ Object



53
54
55
# File 'lib/ransack/visitor.rb', line 53

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

#visit_and(object) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/ransack/visitor.rb', line 28

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



39
40
41
42
# File 'lib/ransack/visitor.rb', line 39

def visit_or(object)
  nodes = object.values.map { |o| accept(o) }.compact
  nodes.inject(&:or)
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



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ransack/visitor.rb', line 57

def visit_Ransack_Nodes_Sort(object)
  if object.valid?
    if object.attr.is_a?(Arel::Attributes::Attribute)
      object.attr.send(object.dir)
    else
      ordered(object)
    end
  else
    scope_name = :"sort_by_#{object.name}_#{object.dir}"
    scope_name if object.context.object.respond_to?(scope_name)
  end
end