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:
parent
760109de05
commit
096a658bac
@ -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
|
||||
|
||||
|
15
app/validators/reservation_slot_subscription_validator.rb
Normal file
15
app/validators/reservation_slot_subscription_validator.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user