Class: Decidim::Elections::Election

Inherits:
ApplicationRecord show all
Includes:
ActionView::Helpers::NumberHelper, FilterableResource, HasAttachments, HasComponent, Loggable, Publicable, Reportable, Resourceable, Searchable, SoftDeletable, Traceable, TranslatableAttributes, TranslatableResource
Defined in:
app/models/decidim/elections/election.rb

Overview

The data store for a document in the Decidim::Elections component. It stores a title, description and any other useful information to render a custom document.

Constant Summary collapse

RESULTS_AVAILABILITY_OPTIONS =
%w(real_time per_question after_end).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.log_presenter_class_for(_log) ⇒ Object



55
56
57
# File 'app/models/decidim/elections/election.rb', line 55

def self.log_presenter_class_for(_log)
  Decidim::Elections::AdminLog::ElectionPresenter
end

.ransackable_associations(_auth_object = nil) ⇒ Object



171
172
173
# File 'app/models/decidim/elections/election.rb', line 171

def self.ransackable_associations(_auth_object = nil)
  %w(questions response_options)
end

.ransackable_attributes(_auth_object = nil) ⇒ Object



175
176
177
# File 'app/models/decidim/elections/election.rb', line 175

def self.ransackable_attributes(_auth_object = nil)
  %w(search_text title description)
end

.ransackable_scopes(_auth_object = nil) ⇒ Object



167
168
169
# File 'app/models/decidim/elections/election.rb', line 167

def self.ransackable_scopes(_auth_object = nil)
  [:with_any_state]
end

Instance Method Details

#auto_start?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/decidim/elections/election.rb', line 63

def auto_start?
  start_at.present?
end

#available_questionsObject

if per question, only the enabled questions are returned if not, all questions are returned if per question, only the enabled questions are returned if not, all questions are returned



132
133
134
135
136
# File 'app/models/decidim/elections/election.rb', line 132

def available_questions
  return questions.enabled.unpublished_results if per_question?

  questions
end

#censusObject



117
118
119
# File 'app/models/decidim/elections/election.rb', line 117

def census
  @census ||= Decidim::Elections.census_registry.find(census_manifest)
end

#census_ready?Boolean

syntax sugar to access the census manifest

Returns:

  • (Boolean)


122
123
124
125
126
# File 'app/models/decidim/elections/election.rb', line 122

def census_ready?
  return false if census.nil?

  census.ready?(self)
end

#editable?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'app/models/decidim/elections/election.rb', line 75

def editable?
  published? ? !started? : !votes.exists?
end

#finished?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/decidim/elections/election.rb', line 87

def finished?
  # If end_at is present and in the past, the election is finished no matter what type of voting
  @finished ||= if end_at.present? && end_at <= Time.current
                  true
                elsif per_question?
                  # Per question elections are considered finished if all questions have published results
                  questions.all?(&:published_results?)
                else
                  false
                end
end

#manual_start?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/models/decidim/elections/election.rb', line 67

def manual_start?
  !auto_start?
end

#ongoing?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/models/decidim/elections/election.rb', line 83

def ongoing?
  started? && !finished?
end

#presenterObject



51
52
53
# File 'app/models/decidim/elections/election.rb', line 51

def presenter
  Decidim::Elections::ElectionPresenter.new(self)
end

#published_results?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'app/models/decidim/elections/election.rb', line 99

def published_results?
  results_at.present?
end

#result_published_questionsObject

Returns the questions that are available for results.



154
155
156
157
158
159
# File 'app/models/decidim/elections/election.rb', line 154

def result_published_questions
  return questions.published_results if per_question?
  return available_questions if published_results?

  []
end

#results_atObject

Date of results publication vary depending on the results_availability If results_availability is “per_question”, the results are published when the first question has its results published. If results_availability is “real_time”, the results are published as soon as the election has started. If “after_end”, publication is manual



108
109
110
111
112
113
114
115
# File 'app/models/decidim/elections/election.rb', line 108

def results_at
  return nil unless published?
  return nil unless started?
  return questions.published_results.first&.published_results_at if per_question?
  return start_at if real_time?

  published_results_at
end

#scheduled?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/decidim/elections/election.rb', line 71

def scheduled?
  published? && !ongoing? && !finished? && !published_results?
end

#started?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/models/decidim/elections/election.rb', line 79

def started?
  start_at.present? && start_at <= Time.current
end

#statusObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/decidim/elections/election.rb', line 138

def status
  return @status if defined?(@status)

  @status =
    if !published?
      :unpublished
    elsif ongoing?
      :ongoing
    elsif finished?
      :finished
    else
      :scheduled
    end
end

#update_votes_count!Object



59
60
61
# File 'app/models/decidim/elections/election.rb', line 59

def update_votes_count!
  update(votes_count: votes.count)
end