Merge branch 'master' into polls_minor_changes
This commit is contained in:
@@ -45,7 +45,7 @@ module Abilities
|
||||
|
||||
can [:read, :update, :valuate, :destroy, :summary], SpendingProposal
|
||||
|
||||
can [:index, :read, :new, :create, :update, :destroy, :calculate_winners], Budget
|
||||
can [:index, :read, :new, :create, :update, :destroy, :calculate_winners, :read_results], Budget
|
||||
can [:read, :create, :update, :destroy], Budget::Group
|
||||
can [:read, :create, :update, :destroy], Budget::Heading
|
||||
can [:hide, :update, :toggle_selection], Budget::Investment
|
||||
|
||||
@@ -59,6 +59,10 @@ class Poll < ActiveRecord::Base
|
||||
voters.where(document_number: document_number, document_type: document_type).exists?
|
||||
end
|
||||
|
||||
def voted_in_booth?(user)
|
||||
Poll::Voter.where(poll: self, user: user, origin: "booth").exists?
|
||||
end
|
||||
|
||||
def date_range
|
||||
unless starts_at.present? && ends_at.present? && starts_at <= ends_at
|
||||
errors.add(:starts_at, I18n.t('errors.messages.invalid_date_range'))
|
||||
|
||||
@@ -8,12 +8,13 @@ class Poll::Answer < ActiveRecord::Base
|
||||
validates :question, presence: true
|
||||
validates :author, presence: true
|
||||
validates :answer, presence: true
|
||||
validates :answer, inclusion: {in: ->(a) { a.question.valid_answers }}
|
||||
validates :answer, inclusion: { in: ->(a) { a.question.valid_answers }},
|
||||
unless: ->(a) { a.question.blank? }
|
||||
|
||||
scope :by_author, ->(author_id) { where(author_id: author_id) }
|
||||
scope :by_question, ->(question_id) { where(question_id: question_id) }
|
||||
|
||||
def record_voter_participation
|
||||
Poll::Voter.create!(user: author, poll: poll)
|
||||
Poll::Voter.find_or_create_by!(user: author, poll: poll, origin: "web")
|
||||
end
|
||||
end
|
||||
@@ -8,7 +8,7 @@ class Poll
|
||||
|
||||
validates :officer_id, presence: true
|
||||
validates :booth_assignment_id, presence: true
|
||||
validates :date, presence: true, uniqueness: { scope: [:officer_id, :booth_assignment_id] }
|
||||
validates :date, presence: true
|
||||
|
||||
delegate :poll_id, :booth_id, to: :booth_assignment
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class Poll::Recount < ActiveRecord::Base
|
||||
|
||||
VALID_ORIGINS = %w{ web booth letter }
|
||||
VALID_ORIGINS = %w{web booth letter}.freeze
|
||||
|
||||
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
|
||||
belongs_to :booth_assignment
|
||||
@@ -22,7 +22,7 @@ class Poll::Recount < ActiveRecord::Base
|
||||
|
||||
[:white, :null, :total].each do |amount|
|
||||
next unless send("#{amount}_amount_changed?") && send("#{amount}_amount_was").present?
|
||||
self["#{amount}_amount_log"] += ":#{send("#{amount}_amount_was").to_s}"
|
||||
self["#{amount}_amount_log"] += ":#{send("#{amount}_amount_was")}"
|
||||
amounts_changed = true
|
||||
end
|
||||
|
||||
@@ -30,7 +30,7 @@ class Poll::Recount < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def update_officer_author
|
||||
self.officer_assignment_id_log += ":#{officer_assignment_id_was.to_s}"
|
||||
self.author_id_log += ":#{author_id_was.to_s}"
|
||||
self.officer_assignment_id_log += ":#{officer_assignment_id_was}"
|
||||
self.author_id_log += ":#{author_id_was}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,28 +5,37 @@ class Poll
|
||||
|
||||
validates :booth_id, presence: true
|
||||
validates :officer_id, presence: true
|
||||
validates :date, presence: true
|
||||
validates :date, uniqueness: { scope: [:officer_id, :booth_id] }
|
||||
validates :date, presence: true, uniqueness: { scope: [:officer_id, :booth_id, :task] }
|
||||
validates :task, presence: true
|
||||
|
||||
enum task: { vote_collection: 0, recount_scrutiny: 1 }
|
||||
|
||||
before_create :persist_data
|
||||
after_create :create_officer_assignments
|
||||
|
||||
def create_officer_assignments
|
||||
booth.booth_assignments.each do |booth_assignment|
|
||||
attrs = { officer_id: officer_id,
|
||||
date: date,
|
||||
booth_assignment_id: booth_assignment.id }
|
||||
Poll::OfficerAssignment.create!(attrs)
|
||||
end
|
||||
end
|
||||
before_destroy :destroy_officer_assignments
|
||||
|
||||
def persist_data
|
||||
self.officer_name = officer.name
|
||||
self.officer_email = officer.email
|
||||
end
|
||||
|
||||
def create_officer_assignments
|
||||
booth.booth_assignments.each do |booth_assignment|
|
||||
attrs = {
|
||||
officer_id: officer_id,
|
||||
date: date,
|
||||
booth_assignment_id: booth_assignment.id,
|
||||
final: recount_scrutiny?
|
||||
}
|
||||
Poll::OfficerAssignment.create!(attrs)
|
||||
end
|
||||
end
|
||||
|
||||
def destroy_officer_assignments
|
||||
Poll::OfficerAssignment.where(booth_assignment: booth.booth_assignments,
|
||||
officer: officer,
|
||||
date: date,
|
||||
final: recount_scrutiny?).destroy_all
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
class Poll
|
||||
class Voter < ActiveRecord::Base
|
||||
|
||||
VALID_ORIGINS = %w{ web booth }
|
||||
|
||||
belongs_to :poll
|
||||
belongs_to :user
|
||||
belongs_to :geozone
|
||||
@@ -10,9 +13,13 @@ class Poll
|
||||
validates :user_id, presence: true
|
||||
|
||||
validates :document_number, presence: true, uniqueness: { scope: [:poll_id, :document_type], message: :has_voted }
|
||||
validates :origin, inclusion: { in: VALID_ORIGINS }
|
||||
|
||||
before_validation :set_demographic_info, :set_document_info
|
||||
|
||||
scope :web, -> { where(origin: 'web') }
|
||||
scope :booth, -> { where(origin: 'booth') }
|
||||
|
||||
def set_demographic_info
|
||||
return if user.blank?
|
||||
|
||||
|
||||
Reference in New Issue
Block a user