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:
decidim-elections/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

Methods included from Searchable

searchable_resources, searchable_resources_by_type, searchable_resources_of_type_comment, searchable_resources_of_type_component, searchable_resources_of_type_participant, searchable_resources_of_type_participatory_space

Methods included from TranslatableAttributes

#attachment?, #default_locale?

Methods included from Publicable

#previously_published?, #publish!, #published?, #unpublish!

Methods included from HasAttachments

#attachment_context

Class Method Details

.log_presenter_class_for(_log) ⇒ Object



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

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

.ransackable_associations(_auth_object = nil) ⇒ Object



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

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

.ransackable_attributes(_auth_object = nil) ⇒ Object



160
161
162
# File 'decidim-elections/app/models/decidim/elections/election.rb', line 160

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

.ransackable_scopes(_auth_object = nil) ⇒ Object



152
153
154
# File 'decidim-elections/app/models/decidim/elections/election.rb', line 152

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

Instance Method Details

#auto_start?Boolean

Returns:

  • (Boolean)


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

def auto_start?
  start_at.present?
end

#available_questionsObject

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



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

def available_questions
  return questions.enabled.unpublished_results if per_question?

  questions
end

#censusObject



104
105
106
# File 'decidim-elections/app/models/decidim/elections/election.rb', line 104

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

#census_ready?Boolean

syntax sugar to access the census manifest

Returns:

  • (Boolean)


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

def census_ready?
  return false if census.nil?

  census.ready?(self)
end

#finished?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
# File 'decidim-elections/app/models/decidim/elections/election.rb', line 74

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)


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

def manual_start?
  !auto_start?
end

#ongoing?Boolean

Returns:

  • (Boolean)


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

def ongoing?
  started? && !finished?
end

#presenterObject



46
47
48
# File 'decidim-elections/app/models/decidim/elections/election.rb', line 46

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

#published_results?Boolean

Returns:

  • (Boolean)


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

def published_results?
  results_at.present?
end

#result_published_questionsObject

Returns the questions that are available for results.



139
140
141
142
143
144
# File 'decidim-elections/app/models/decidim/elections/election.rb', line 139

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



95
96
97
98
99
100
101
102
# File 'decidim-elections/app/models/decidim/elections/election.rb', line 95

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)


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

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

#started?Boolean

Returns:

  • (Boolean)


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

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

#statusObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'decidim-elections/app/models/decidim/elections/election.rb', line 123

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

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