Class: Benedictus::RelationResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/benedictus/relation_resolver.rb

Defined Under Namespace

Classes: RawSqlAdapter

Constant Summary collapse

MAX_DEPTH =
5

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(value) ⇒ Object



23
24
25
# File 'lib/benedictus/relation_resolver.rb', line 23

def self.call(value)
  new.call(value)
end

Instance Method Details

#call(value, depth: 0) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/benedictus/relation_resolver.rb', line 27

def call(value, depth: 0)
  raise UnresolvableExpression, "exceeded resolution depth (#{MAX_DEPTH})" if depth > MAX_DEPTH

  return value if active_record_relation?(value)
  return call(value.call, depth: depth + 1) if value.respond_to?(:call)
  return call(value.to_relation, depth: depth + 1) if value.respond_to?(:to_relation)
  return RawSqlAdapter.new(value.to_sql) if value.respond_to?(:to_sql)

  raise UnresolvableExpression,
        "Expression returned a #{value.class}; expected an ActiveRecord::Relation " \
        "or something responding to .to_sql."
end