diff --git a/app/assets/javascripts/controllers/main/DataEntryCtrl.coffee b/app/assets/javascripts/controllers/main/DataEntryCtrl.coffee index 4eba6b2..4add8c7 100644 --- a/app/assets/javascripts/controllers/main/DataEntryCtrl.coffee +++ b/app/assets/javascripts/controllers/main/DataEntryCtrl.coffee @@ -35,6 +35,11 @@ $scope.generate_schedule = () -> window.location.href = '/#/schedule' + $scope.getWorkType = (wt) -> + if wt.nil? + return "" + else return "(" + wt + ")" + ### PEOPLE AND CONFLICTS ### $scope.people_message = {person: {}} $scope.people_success = (response) -> diff --git a/app/assets/javascripts/controllers/main/ScheduleCtrl.coffee b/app/assets/javascripts/controllers/main/ScheduleCtrl.coffee index 4130560..82d7401 100644 --- a/app/assets/javascripts/controllers/main/ScheduleCtrl.coffee +++ b/app/assets/javascripts/controllers/main/ScheduleCtrl.coffee @@ -54,10 +54,16 @@ swappable_works $scope.highlight_all_swappable = (orig_work) -> - for rehearsal in $scope.schedule.rehearsals - for work in rehearsal.works - if work.id in $scope.all_swappable(orig_work) - work.swappable = true + $http({ + method: 'GET', + url: '/works/all_fits/' + orig_work.id + '.json' + }).then((response) -> + console.log response.data + for rehearsal in $scope.schedule.rehearsals + for work in rehearsal.works + if work.id in response.data + work.swappable = true + ) $scope.unhighlight_all_swappable = () -> for rehearsal in $scope.schedule.rehearsals diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index ae4952e..cefab5d 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -49,6 +49,22 @@ def destroy end end + def all_fits + work = Work.find(params[:id]) + start_time = work.rehearsal.start_time + rehearsal_works = work.rehearsal.works.order(:sequence_id) + for w in rehearsal_works do + break if w.id == work.id + start_time += w.duration + start_time += w.break_duration + end + works = Work.where(production_id: current_user.person.production_id).where.not(id: work.id).select { |w| w.fits?(start_time) }.map(&:id) + + respond_to do |format| + format.json { render json: works } + end + end + private def set_work @work = Work.find(params[:id]) diff --git a/app/models/work.rb b/app/models/work.rb index 0673f40..ca860ef 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -9,10 +9,18 @@ class Work < ApplicationRecord accepts_nested_attributes_for :person_works, allow_destroy: true accepts_nested_attributes_for :dependencies, allow_destroy: true + validate :duration_not_zero + validates :name, :duration, presence: true + def called people end + def duration_not_zero + errors.add(:duration, "Duration must be at least 1 minute") if + duration < 1 + end + # Whether this piece of work can be scheduled at a given start time # without conflicts. # TODO Allow for expressing priority, maybe by returning a number instead @@ -22,9 +30,9 @@ def fits?(start_datetime) end def all_conflicts(start_datetime) - end_datetime = start_time + self.duration.minutes + end_datetime = start_datetime + self.duration.minutes conflicts = Set.new - work.called.each do |p| + self.called.each do |p| p.conflicts.each do |c| unless c.fits?(start_datetime, end_datetime) conflicts << c diff --git a/app/views/people/index.html.erb b/app/views/people/index.html.erb index 3ac8f93..c9e09fa 100644 --- a/app/views/people/index.html.erb +++ b/app/views/people/index.html.erb @@ -16,6 +16,8 @@ <%= person.first %> <%= person.last %> + <%= person.cell %> + <%= person.email %> <%= link_to 'Show', person %> <%= link_to 'Edit', edit_person_path(person) %> <%= link_to 'Destroy', person, method: :delete, data: { confirm: 'Are you sure?' } %> diff --git a/config/routes.rb b/config/routes.rb index 0e90846..230bd29 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,8 @@ post 'api/mobile/cell' => 'mobile#cell' get 'api/mobile/refresh' => 'mobile#refresh' + get 'works/all_fits/:id' => 'works#all_fits' + # TODO: get rid of this get 'generate' => 'schedules#generate'