Class: Decidim::Query
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/decidim/query.rb
Direct Known Subclasses
LastActivity, Messaging::UserConversations, ParticipatoryProcessesWithUserRole, ParticipatorySpaceLastActivity, PublicComponents, StatsUsersCount, UserGroups::AcceptedMemberships, UserGroups::AcceptedUserGroups, UserGroups::AcceptedUsers, UserGroups::AdminMemberships, UserGroups::InvitedMemberships, UserGroups::ManageableUserGroups, UserGroups::MemberMemberships
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_query ⇒ Object
80
81
82
|
# File 'lib/decidim/query.rb', line 80
def cached_query
@cached_query ||= query
end
|
#each ⇒ Object
54
55
56
|
# File 'lib/decidim/query.rb', line 54
def each(&)
cached_query.each(&)
end
|
#eager? ⇒ Boolean
76
77
78
|
# File 'lib/decidim/query.rb', line 76
def eager?
cached_query.is_a?(Array)
end
|
#exists? ⇒ 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
64
65
66
|
# File 'lib/decidim/query.rb', line 64
def none?
!exists?
end
|
#query ⇒ Object
30
31
32
|
# File 'lib/decidim/query.rb', line 30
def query
@scope
end
|
#relation? ⇒ 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
|