Class: Decidim::Elections::Question

Inherits:
ApplicationRecord show all
Includes:
Loggable, Resourceable, Traceable
Defined in:
decidim-elections/app/models/decidim/elections/question.rb

Overview

The data store for a Question in the Decidim::Elections component. It stores a title, description and a maximum number of selection that voters can choose.

Instance Method Summary collapse

Instance Method Details

#blank_votesObject


33
34
35
# File 'decidim-elections/app/models/decidim/elections/question.rb', line 33

def blank_votes
  @blank_votes ||= results.blank_answers.sum(:value)
end

#blank_votes_percentageObject

A result percentage relative to the question Returns a Float.


43
44
45
46
47
48
49
50
# File 'decidim-elections/app/models/decidim/elections/question.rb', line 43

def blank_votes_percentage
  @blank_votes_percentage ||= begin
    return 0 unless results_total.positive?

    result = blank_votes.to_f / results_total * 100.0
    result.round
  end
end

#nota_option?Boolean

Public: Checks if the question accepts a blank/NOTA as an answer

Returns a boolean.

Returns:

  • (Boolean)

29
30
31
# File 'decidim-elections/app/models/decidim/elections/question.rb', line 29

def nota_option?
  @nota_option ||= min_selections.zero?
end

#results_totalObject


37
38
39
# File 'decidim-elections/app/models/decidim/elections/question.rb', line 37

def results_total
  @results_total ||= answers.sum(&:results_total) + blank_votes
end

#slugObject


52
53
54
# File 'decidim-elections/app/models/decidim/elections/question.rb', line 52

def slug
  "question-#{id}"
end

#valid_max_selection?Boolean

Public: Checks if enough answers are given for max_selections attribute

Returns a boolean.

Returns:

  • (Boolean)

22
23
24
# File 'decidim-elections/app/models/decidim/elections/question.rb', line 22

def valid_max_selection?
  max_selections <= answers.count
end