From a0397a2c153bd1b2c667043ad05cfad915565e3e Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 13 Apr 2016 13:37:05 +0200 Subject: [PATCH] [bug taiga#25] allow event update if changing ng_places --- app/models/event.rb | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/app/models/event.rb b/app/models/event.rb index b28b8b0ba..8007f6a4e 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -13,8 +13,7 @@ class Event < ActiveRecord::Base attr_accessor :recurrence, :recurrence_end_at after_create :event_recurrence - before_save :set_nb_free_places - before_update :update_nb_free_places, if: :nb_total_places_changed? + before_save :update_nb_free_places def name title @@ -33,6 +32,10 @@ class Event < ActiveRecord::Base end end + def reservations + Reservation.where(reservable: self) + end + private def event_recurrence if recurrence.present? and recurrence != 'none' @@ -79,16 +82,12 @@ class Event < ActiveRecord::Base end end - def set_nb_free_places - if nb_free_places.nil? - self.nb_free_places = nb_total_places - end - end - def update_nb_free_places - unless nb_total_places_was.nil? or nb_total_places.nil? - diff = nb_total_places - nb_total_places_was - self.nb_free_places += diff + if nb_total_places.nil? + self.nb_free_places = nil + else + reserved_places = reservations.map{|r| r.nb_reserve_places + r.nb_reserve_reduced_places}.inject(0){|sum, t| sum + t } + self.nb_free_places = (nb_total_places - reserved_places) end end end