Class: Decidim::Elections::Election

Inherits:
ApplicationRecord show all
Includes:
Forms::HasQuestionnaire, HasAttachmentCollections, HasAttachments, HasComponent, Loggable, Publicable, Resourceable, Traceable, TranslatableResource
Defined in:
decidim-elections/app/models/decidim/elections/election.rb

Overview

The data store for an Election in the Decidim::Elections component. It stores a title, description and any other useful information to perform an election.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Publicable

#publish!, #published?, #unpublish!

Methods included from HasAttachments

#attachment_context

Class Method Details

.log_presenter_class_for(_log) ⇒ Object


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

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

Instance Method Details

#allow_resource_permissions?Boolean

Public: Overrides the Resourceable concern method to allow setting permissions at resource level

Returns:

  • (Boolean)

138
139
140
# File 'decidim-elections/app/models/decidim/elections/election.rb', line 138

def allow_resource_permissions?
  true
end

#blocked?Boolean

Public: Checks if the election has a blocked_at value

Returns a boolean.

Returns:

  • (Boolean)

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

def blocked?
  blocked_at.present?
end

#finished?Boolean

Public: Checks if the election finished

Returns a boolean.

Returns:

  • (Boolean)

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

def finished?
  end_time < Time.current
end

#maximum_hours_before_start?Boolean

Public: Checks if the election start_time is maximum some hours before than the present time

Returns a boolean.

Returns:

  • (Boolean)

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

def maximum_hours_before_start?
  start_time < (Time.zone.at(Decidim::Elections.start_vote_maximum_hours_before_start.hours.from_now))
end

#minimum_answers?Boolean

Public: Checks if the number of answers are minimum 2 for each question

Returns a boolean.

Returns:

  • (Boolean)

88
89
90
# File 'decidim-elections/app/models/decidim/elections/election.rb', line 88

def minimum_answers?
  questions.all? { |question| question.answers.size > 1 }
end

#minimum_hours_before_start?Boolean

Public: Checks if the election start_time is minimum some hours later than the present time

Returns a boolean.

Returns:

  • (Boolean)

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

def minimum_hours_before_start?
  start_time > (Time.zone.at(Decidim::Elections.setup_minimum_hours_before_start.hours.from_now))
end

#ongoing?Boolean

Public: Checks if the election ongoing now

Returns a boolean.

Returns:

  • (Boolean)

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

def ongoing?
  started? && !finished?
end

#results?Boolean

Public: Checks if the election results are present

Returns a boolean.

Returns:

  • (Boolean)

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

def results?
  bb_tally_ended? || results_published?
end

#results_published?Boolean

Public: Checks if the election results are published and election finished

Returns a boolean.

Returns:

  • (Boolean)

95
96
97
# File 'decidim-elections/app/models/decidim/elections/election.rb', line 95

def results_published?
  bb_results_published?
end

#started?Boolean

Public: Checks if the election started

Returns a boolean.

Returns:

  • (Boolean)

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

def started?
  start_time <= Time.current
end

#trustee_action_required?Boolean

Returns:

  • (Boolean)

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

def trustee_action_required?
  bb_key_ceremony? || bb_tally?
end

#valid_questions?Boolean

Public: Checks if the election questions are valid

Returns a boolean.

Returns:

  • (Boolean)

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

def valid_questions?
  questions.all?(&:valid_max_selection?)
end

#voting_period_statusObject

Public: Gets the voting period status of the election

Returns one of these symbols: upcoming, ongoing or finished


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

def voting_period_status
  if finished?
    :finished
  elsif started?
    :ongoing
  else
    :upcoming
  end
end