2018-05-07 16:47:29 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2017-09-05 22:23:34 +03:00
|
|
|
class Registration < ApplicationRecord
|
2018-01-04 18:45:39 +05:30
|
|
|
require 'csv'
|
2014-06-23 19:07:50 +03:00
|
|
|
belongs_to :user
|
2013-01-07 09:04:09 +01:00
|
|
|
belongs_to :conference
|
2013-02-02 16:43:47 +01:00
|
|
|
|
2014-02-09 18:05:57 +02:00
|
|
|
has_and_belongs_to_many :qanswers
|
2014-01-01 11:39:59 +02:00
|
|
|
has_and_belongs_to_many :vchoices
|
2013-01-07 09:04:09 +01:00
|
|
|
|
2014-08-28 15:21:05 +02:00
|
|
|
has_many :events_registrations
|
2016-05-24 23:54:46 +05:30
|
|
|
has_many :events, through: :events_registrations, dependent: :destroy
|
|
|
|
|
|
2017-03-28 21:47:17 +02:00
|
|
|
has_paper_trail ignore: %i(updated_at week), meta: { conference_id: :conference_id }
|
2014-08-28 15:21:05 +02:00
|
|
|
|
2014-06-23 19:07:50 +03:00
|
|
|
accepts_nested_attributes_for :user
|
2014-02-09 18:05:57 +02:00
|
|
|
accepts_nested_attributes_for :qanswers
|
2013-07-16 09:47:23 +02:00
|
|
|
|
2014-06-24 12:14:53 +03:00
|
|
|
delegate :name, to: :user
|
|
|
|
|
delegate :email, to: :user
|
|
|
|
|
delegate :nickname, to: :user
|
|
|
|
|
delegate :affiliation, to: :user
|
2015-04-22 18:55:51 +03:00
|
|
|
delegate :username, to: :user
|
2014-06-24 12:14:53 +03:00
|
|
|
|
2013-07-12 10:45:40 +03:00
|
|
|
alias_attribute :other_needs, :other_special_needs
|
2014-06-02 12:37:57 +02:00
|
|
|
|
2015-04-18 21:44:40 +03:00
|
|
|
validates :user, presence: true
|
|
|
|
|
|
2017-03-28 21:47:17 +02:00
|
|
|
validates :user_id, uniqueness: { scope: :conference_id, message: 'already Registered!' }
|
2016-02-02 00:16:43 +01:00
|
|
|
validate :registration_limit_not_exceed, on: :create
|
2018-03-16 16:23:01 -07:00
|
|
|
validates :accepted_code_of_conduct, acceptance: {
|
2019-03-27 10:19:01 +01:00
|
|
|
if: -> { conference.try(:code_of_conduct).present? }
|
2018-03-16 16:23:01 -07:00
|
|
|
}
|
|
|
|
|
|
2014-08-28 15:21:05 +02:00
|
|
|
after_create :set_week, :subscribe_to_conference, :send_registration_mail
|
2014-06-25 14:58:33 +02:00
|
|
|
|
2016-04-22 18:18:46 +03:00
|
|
|
##
|
|
|
|
|
# Makes a list of events that includes (in that order):
|
|
|
|
|
# Events that require registration, and registration to them is still possible
|
|
|
|
|
# Events to which the user is already registered to
|
|
|
|
|
# ==== RETURNS
|
|
|
|
|
# * +Array+ -> [event_to_register_to, event_already_registered_to]
|
|
|
|
|
def events_ordered
|
|
|
|
|
(conference.program.events.with_registration_open - events) + events
|
|
|
|
|
end
|
|
|
|
|
|
2014-06-25 14:58:33 +02:00
|
|
|
private
|
|
|
|
|
|
2014-08-28 15:21:05 +02:00
|
|
|
def subscribe_to_conference
|
|
|
|
|
Subscription.create(conference_id: conference.id, user_id: user.id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def send_registration_mail
|
|
|
|
|
if conference.email_settings.send_on_registration?
|
2016-04-20 18:59:24 +02:00
|
|
|
Mailbot.registration_mail(conference, user).deliver_later
|
2014-08-28 15:21:05 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-06-25 14:58:33 +02:00
|
|
|
def set_week
|
2018-09-03 21:28:14 +02:00
|
|
|
update!(week: created_at.strftime('%W'))
|
2014-06-25 14:58:33 +02:00
|
|
|
end
|
2016-02-02 00:16:43 +01:00
|
|
|
|
|
|
|
|
def registration_limit_not_exceed
|
2019-05-03 17:14:14 +02:00
|
|
|
if conference.registration_limit > 0 && conference.registrations.count >= conference.registration_limit
|
2016-02-02 00:16:43 +01:00
|
|
|
errors.add(:base, 'Registration limit exceeded')
|
|
|
|
|
end
|
|
|
|
|
end
|
2013-07-15 16:07:23 +02:00
|
|
|
end
|