osem-fcy/app/models/registration.rb
AEtherC0r3 efaf07178f Upgrade to Rails 5
Update config with rails app:update
Update schema.rb rails db:migrate
Add puma
Make jobs and models inherit from ApplicationJob and ApplicationRecord
Update acts_as_list to 0.9.7 in order to fix
"undefined method `sanitize_sql_hash_for_conditions'" error
Update web-console to 2.3.0 to fix a 500 internal server error
Replace before_filter with before_action
Add rails-controller-testing gem
Add prepend: :true to protect_from_forgery in ApplicationController to
avoid ActionController::InvalidAuthenticityToken exceptions
Remove activeuuid
Update formtastic to 3.1.5 to fix deprecation warnings and issues
with the Input class
Update ahoy_matey to 1.6.0
Update cancancan to 2.0.0 to fix issues with malformed sql queries
Fix program spec
Fix issue with the picture being nil in admin/Organizations#new and #edit
and Organizations#show
Fix ActiveRecord::Base.raise_in_transactional_callbacks= deprecation
warning by removing an unnecessary line in application.rb
Fix failing versions specs
2017-12-11 20:58:04 +02:00

83 lines
2.7 KiB
Ruby

class Registration < ApplicationRecord
belongs_to :user
belongs_to :conference
has_and_belongs_to_many :qanswers
has_and_belongs_to_many :vchoices
has_many :events_registrations
has_many :events, through: :events_registrations, dependent: :destroy
has_paper_trail ignore: %i(updated_at week), meta: { conference_id: :conference_id }
accepts_nested_attributes_for :user
accepts_nested_attributes_for :qanswers
delegate :name, to: :user
delegate :email, to: :user
delegate :nickname, to: :user
delegate :affiliation, to: :user
delegate :username, to: :user
alias_attribute :other_needs, :other_special_needs
validates :user, presence: true
validates :user_id, uniqueness: { scope: :conference_id, message: 'already Registered!' }
validate :registration_limit_not_exceed, on: :create
validate :registration_to_events_only_if_present
after_create :set_week, :subscribe_to_conference, :send_registration_mail
##
# 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
def week
created_at.strftime('%W').to_i
end
private
##
# If the user registers to attend events that are already scheduled,
# only allow registration to events if the user will be present
# (based on arrival and departure attributes)
# No validation if arrival/departure attributes are empty
def registration_to_events_only_if_present
if (arrival || departure) && events.pluck(:start_time).any?
errors.add(:arrival, 'is too late! You cannot register for events that take place before your arrival') if events.pluck(:start_time).compact.map { |x| x < arrival }.any?
errors.add(:departure, 'is too early! You cannot register for events that take place after your departure') if events.pluck(:start_time).compact.map { |x| x > departure }.any?
end
end
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?
Mailbot.registration_mail(conference, user).deliver_later
end
end
def set_week
self.week = created_at.strftime('%W')
without_versioning do
save!
end
end
def registration_limit_not_exceed
if conference.registration_limit > 0 && conference.registrations(:reload).count >= conference.registration_limit
errors.add(:base, 'Registration limit exceeded')
end
end
end