New associations and models: schedule versioning

For the schedule versioning in the admin interface (having multiple possible shcedules) we need to change some models and associations. It has also been create a rake:move_events_attributes task to migrate the data.

User Interface addapted to make use of the introduced associations and models
This commit is contained in:
Ana 2016-07-08 13:28:02 +02:00
parent e34a1904e0
commit c047ffd699
21 changed files with 165 additions and 49 deletions

View file

@ -16,9 +16,9 @@ class Event < ActiveRecord::Base
has_many :events_registrations
has_many :registrations, through: :events_registrations
has_many :event_schedules, dependent: :destroy
belongs_to :track
belongs_to :room
belongs_to :difficulty_level
belongs_to :program
@ -71,11 +71,19 @@ class Event < ActiveRecord::Base
end
##
# Checkes if the event has a start_time and a room
# Checkes if the event has a start_time and a room for the selected schedule if there is any
# ====Returns
# * +true+ or +false+
def scheduled?
room.present? && start_time.present?
selected_event_schedule.try(:start_time).present? && selected_event_schedule.try(:room).present?
end
##
# Checkes if the event has a start_time and a room for the selected one if there is any.
# ====Returns
# * +true+ or +false+
def unscheduled?
state == 'confirmed' && (!selected_event_schedule.try(:start_time).present? || !selected_event_schedule.try(:room).present?)
end
def registration_possible?
@ -132,7 +140,7 @@ class Event < ActiveRecord::Base
def as_json(options)
json = super(options)
json[:room_guid] = room.try(:guid)
json[:room_guid] = scheduled_room.try(:guid)
json[:track_color] = track.try(:color) || '#FFFFFF'
json[:length] = event_type.try(:length) || EventType::LENGTH_STEP
@ -245,7 +253,21 @@ class Event < ActiveRecord::Base
# Returns end of the event
#
def end_time
self.start_time + self.event_type.length.minutes
self.scheduled_start_time + self.event_type.length.minutes
end
##
# Returns the room in which the event is scheduled
#
def scheduled_room
selected_event_schedule.try(:room)
end
##
# Returns the start time at which this event is scheduled
#
def scheduled_start_time
selected_event_schedule.try(:start_time)
end
##
@ -257,11 +279,16 @@ class Event < ActiveRecord::Base
private
# returns the event_schedule for this event and for the selected_schedule
def selected_event_schedule
event_schedules.find_by(schedule_id: program.try(:selected_schedule))
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)
return unless scheduled_room && max_attendees_changed?
errors.add(:max_attendees, "cannot be more than the room's capacity (#{scheduled_room.size})") if max_attendees && (max_attendees > scheduled_room.size)
end
def abstract_limit

View file

@ -0,0 +1,5 @@
class EventSchedule < ActiveRecord::Base
belongs_to :schedule
belongs_to :event
belongs_to :room
end

View file

@ -7,6 +7,7 @@ class Program < ActiveRecord::Base
has_many :event_types, dependent: :destroy
has_many :tracks, dependent: :destroy
has_many :difficulty_levels, dependent: :destroy
has_many :schedules, dependent: :destroy
has_many :events, dependent: :destroy do
def require_registration
where(require_registration: true, state: :confirmed)
@ -26,12 +27,12 @@ class Program < ActiveRecord::Base
where(state: :confirmed)
end
def scheduled
where.not(start_time: nil).where.not(room: nil).order(start_time: :asc)
def scheduled(schedule_id)
joins(:event_schedules).where('event_schedules.schedule_id = ? AND event_schedules.start_time IS NOT NULL AND event_schedules.room_id IS NOT NULL', schedule_id)
end
def unscheduled
confirmed.where('start_time IS NULL OR room_id IS NULL')
select(&:unscheduled?)
end
def highlights

View file

@ -1,6 +1,6 @@
class Room < ActiveRecord::Base
belongs_to :venue
has_many :events, dependent: :nullify
has_many :event_schedules, dependent: :nullify
before_create :generate_guid

4
app/models/schedule.rb Normal file
View file

@ -0,0 +1,4 @@
class Schedule < ActiveRecord::Base
belongs_to :program
has_many :event_schedules, dependent: :destroy
end