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

@ -191,7 +191,7 @@ module Admin
# Set also in proposals controller # Set also in proposals controller
:title, :subtitle, :event_type_id, :abstract, :description, :require_registration, :difficulty_level_id, :title, :subtitle, :event_type_id, :abstract, :description, :require_registration, :difficulty_level_id,
# Set only in admin/events controller # Set only in admin/events controller
:track_id, :state, :language, :start_time, :is_highlight, :max_attendees, :track_id, :state, :language, :is_highlight, :max_attendees,
# Not used anymore? # Not used anymore?
:proposal_additional_speakers, :user, :users_attributes) :proposal_additional_speakers, :user, :users_attributes)
end end

View file

@ -5,7 +5,7 @@ module Api
respond_to :json respond_to :json
def index def index
events = Event.includes(:track, :room, :event_type, event_users: :user) events = Event.includes(:track, :event_type, event_users: :user)
if @conference if @conference
events = events.where(program: @conference.program) events = events.where(program: @conference.program)

View file

@ -16,9 +16,9 @@ class Event < ActiveRecord::Base
has_many :events_registrations has_many :events_registrations
has_many :registrations, through: :events_registrations has_many :registrations, through: :events_registrations
has_many :event_schedules, dependent: :destroy
belongs_to :track belongs_to :track
belongs_to :room
belongs_to :difficulty_level belongs_to :difficulty_level
belongs_to :program belongs_to :program
@ -71,11 +71,19 @@ class Event < ActiveRecord::Base
end 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 # ====Returns
# * +true+ or +false+ # * +true+ or +false+
def scheduled? 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 end
def registration_possible? def registration_possible?
@ -132,7 +140,7 @@ class Event < ActiveRecord::Base
def as_json(options) def as_json(options)
json = super(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[:track_color] = track.try(:color) || '#FFFFFF'
json[:length] = event_type.try(:length) || EventType::LENGTH_STEP json[:length] = event_type.try(:length) || EventType::LENGTH_STEP
@ -245,7 +253,21 @@ class Event < ActiveRecord::Base
# Returns end of the event # Returns end of the event
# #
def end_time 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 end
## ##
@ -257,11 +279,16 @@ class Event < ActiveRecord::Base
private 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 # Do not allow, for the event, more attendees than the size of the room
def max_attendees_no_more_than_room_size def max_attendees_no_more_than_room_size
return unless room && max_attendees_changed? return unless scheduled_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) errors.add(:max_attendees, "cannot be more than the room's capacity (#{scheduled_room.size})") if max_attendees && (max_attendees > scheduled_room.size)
end end
def abstract_limit 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 :event_types, dependent: :destroy
has_many :tracks, dependent: :destroy has_many :tracks, dependent: :destroy
has_many :difficulty_levels, dependent: :destroy has_many :difficulty_levels, dependent: :destroy
has_many :schedules, dependent: :destroy
has_many :events, dependent: :destroy do has_many :events, dependent: :destroy do
def require_registration def require_registration
where(require_registration: true, state: :confirmed) where(require_registration: true, state: :confirmed)
@ -26,12 +27,12 @@ class Program < ActiveRecord::Base
where(state: :confirmed) where(state: :confirmed)
end end
def scheduled def scheduled(schedule_id)
where.not(start_time: nil).where.not(room: nil).order(start_time: :asc) 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 end
def unscheduled def unscheduled
confirmed.where('start_time IS NULL OR room_id IS NULL') select(&:unscheduled?)
end end
def highlights def highlights

View file

@ -1,6 +1,6 @@
class Room < ActiveRecord::Base class Room < ActiveRecord::Base
belongs_to :venue belongs_to :venue
has_many :events, dependent: :nullify has_many :event_schedules, dependent: :nullify
before_create :generate_guid 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

View file

@ -22,20 +22,20 @@ class ConferenceSerializer < ActiveModel::Serializer
def rooms def rooms
if object.venue if object.venue
object.venue.rooms.includes(:events).map do |room| { id: room.id, object.venue.rooms.map do |room| { id: room.id,
size: room.size, size: room.size,
events: room.events.map do |event| { guid: event.title, events: room.event_schedules.map do |event_schedule| { guid: event_schedule.event.title,
title: event.title, title: event_schedule.event.title,
subtitle: event.subtitle, subtitle: event_schedule.event.subtitle,
abstract: event.abstract, abstract: event_schedule.event.abstract,
description: event.description, description: event_schedule.event.description,
is_highlight: event.is_highlight, is_highlight: event_schedule.event.is_highlight,
require_registration: event.require_registration, require_registration: event_schedule.event.require_registration,
start_time: event.start_time, start_time: event_schedule.start_time,
event_type_id: event.event_type.id, event_type_id: event_schedule.event.event_type.id,
difficulty_level_id: event.difficulty_level_id, difficulty_level_id: event_schedule.event.difficulty_level_id,
track_id: event.track_id, track_id: event_schedule.event.track_id,
speaker_names: event.speaker_names speaker_names: event_schedule.event.speaker_names
} }
end end
} }

