Registration for Events

This commit is contained in:
Stella Rouzi 2016-04-22 18:18:46 +03:00
parent f188fd5575
commit 01ec43e53a
35 changed files with 522 additions and 46 deletions

View file

@ -14,7 +14,8 @@ class Event < ActiveRecord::Base
has_many :commercials, as: :commercialable, dependent: :destroy
belongs_to :event_type
has_and_belongs_to_many :registrations
has_many :events_registrations
has_many :registrations, through: :events_registrations
belongs_to :track
belongs_to :room
@ -32,6 +33,10 @@ class Event < ActiveRecord::Base
validates :abstract, presence: true
validates :event_type, presence: true
validates :program, presence: true
validates :max_attendees, numericality: { only_integer: true, greater_than_or_equal_to: 1, allow_nil: true }
validate :max_attendees_and_require_registration
validate :max_attendees_no_more_than_room_size
scope :confirmed, -> { where(state: 'confirmed') }
scope :highlighted, -> { where(is_highlight: true) }
@ -64,6 +69,19 @@ class Event < ActiveRecord::Base
end
end
##
# Checkes if the event has a start_time and a room
# ====Returns
# * +true+ or +false+
def scheduled?
room && start_time ? true : false
end
def registration_possible?
return false unless max_attendees
registrations.count < max_attendees
end
def voted?(event, user)
event.votes.where('user_id = ?', user).first
end
@ -207,6 +225,21 @@ class Event < ActiveRecord::Base
private
##
# If max_attendees variable is set (higher than 0)
# variable require_registration must also be set
def max_attendees_and_require_registration
errors.add(:require_registration, 'must be enabled, when you set max_attendees') if max_attendees && !require_registration
errors.add(:max_attendees, 'must be enabled, when you set require_registration') if require_registration && max_attendees.nil?
end
##
# Do not allow, for the event, more attendees than the size of the room
def max_attendees_no_more_than_room_size
return unless room && max_attendees_changed?
errors.add(:max_attendees, "cannot be more than the room's capacity (#{room.size})") if max_attendees && (max_attendees > room.size)
end
def abstract_limit
# If we don't have an event type, there is no need to count anything
return unless event_type && abstract

View file

@ -1,4 +1,12 @@
class EventsRegistration < ActiveRecord::Base
belongs_to :registration
belongs_to :event
has_one :user, through: :registration
delegate :name, to: :registration
delegate :email, to: :registration
validates :event, :registration, presence: true
validates :event, uniqueness: { scope: :registration }
end

View file

@ -8,10 +8,21 @@ class Program < ActiveRecord::Base
has_many :tracks, dependent: :destroy
has_many :difficulty_levels, dependent: :destroy
has_many :events, dependent: :destroy do
def workshops
def require_registration
where(require_registration: true, state: :confirmed)
end
def with_registration_open
where(require_registration: true, state: :confirmed).
map { |e| e if e.max_attendees > e.registrations.count }.compact
end
# All confirmed events of the conference with attribute require_registration
# excluding the events the user has already registered to
def remaining_for_registration(registration)
require_registration - registration.events
end
def confirmed
where(state: :confirmed)
end

View file

@ -7,7 +7,7 @@ class Registration < ActiveRecord::Base
has_and_belongs_to_many :vchoices
has_many :events_registrations
has_many :workshops, through: :events_registrations, source: :event
has_many :events, through: :events_registrations
accepts_nested_attributes_for :user
accepts_nested_attributes_for :qanswers
@ -24,16 +24,40 @@ class Registration < ActiveRecord::Base
validates_uniqueness_of :user_id, 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
after_destroy :destroy_purchased_tickets
##
# 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 destroy_purchased_tickets
ticket_purchased = TicketPurchase.where(conference_id: conference_id, user_id: user.id)
ticket_purchased.destroy_all

View file

@ -36,6 +36,7 @@ class User < ActiveRecord::Base
has_many :event_users, dependent: :destroy
has_many :events, -> { uniq }, through: :event_users
has_many :registrations, dependent: :destroy
has_many :events_registrations, through: :registrations
has_many :ticket_purchases, dependent: :destroy
has_many :tickets, through: :ticket_purchases, source: :ticket
has_many :votes, dependent: :destroy
@ -53,10 +54,33 @@ class User < ActiveRecord::Base
},
presence: true
##
# Checkes if the user attended the event
# This is used for events that require registration
# The user must have registered to attend the event
# Gets an event
# === Returns
# * +true+ if the user attended the event
# * +false+ if the user did not attend the event
def attended_event? event
event_registration = event.events_registrations.find_by(registration: self.registrations)
return false unless event_registration.present?
event_registration.attended
end
def name
self[:name] || username
end
##
# Checks if a user has registered to an event
# ====Returns
# * +true+ or +false+
def registered_to_event? event
event.registrations.pluck(:id).include? self.registrations.find_by(conference_id: event.program.conference.id).id
end
def subscribed? conference
self.subscriptions.find_by(conference_id: conference.id).present?
end