Class: SpreeCmCommissioner::VoteCreditDeductor
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::VoteCreditDeductor
- Defined in:
- app/services/spree_cm_commissioner/vote_credit_deductor.rb
Instance Attribute Summary collapse
-
#contestant ⇒ Object
readonly
Returns the value of attribute contestant.
-
#quantity ⇒ Object
readonly
Returns the value of attribute quantity.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#voting_session ⇒ Object
readonly
Returns the value of attribute voting_session.
Instance Method Summary collapse
-
#call ⇒ Object
Returns the VotingCredit that was deducted.
-
#initialize(voting_session:, contestant:, user:, quantity:) ⇒ VoteCreditDeductor
constructor
A new instance of VoteCreditDeductor.
Constructor Details
#initialize(voting_session:, contestant:, user:, quantity:) ⇒ VoteCreditDeductor
Returns a new instance of VoteCreditDeductor.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/services/spree_cm_commissioner/vote_credit_deductor.rb', line 10 def initialize( voting_session:, # VotingSession the vote belongs to contestant:, # VotingContestant being voted for user:, # Spree::User casting the vote quantity: # number of votes to cast ) @voting_session = voting_session @contestant = contestant @user = user @quantity = quantity end |
Instance Attribute Details
#contestant ⇒ Object (readonly)
Returns the value of attribute contestant.
8 9 10 |
# File 'app/services/spree_cm_commissioner/vote_credit_deductor.rb', line 8 def contestant @contestant end |
#quantity ⇒ Object (readonly)
Returns the value of attribute quantity.
8 9 10 |
# File 'app/services/spree_cm_commissioner/vote_credit_deductor.rb', line 8 def quantity @quantity end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
8 9 10 |
# File 'app/services/spree_cm_commissioner/vote_credit_deductor.rb', line 8 def user @user end |
#voting_session ⇒ Object (readonly)
Returns the value of attribute voting_session.
8 9 10 |
# File 'app/services/spree_cm_commissioner/vote_credit_deductor.rb', line 8 def voting_session @voting_session end |
Instance Method Details
#call ⇒ Object
Returns the VotingCredit that was deducted.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/services/spree_cm_commissioner/vote_credit_deductor.rb', line 23 def call credit = find_eligible_credit raise 'No eligible voting credits available' if credit.nil? credit.with_lock do raise 'Insufficient voting credits' unless credit.can_use?(quantity) # Deduct free votes first, then paid. # Example: quantity=5, free_remaining=3, paid_remaining=10 # free_deduct=3, paid_deduct=2 free_deduct = [credit.free_votes_remaining, quantity].min paid_deduct = quantity - free_deduct credit.update!( free_votes_used: credit.free_votes_used + free_deduct, paid_votes_used: credit.paid_votes_used + paid_deduct ) memo = "Used #{quantity}, paid: #{paid_deduct}, free: #{free_deduct} votes for voting session #{voting_session.id}" SpreeCmCommissioner::VotingCreditTransaction.create!( tenant_id: credit.tenant_id, vote_credit: credit, action: :use, amount: quantity, originator: voting_session, episode_id: voting_session.episode_id, user_total_amount: credit.available_votes, memo: memo ) end credit end |