Class: Decidim::Query

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

Direct Known Subclasses

Admin::ActiveUsersCounter, Admin::NewsletterRecipients, Admin::UserFilter, Admin::UserGroupsEvaluation, Assemblies::Admin::AdminUsers, Assemblies::Admin::AssemblyMembers, Assemblies::AssembliesWithUserRole, Assemblies::FilteredAssemblies, Assemblies::OrganizationAssemblies, Assemblies::OrganizationPrioritizedAssemblies, Assemblies::OrganizationPublishedAssemblies, Assemblies::ParentAssemblies, Assemblies::ParentAssembliesForSelect, Assemblies::PrioritizedAssemblies, Assemblies::PromotedAssemblies, Assemblies::PublishedAssemblies, Assemblies::VisibleAssemblies, Budgets::FilteredProjects, Comments::SortedComments, Conferences::Admin::AdminUsers, Conferences::Admin::ConferenceInvites, Conferences::Admin::ConferenceSpeakers, Conferences::ConferenceProgramMeetings, Conferences::ConferenceProgramMeetingsByDay, Conferences::ConferencesWithUserRole, Conferences::OrganizationConferences, Conferences::OrganizationPrioritizedConferences, Conferences::OrganizationPublishedConferences, Conferences::PrioritizedConferences, Conferences::PromotedConferences, Conferences::PublishedConferences, Conferences::VisibleConferences, Consultations::Admin::AdminUsers, Consultations::OrganizationActiveConsultations, Consultations::OrganizationConsultations, Consultations::OrganizationQuestions, Elections::Admin::PendingActions, Elections::Admin::VotesForStatistics, Elections::ElectionsFinishedToEnd, Elections::ElectionsReadyToStart, Elections::FilteredElections, Elections::Trustees::ByParticipatorySpace, Elections::Trustees::ByParticipatorySpaceTrusteeIds, Elections::Votes::LastVoteForVoter, Elections::Votes::PendingVotes, Forms::QuestionnaireParticipant, Forms::QuestionnaireParticipants, Forms::QuestionnaireUserAnswers, Initiatives::Admin::AdminUsers, Initiatives::Admin::ManageableInitiatives, Initiatives::FreetextInitiativeTypes, Initiatives::InitiativeTypes, Initiatives::InitiativesCreated, Initiatives::InitiativesPromoted, Initiatives::OrganizationPrioritizedInitiatives, Initiatives::OutdatedValidatingInitiatives, Initiatives::SimilarInitiatives, Initiatives::SupportPeriodFinishedInitiatives, Initiatives::UserAuthorizations, Meetings::Admin::Invites, Meetings::FilteredMeetings, Meetings::QuestionnaireUserAnswers, Messaging::UserConversations, ParticipatoryProcesses::Admin::AdminUsers, ParticipatoryProcesses::Admin::Moderators, ParticipatoryProcesses::FilteredParticipatoryProcessGroups, ParticipatoryProcesses::FilteredParticipatoryProcesses, ParticipatoryProcesses::GroupParticipatoryProcesses, ParticipatoryProcesses::GroupPublishedParticipatoryProcesses, ParticipatoryProcesses::HighlightedParticipatoryProcesses, ParticipatoryProcesses::OrganizationParticipatoryProcessGroups, ParticipatoryProcesses::OrganizationParticipatoryProcesses, ParticipatoryProcesses::OrganizationPrioritizedParticipatoryProcessGroups, ParticipatoryProcesses::OrganizationPrioritizedParticipatoryProcesses, ParticipatoryProcesses::OrganizationPromotedParticipatoryProcessGroups, ParticipatoryProcesses::OrganizationPublishedParticipatoryProcesses, ParticipatoryProcesses::ParticipatoryProcessesByGroup, ParticipatoryProcesses::PrioritizedParticipatoryProcesses, ParticipatoryProcesses::PromotedParticipatoryProcessGroups, ParticipatoryProcesses::PromotedParticipatoryProcesses, ParticipatoryProcesses::PublishedParticipatoryProcesses, ParticipatoryProcesses::StatsFollowersCount, ParticipatoryProcesses::StatsParticipantsCount, ParticipatoryProcesses::VisibleParticipatoryProcessGroups, ParticipatoryProcesses::VisibleParticipatoryProcesses, ParticipatoryProcessesWithUserRole, Proposals::FilteredProposals, Proposals::SimilarProposals, PublicActivities, PublicComponents, SimilarEmendations, Sortitions::Admin::ParticipatorySpaceProposalComponents, Sortitions::Admin::ParticipatorySpaceProposals, Sortitions::FilteredSortitions, StatsUsersCount, UserGroups::AcceptedMemberships, UserGroups::AcceptedUserGroups, UserGroups::AcceptedUsers, UserGroups::AdminMemberships, UserGroups::InvitedMemberships, UserGroups::ManageableUserGroups, UserGroups::MemberMemberships, Verifications::Authorizations, Verifications::AuthorizationsBeforeDate, Votings::Admin::AdminUsers, Votings::Admin::BallotStyleByVotingCode, Votings::Admin::PollingOfficersJoinPollingStations, Votings::Admin::PollingOfficersJoinPollingStationsAndUser, Votings::Admin::PollingOfficersJoinUser, Votings::OrganizationPrioritizedVotings, Votings::OrganizationPromotedVotings, Votings::OrganizationPublishedVotings, Votings::OrganizationVotings, Votings::PrioritizedVotings, Votings::PublishedVotings, Votings::Votes::InPersonVoteForVoter, Votings::Votes::PendingInPersonVotes

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 'decidim-core/lib/decidim/query.rb', line 26

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

Class Method Details

.merge(*queries) ⇒ Object


22
23
24
# File 'decidim-core/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 'decidim-core/lib/decidim/query.rb', line 80

def cached_query
  @cached_query ||= query
end

#each(&block) ⇒ Object


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

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

#eager?Boolean

Returns:

  • (Boolean)

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

def eager?
  cached_query.is_a?(Array)
end

#exists?Boolean

Returns:

  • (Boolean)

58
59
60
61
62
# File 'decidim-core/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 'decidim-core/lib/decidim/query.rb', line 64

def none?
  !exists?
end

#queryObject


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

def query
  @scope
end

#relation?Boolean

Returns:

  • (Boolean)

72
73
74
# File 'decidim-core/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 'decidim-core/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