View file

@ -0,0 +1,14 @@
class EventScheduleSerializer < ActiveModel::Serializer
include ActionView::Helpers::TextHelper
attributes :date, :room
def date
t = object.start_time
t.blank? ? '' : %( #{I18n.l t, format: :short}#{t.formatted_offset(false)} )
end
def room
object.room.try(:guid)
end
end

View file

@ -1,11 +1,11 @@
class EventSerializer < ActiveModel::Serializer class EventSerializer < ActiveModel::Serializer
include ActionView::Helpers::TextHelper include ActionView::Helpers::TextHelper
attributes :guid, :title, :length, :date, :language, :abstract, :speaker_ids, :type, :room, :track attributes :guid, :title, :length, :scheduled_date, :language, :abstract, :speaker_ids, :type, :scheduled_room, :track
def date def scheduled_date
t = object.start_time t = object.scheduled_start_time
t.blank? ? '' : %{ #{I18n.l t, format: :short}#{t.formatted_offset(false)} } t.blank? ? '' : %( #{I18n.l t, format: :short}#{t.formatted_offset(false)} )
end end
def speaker_ids def speaker_ids
@ -17,8 +17,8 @@ class EventSerializer < ActiveModel::Serializer
object.event_type.try(:title) object.event_type.try(:title)
end end
def room def scheduled_room
object.room.try(:guid) object.scheduled_room.try(:guid)
end end
def track def track

View file

@ -106,18 +106,18 @@
%td %td
= @event.language = @event.language
- unless @event.room.nil? - unless @event.scheduled_room.nil?
%tr %tr
%td %td
%b Room %b Room
%td %td
= @event.room.name = @event.scheduled_room.name
- unless @event.start_time.nil? - unless @event.scheduled_start_time.nil?
%tr %tr
%td %td
%b Scheduled time %b Scheduled time
%td %td
= @event.start_time = @event.scheduled_start_time
%tr %tr
%td %td
%b Submitter %b Submitter

View file

@ -11,7 +11,7 @@
for for
= @event.title = @event.title
- if @event.room && (@event_registrations.length > @event.room.size) - if @event.scheduled_room && (@event_registrations.length > @event.scheduled_room.size)
%b Attention: %b Attention:
You have more registrations than the capacity of the room! You have more registrations than the capacity of the room!

View file

@ -27,5 +27,4 @@
%td %td
= event.state = event.state
%td %td
= event.start_time = event.scheduled_start_time

View file

@ -19,7 +19,7 @@
%date= event.start_time.iso8601 %date= event.start_time.iso8601
%start= event.start_time.strftime('%H:%M') %start= event.start_time.strftime('%H:%M')
%duration= length_timestamp(event.event_type.length) %duration= length_timestamp(event.event_type.length)
%room= event.room.name %room= event.scheduled_room.name
%type= event.event_type.name %type= event.event_type.name
%language= ISO_639.find_by_english_name(event.language).third if event.language %language= ISO_639.find_by_english_name(event.language).third if event.language
%slug= "#{event.id} #{event.title}".parameterize %slug= "#{event.id} #{event.title}".parameterize

View file

@ -11,7 +11,7 @@
.text-muted .text-muted
= registered_text(event) = registered_text(event)
- if event.scheduled? - if event.scheduled?
(Scheduled on: #{event.start_time.to_date}) (Scheduled on: #{event.scheduled_start_time.to_date})
%br %br

View file

@ -49,7 +49,7 @@
= f.inputs 'Enable pre-registration' do = f.inputs 'Enable pre-registration' do
= f.input :require_registration, label: 'Require participants to register to your event' = f.input :require_registration, label: 'Require participants to register to your event'
- message = @event.room ? "Value must be between 1 and #{@event.room.size}" : 'Check room capacity after scheduling.' - message = @event.scheduled_room ? "Value must be between 1 and #{@event.scheduled_room.size}" : 'Check room capacity after scheduling.'
= f.input :max_attendees, hint: 'The maximum number of participants. ' + message = f.input :max_attendees, hint: 'The maximum number of participants. ' + message
- if current_user.has_any_role? :admin, { name: :organizer, resource: @conference }, { name: :cfp, resource: @conference } - if current_user.has_any_role? :admin, { name: :organizer, resource: @conference }, { name: :cfp, resource: @conference }

View file

@ -67,15 +67,15 @@
%dl#proposal-info %dl#proposal-info
.col-md-12 .col-md-12
%dt Date: %dt Date:
%dd= @event.start_time.strftime("%Y %B %e %H:%M") if @event.start_time %dd= @event.scheduled_start_time.strftime("%Y %B %e %H:%M") if @event.scheduled_start_time.present?
.col-md-12 .col-md-12
%dt Duration: %dt Duration:
%dd= show_time(@event.event_type.length) %dd= show_time(@event.event_type.length)
.col-md-12 .col-md-12
%dt Room: %dt Room:
%dd %dd
- if @event.room - if @event.scheduled_room
= @event.room.name = @event.scheduled_room.name
.col-md-12 .col-md-12
%dt Conference: %dt Conference:
%dd= @event.program.conference.title %dd= @event.program.conference.title

View file

@ -0,0 +1,9 @@
class CreateSchedules < ActiveRecord::Migration
def change
create_table :schedules do |t|
t.belongs_to :program, index: true
t.timestamps null: false
end
add_column :programs, :selected_schedule, :integer # Selected schedule ID
end
end

View file

@ -0,0 +1,11 @@
class CreateEventSchedules < ActiveRecord::Migration
def change
create_table :event_schedules do |t|
t.belongs_to :event, index: true
t.belongs_to :schedule, index: true
t.belongs_to :room, index: true
t.datetime :start_time
t.timestamps null: false
end
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160624151257) do ActiveRecord::Schema.define(version: 20160704092023) do
create_table "ahoy_events", force: :cascade do |t| create_table "ahoy_events", force: :cascade do |t|
t.uuid "visit_id", limit: 16 t.uuid "visit_id", limit: 16
@ -176,6 +176,19 @@ ActiveRecord::Schema.define(version: 20160624151257) do
t.text "cfp_dates_updated_body" t.text "cfp_dates_updated_body"
end end
create_table "event_schedules", force: :cascade do |t|
t.integer "event_id"
t.integer "schedule_id"
t.integer "room_id"
t.datetime "start_time"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "event_schedules", ["event_id"], name: "index_event_schedules_on_event_id"
add_index "event_schedules", ["room_id"], name: "index_event_schedules_on_room_id"
add_index "event_schedules", ["schedule_id"], name: "index_event_schedules_on_schedule_id"
create_table "event_types", force: :cascade do |t| create_table "event_types", force: :cascade do |t|
t.string "title", null: false t.string "title", null: false
t.integer "length", default: 30 t.integer "length", default: 30
@ -261,6 +274,7 @@ ActiveRecord::Schema.define(version: 20160624151257) do
t.boolean "blind_voting", default: false t.boolean "blind_voting", default: false
t.datetime "voting_start_date" t.datetime "voting_start_date"
t.datetime "voting_end_date" t.datetime "voting_end_date"
t.integer "selected_schedule"
end end
create_table "qanswers", force: :cascade do |t| create_table "qanswers", force: :cascade do |t|
@ -335,6 +349,14 @@ ActiveRecord::Schema.define(version: 20160624151257) do
t.integer "venue_id", null: false t.integer "venue_id", null: false
end end
create_table "schedules", force: :cascade do |t|
t.integer "program_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "schedules", ["program_id"], name: "index_schedules_on_program_id"
create_table "splashpages", force: :cascade do |t| create_table "splashpages", force: :cascade do |t|
t.integer "conference_id" t.integer "conference_id"
t.boolean "public" t.boolean "public"

View file

@ -0,0 +1,24 @@
namespace :data do
desc 'Move the start_time and room attributes from Event to EventSchedule'
task move_events_attributes: :environment do
Program.all.each do |program|
schedule = Schedule.create(program: program)
program.selected_schedule = schedule.id
program.save
program.events.each do |event|
unless event.start_time.nil? && event.room_id.nil?
# we can not use .room as this relation has been removed
EventSchedule.create(event: event,
schedule: schedule,
start_time: event.start_time,
room_id: event.room_id)
event.start_time = nil
event.room_id = nil
event.save
end
end
end
puts 'The start_time and room attributes has been moved from Event to EventSchedule'
end
end