Class: Decidim::Elections::Votes::LastVoteForVoter

Inherits:
Query
  • Object
show all
Defined in:
decidim-elections/app/queries/decidim/elections/votes/last_vote_for_voter.rb

Overview

A class used to find the last vote casted by a voter in an election

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Query

#cached_query, #each, #eager?, #exists?, merge, #none?, #relation?, #|

Constructor Details

#initialize(election, voter_id) ⇒ LastVoteForVoter

Returns a new instance of LastVoteForVoter.



16
17
18
19
# File 'decidim-elections/app/queries/decidim/elections/votes/last_vote_for_voter.rb', line 16

def initialize(election, voter_id)
  @voter_id = voter_id
  @election = election
end

Class Method Details

.for(election, voter_id) ⇒ Object

Syntactic sugar to initialize the class and return the queried objects.

election - the election where the vote was casted voter_id - the identifier of the voter



12
13
14
# File 'decidim-elections/app/queries/decidim/elections/votes/last_vote_for_voter.rb', line 12

def self.for(election, voter_id)
  new(election, voter_id).query
end

Instance Method Details

#queryObject



21
22
23
24
25
# File 'decidim-elections/app/queries/decidim/elections/votes/last_vote_for_voter.rb', line 21

def query
  Decidim::Elections::Vote.where(election: @election, voter_id: @voter_id)
                          .order("created_at")
                          .last
end