Class: Ransack::Visitor

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

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)

37
38
39
# File 'lib/ransack/visitor.rb', line 37

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

#visit(object) ⇒ Object


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

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

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


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

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