Enable blind voting

This commit is contained in:
Stella Rouzi 2016-07-05 16:44:53 +03:00 committed by differentreality
parent 8388385b51
commit 59eeb7edbd
17 changed files with 391 additions and 108 deletions

View file

@ -82,8 +82,24 @@ class Event < ActiveRecord::Base
registrations.count < max_attendees
end
def voted?(event, user)
event.votes.where('user_id = ?', user).first
##
# Finds the rating of the user for the event
# ====Returns
# * +integer+ -> the rating of the user for the event
def user_rating(user)
(vote = votes.find_by(user: user)) ? vote.rating : 0
end
##
# Checks if the event has votes
# If a user is provided, it checks if the event has votes by the user
# ====Returns
# * +true+ -> If the event has votes (optionally, by the user)
# * +false+ -> If the event does not have any votes (optionally, by the user)
def voted?(user=nil)
return votes.where(user: user).any? if user
votes.any?
end
def average_rating

View file

@ -47,12 +47,58 @@ class Program < ActiveRecord::Base
accepts_nested_attributes_for :difficulty_levels, allow_destroy: true
# validates :conference_id, presence: true, uniqueness: true
validates :rating, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 10 }
validates :rating, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 10, only_integer: true }
validate :voting_start_date_before_end_date
validate :voting_dates_exist
before_create :create_event_types
before_create :create_difficulty_levels
validate :check_languages_format
##
# Checks if blind_voting is enabled and if voting period is over
# ====Returns
# * +true+ -> If we can show voting details
# * +false+ -> If we cannot show voting details
def show_voting?
return true unless blind_voting
Date.today > voting_end_date
end
##
# Checks if we are still in voting period
# ====Returns
# * +true+ -> If the voting period is not over yet
# * +false+ -> If the voting period is over
def voting_period?
return false unless voting_start_date && voting_end_date
(voting_start_date.to_datetime..voting_end_date.to_datetime).cover? Time.current
end
##
# Checks if both voting_start_date and voting_end_date are set
# ====Returns
# Errors when the condition is not true
def voting_dates_exist
errors.add(:voting_start_date, 'must be set, when blind voting is enabled') if blind_voting && !voting_start_date && !voting_end_date
errors.add(:voting_end_date, 'must be set, when blind voting is enabled') if blind_voting && !voting_start_date && !voting_end_date
errors.add(:voting_end_date, 'must be set, when voting_start_date is set') if voting_start_date && !voting_end_date
errors.add(:voting_start_date, 'must be set, when voting_end_date is set') if voting_end_date && !voting_start_date
end
##
# Checks if voting_start_date is before voting_end_date
# ====Returns
# Errors when the condition is not true
def voting_start_date_before_end_date
errors.add(:voting_start_date, 'must be before voting end date') if voting_start_date && voting_end_date && voting_start_date > voting_end_date
end
##
# Checcks if the program has rating enabled
#