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



57
58
59
# File 'app/models/decidim/elections/election.rb', line 57

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

.ransackable_associations(_auth_object = nil) ⇒ Object



173
174
175
# File 'app/models/decidim/elections/election.rb', line 173

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

.ransackable_attributes(_auth_object = nil) ⇒ Object



177
178
179
# File 'app/models/decidim/elections/election.rb', line 177

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

.ransackable_scopes(_auth_object = nil) ⇒ Object



169
170
171
# File 'app/models/decidim/elections/election.rb', line 169

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

Instance Method Details

#auto_start?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/models/decidim/elections/election.rb', line 65

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



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

def available_questions
  return questions.enabled.unpublished_results if per_question?

  questions
end

#censusObject



119
120
121
# File 'app/models/decidim/elections/election.rb', line 119

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

#census_ready?Boolean

syntax sugar to access the census manifest

Returns:

  • (Boolean)


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

def census_ready?
  return false if census.nil?

  census.ready?(self)
end

#editable?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'app/models/decidim/elections/election.rb', line 77

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

#finished?Boolean

Returns:

  • (Boolean)


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

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)


69
70
71
# File 'app/models/decidim/elections/election.rb', line 69

def manual_start?
  !auto_start?
end

#ongoing?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'app/models/decidim/elections/election.rb', line 85

def ongoing?
  started? && !finished?
end

#presenterObject



53
54
55
# File 'app/models/decidim/elections/election.rb', line 53

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

#published_results?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'app/models/decidim/elections/election.rb', line 101

def published_results?
  results_at.present?
end

#result_published_questionsObject

Returns the questions that are available for results.



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

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



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

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)


73
74
75
# File 'app/models/decidim/elections/election.rb', line 73

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

#started?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'app/models/decidim/elections/election.rb', line 81

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

#statusObject



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

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



61
62
63
# File 'app/models/decidim/elections/election.rb', line 61

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