Class: Decidim::Elections::ElectionPresenter

Inherits:
ResourcePresenter
  • Object
show all
Includes:
ActionView::Helpers::UrlHelper, ResourceHelper
Defined in:
app/presenters/decidim/elections/election_presenter.rb

Instance Method Summary collapse

Instance Method Details

#electionObject



9
10
11
# File 'app/presenters/decidim/elections/election_presenter.rb', line 9

def election
  __getobj__
end

#election_pathObject



13
14
15
16
17
# File 'app/presenters/decidim/elections/election_presenter.rb', line 13

def election_path
  return nil unless election

  Decidim::ResourceLocatorPresenter.new(election).path
end

#title(html_escape: false, all_locales: false) ⇒ Object



19
20
21
22
23
# File 'app/presenters/decidim/elections/election_presenter.rb', line 19

def title(html_escape: false, all_locales: false)
  return unless election

  super(election.title, html_escape, all_locales)
end

#to_json(admin: false) ⇒ Object

A JSON representation of the election, including its questions and response options. Suitable for rendering results in real time. Unless ‘admin: true` is passed, only results for questions with published results are included.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/presenters/decidim/elections/election_presenter.rb', line 28

def to_json(admin: false)
  {
    id: election.id,
    ongoing: election.ongoing?,
    status: election.status,
    start_date: election.start_at&.iso8601,
    end_date: election.end_at.iso8601,
    title: election.translated_attribute(title),
    description: election.translated_attribute(description),
    questions: questions.map do |question|
      {
        id: question.id,
        body: translated_attribute(question.body),
        position: question.position,
        voting_enabled: question.voting_enabled?,
        published_results: question.published_results?
      }.tap do |hash|
        next unless admin || result_published_questions.include?(question)

        hash[:total_votes] = question.total_votes
        hash[:total_votes_text] = I18n.t("total_votes", scope: "decidim.elections.elections.vote_results", count: question.total_votes)
      end.merge(
        response_options: question.response_options.map do |option|
          {
            id: option.id,
            body: translated_attribute(option.body)
          }.tap do |hash|
            next unless admin || result_published_questions.include?(question)

            hash[:votes_count] = option.votes_count
            hash[:votes_count_text] = I18n.t("votes_count", scope: "decidim.elections.elections.show", count: option.votes_count)
            hash[:votes_percent_text] = number_to_percentage(option.votes_percent, precision: 1)
            hash[:votes_percent] = option.votes_percent
          end
        end
      )
    end
  }
end