2014-08-10 21:17:06 +03:00
|
|
|
class Event < ActiveRecord::Base
|
2015-04-20 18:37:05 +03:00
|
|
|
include Proposable
|
|
|
|
|
2015-08-14 23:33:08 +03:00
|
|
|
belongs_to :conference
|
2015-08-14 23:40:32 +03:00
|
|
|
belongs_to :track
|
2015-10-08 02:34:19 +03:00
|
|
|
has_one :slot
|
2015-07-29 17:54:41 +03:00
|
|
|
|
2015-09-03 23:16:39 +03:00
|
|
|
has_many :participations, dependent: :destroy
|
2015-07-29 17:54:41 +03:00
|
|
|
has_many :pending_participations, ->() { pending }, class_name: 'Participation'
|
|
|
|
has_many :approved_participations, ->() { approved }, class_name: 'Participation'
|
|
|
|
has_many :participants, through: :approved_participations
|
2016-10-15 05:59:07 +03:00
|
|
|
has_many :participants_with_personal_profiles, through: :approved_participations, source: :participant_with_personal_profile
|
2016-10-13 08:59:22 +03:00
|
|
|
has_many :conflict_counts, -> { order(number_of_conflicts: :desc) }, foreign_key: :left_id
|
2015-07-29 17:54:41 +03:00
|
|
|
|
2015-07-19 20:01:52 +03:00
|
|
|
belongs_to :event_type
|
2015-07-19 14:32:25 +03:00
|
|
|
|
2016-10-11 05:16:31 +03:00
|
|
|
scope :ranked, -> { where.not(ranked: nil).where.not(votes: nil) }
|
|
|
|
|
2015-08-15 00:43:30 +03:00
|
|
|
validates :conference, presence: true
|
2014-08-10 21:17:06 +03:00
|
|
|
validates :title, presence: true
|
|
|
|
validates :abstract, presence: true
|
|
|
|
validates :description, presence: true
|
2014-09-04 00:39:54 +03:00
|
|
|
validates :agreement, acceptance: true
|
2015-08-15 00:43:30 +03:00
|
|
|
validates :track, presence: true
|
|
|
|
validates :language, inclusion: {in: I18n.available_locales.map(&:to_s)}, presence: true
|
2015-08-20 00:24:22 +03:00
|
|
|
validates :event_type, presence: true
|
|
|
|
validates :length, presence: true, numericality: {only_integer: true}
|
|
|
|
validate :length_is_within_the_permitted_interval
|
2016-10-15 21:33:02 +03:00
|
|
|
validate :track_belongs_to_the_selected_conference
|
|
|
|
validate :event_type_belongs_to_the_selected_conference
|
2015-04-20 18:37:14 +03:00
|
|
|
|
2015-10-15 19:06:41 +03:00
|
|
|
delegate :status, to: :proposition
|
|
|
|
|
2016-10-09 02:13:36 +03:00
|
|
|
accepts_nested_attributes_for :participations, allow_destroy: true
|
|
|
|
|
2016-10-08 22:40:56 +03:00
|
|
|
def all_participants_have_profiles?
|
2016-10-15 05:59:07 +03:00
|
|
|
participants_with_personal_profiles.all? { |participant| participant.has_personal_profile? }
|
2016-10-08 22:40:56 +03:00
|
|
|
end
|
|
|
|
|
2015-07-26 15:58:08 +03:00
|
|
|
def proposer_profile
|
|
|
|
proposer.personal_profile(conference)
|
|
|
|
end
|
2015-08-15 00:43:30 +03:00
|
|
|
|
2015-08-20 00:33:13 +03:00
|
|
|
def length
|
|
|
|
read_attribute(:length) || event_type.try(:minimum_length)
|
|
|
|
end
|
|
|
|
|
2015-08-20 01:42:35 +03:00
|
|
|
def proposable_type
|
|
|
|
event_type.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def proposable_description
|
|
|
|
{
|
|
|
|
proposer_email: proposer_email,
|
|
|
|
title: title,
|
|
|
|
subtitle: subtitle,
|
|
|
|
track: track.name,
|
|
|
|
length: "#{length} m",
|
|
|
|
language: language,
|
|
|
|
abstract: abstract,
|
|
|
|
description: description,
|
|
|
|
notes: notes
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-10-11 05:16:31 +03:00
|
|
|
def ranked?
|
|
|
|
conference.has_vote_results? and rank.present? and number_of_votes.present?
|
|
|
|
end
|
|
|
|
|
2016-10-11 01:52:19 +03:00
|
|
|
def per_cent_of_votes
|
|
|
|
if conference.has_vote_results? and conference.number_of_ballots_cast > 0
|
|
|
|
Rational(number_of_votes * 100, conference.number_of_ballots_cast)
|
|
|
|
else
|
|
|
|
Float::NAN
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-15 00:43:30 +03:00
|
|
|
private
|
|
|
|
|
2016-10-15 21:33:02 +03:00
|
|
|
def event_type_belongs_to_the_selected_conference
|
|
|
|
unless conference.present? and conference.event_types.include?(event_type)
|
|
|
|
errors.add :event_type, :must_be_a_valid_event_type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-15 00:43:30 +03:00
|
|
|
def track_belongs_to_the_selected_conference
|
|
|
|
unless conference.present? and conference.tracks.include?(track)
|
|
|
|
errors.add :track, :must_be_a_valid_track
|
|
|
|
end
|
|
|
|
end
|
2015-08-20 00:24:22 +03:00
|
|
|
|
|
|
|
def length_is_within_the_permitted_interval
|
2016-10-15 21:33:02 +03:00
|
|
|
if event_type.present?
|
|
|
|
unless length >= event_type.minimum_length and length <= event_type.maximum_length
|
|
|
|
errors.add :length, :must_be_between, minimum: event_type.minimum_length, maximum: event_type.maximum_length
|
|
|
|
end
|
2015-08-20 00:24:22 +03:00
|
|
|
end
|
|
|
|
end
|
2014-08-10 21:17:06 +03:00
|
|
|
end
|