1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-19 13:54:25 +01:00

add a validator to check reservation's slot is or not restrict for subscriptions

This commit is contained in:
Du Peng 2020-02-11 13:22:20 +01:00
parent 760109de05
commit 096a658bac
2 changed files with 18 additions and 0 deletions

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class Reservation < ActiveRecord::Base
include NotifyWith::NotificationAttachedObject
@ -17,6 +19,7 @@ class Reservation < ActiveRecord::Base
validates_presence_of :reservable_id, :reservable_type
validate :machine_not_already_reserved, if: -> { reservable.is_a?(Machine) }
validate :training_not_fully_reserved, if: -> { reservable.is_a?(Training) }
validates_with ReservationSlotSubscriptionValidator
attr_accessor :plan_id, :subscription

View File

@ -0,0 +1,15 @@
# frozen_string_literal: true
class ReservationSlotSubscriptionValidator < ActiveModel::Validator
def validate(record)
record.slots.each do |s|
unless s.availability.plan_ids.empty?
if record.user.subscribed_plan && s.availability.plan_ids.include?(record.user.subscribed_plan.id)
elsif s.availability.plan_ids.include?(record.plan_id)
else
record.errors[:slots] << 'slot is restrict for subscriptions'
end
end
end
end
end