1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/models/cart_item/subscription.rb

53 lines
932 B
Ruby
Raw Normal View History

# frozen_string_literal: true
# A subscription added to the shopping cart
class CartItem::Subscription < CartItem::BaseItem
2021-10-14 18:20:10 +02:00
attr_reader :start_at
def initialize(plan, customer, start_at = nil)
2021-04-26 11:40:26 +02:00
raise TypeError unless plan.is_a? Plan
@plan = plan
@customer = customer
2021-10-14 18:20:10 +02:00
@start_at = start_at
super
end
def plan
raise InvalidGroupError if @plan.group_id != @customer.group_id
@plan
end
def price
amount = plan.amount
elements = { plan: amount }
{ elements: elements, amount: amount }
end
def name
@plan.base_name
end
def to_object
2021-06-01 12:20:02 +02:00
::Subscription.new(
plan_id: @plan.id,
2021-10-14 18:20:10 +02:00
statistic_profile_id: StatisticProfile.find_by(user: @customer).id,
start_at: @start_at
)
end
def type
'subscription'
end
def valid?(_all_items)
if @plan.disabled
@errors[:item] = 'plan is disabled'
return false
end
true
end
end