1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/models/prepaid_pack.rb

32 lines
1.1 KiB
Ruby
Raw Normal View History

2021-06-21 14:58:49 +02:00
# frozen_string_literal: true
# Prepaid-packs of hours for machines/spaces.
2021-06-21 17:39:48 +02:00
#
# A prepaid-pack is a set a hours that can be bought by a member. After having bought one, a member will be able to book, for free,
# as much hours as there is in the pack, until the validity has not expired.
#
# The number of hours in a pack is stored in minutes.
2021-06-21 14:58:49 +02:00
class PrepaidPack < ApplicationRecord
belongs_to :priceable, polymorphic: true
2022-12-28 17:51:27 +01:00
belongs_to :machine, foreign_type: 'Machine', foreign_key: 'priceable_id', inverse_of: :prepaid_packs
belongs_to :space, foreign_type: 'Space', foreign_key: 'priceable_id', inverse_of: :prepaid_packs
2021-06-25 17:24:34 +02:00
2021-06-21 14:58:49 +02:00
belongs_to :group
2022-12-28 17:51:27 +01:00
has_many :statistic_profile_prepaid_packs, dependent: :destroy
has_many :cart_item_prepaid_packs, class_name: 'CartItem::PrepaidPack', dependent: :destroy
2021-06-21 14:58:49 +02:00
validates :amount, :group_id, :priceable_id, :priceable_type, :minutes, presence: true
2021-06-21 17:39:48 +02:00
def validity
return nil if validity_interval.nil?
validity_count&.send(validity_interval)
2021-06-21 14:58:49 +02:00
end
2021-06-21 17:39:48 +02:00
def destroyable?
statistic_profile_prepaid_packs.empty?
2021-06-21 14:58:49 +02:00
end
end