Class: Decidim::Query

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/decidim/query.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope = ActiveRecord::NullRelation) ⇒ Query

Returns a new instance of Query.



26
27
28
# File 'lib/decidim/query.rb', line 26

def initialize(scope = ActiveRecord::NullRelation)
  @scope = scope
end

Class Method Details

.merge(*queries) ⇒ Object



22
23
24
# File 'lib/decidim/query.rb', line 22

def self.merge(*queries)
  queries.reduce(Decidim::Query.new([])) { |a, e| a.merge(e) }
end

Instance Method Details

#cached_queryObject



80
81
82
# File 'lib/decidim/query.rb', line 80

def cached_query
  @cached_query ||= query
end

#each(&block) ⇒ Object



54
55
56
# File 'lib/decidim/query.rb', line 54

def each(&block)
  cached_query.each(&block)
end

#eager?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/decidim/query.rb', line 76

def eager?
  cached_query.is_a?(Array)
end

#exists?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/decidim/query.rb', line 58

def exists?
  return cached_query.exists? if relation?

  cached_query.present?
end

#none?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/decidim/query.rb', line 64

def none?
  !exists?
end

#queryObject



30
31
32
# File 'lib/decidim/query.rb', line 30

def query
  @scope
end

#relation?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/decidim/query.rb', line 72

def relation?
  cached_query.is_a?(ActiveRecord::Relation)
end

#|(other) ⇒ Object Also known as: merge



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/decidim/query.rb', line 34

def |(other)
  return other if @scope.is_a?(Array) && @scope == []

  if relation? && other.relation?
    Decidim::Query.new(cached_query.merge(other.cached_query))
  elsif eager? && other.eager?
    Decidim::Query.new(cached_query | other.cached_query)
  else
    raise Decidim::UnableToComposeQueriesException.new(self, other)
  end
end