From 44fcb2e089a419299d83829251844e3134330d52 Mon Sep 17 00:00:00 2001 From: Nicolas Florentin Date: Wed, 6 Apr 2016 18:24:26 +0200 Subject: [PATCH] draft a service to handle users credits --- app/services/users_credit_manager.rb | 82 ++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 app/services/users_credit_manager.rb diff --git a/app/services/users_credit_manager.rb b/app/services/users_credit_manager.rb new file mode 100644 index 000000000..1372e814c --- /dev/null +++ b/app/services/users_credit_manager.rb @@ -0,0 +1,82 @@ +class UsersCreditManager + attr_reader :manager + + def initialize(reservation) + if reservation.reservable_type == "Training" + @manager = Training.new(reservation) + elsif reservation.reservable_type == "Machine" + @manager = Machine.new(reservation) + end + end + + class Machine < Manager + def will_use_credits? + end + + def credited_hours_number + end + + def update + super + end + + private + def _will_use_credits? + end + end + + class Training < Manager + def will_use_credits? + _will_use_credits?[0] + end + + def update + super + will_use_credits, training_credit = _will_use_credits? + if will_use_credits + user.credits << training_credit # we create a new UsersCredit object + end + end + + private + def _will_use_credits? + # if there is a training_credit defined for this plan and this training + if training_credit = plan.training_credits.find_by(creditable_id: reservation.reservable_id) + # if user has not used all the plan credits + if user.training_credits.count < plan.training_credit_nb + return true, training_credit + end + end + return false, nil + end + end + + private + class Manager + attr_reader :reservation + + def initialize(reservation) + @reservation = reservation + @already_updated = false + end + + def plan + user.subscribed_plan + end + + def user + reservation.user + end + + def update + if @already_updated + raise AlreadyUpdated, "update credit is not idempotent ! do not try to update twice." + else + @already_updated = true + end + end + end + + class AlreadyUpdated < StandardError + end +end