Class: SpreeCmCommissioner::TicketTransfer
- Defined in:
- app/models/spree_cm_commissioner/ticket_transfer.rb
Constant Summary collapse
- SETTLEMENT_STATUS_TRANSITIONS =
{ 'pending' => %w[ready], 'ready' => %w[disputed completed], 'disputed' => %w[resolved], 'resolved' => %w[completed], 'completed' => [] }.freeze
Class Method Summary collapse
-
.hop_count_for(guest, max_hops:) ⇒ Object
Number of completed transfer hops that produced ‘guest`.
- .ransackable_associations(_auth_object = nil) ⇒ Object
- .ransackable_attributes(_auth_object = nil) ⇒ Object
Instance Method Summary collapse
- #free_transfer? ⇒ Boolean
-
#log_settlement_status_changes(user_id:) ⇒ Object
Records an audit trail of settlement_status changes to spree_state_changes.
- #log_state_changes(user_id:) ⇒ Object
Class Method Details
.hop_count_for(guest, max_hops:) ⇒ Object
Number of completed transfer hops that produced ‘guest`.
- 0 for an original guest (never transferred into existence)
- 1 for Bob's guest after Alice -> Bob completed
- 2 for Carol's guest after Alice -> Bob -> Carol completed
Walks the chain via ‘to_guest_id` -> `from_guest` links.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/models/spree_cm_commissioner/ticket_transfer.rb', line 102 def self.hop_count_for(guest, max_hops:) return 0 if guest.blank? count = 0 guest_id = guest.id while count < max_hops incoming = completed.where(to_guest_id: guest_id).select(:id, :from_guest_id).first break if incoming.nil? count += 1 guest_id = incoming.from_guest_id break if guest_id.nil? end count end |
.ransackable_associations(_auth_object = nil) ⇒ Object
61 62 63 |
# File 'app/models/spree_cm_commissioner/ticket_transfer.rb', line 61 def self.ransackable_associations(_auth_object = nil) %w[from_user to_user from_guest to_guest order] end |
.ransackable_attributes(_auth_object = nil) ⇒ Object
57 58 59 |
# File 'app/models/spree_cm_commissioner/ticket_transfer.rb', line 57 def self.ransackable_attributes(_auth_object = nil) %w[number state settlement_status created_at from_user_id to_user_id order_id price total] end |
Instance Method Details
#free_transfer? ⇒ Boolean
65 66 67 |
# File 'app/models/spree_cm_commissioner/ticket_transfer.rb', line 65 def free_transfer? gift? || manual_payment? end |
#log_settlement_status_changes(user_id:) ⇒ Object
Records an audit trail of settlement_status changes to spree_state_changes. Call this explicitly after a successful save to capture who changed the status and when.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/models/spree_cm_commissioner/ticket_transfer.rb', line 71 def log_settlement_status_changes(user_id:) return unless persisted? && saved_change_to_settlement_status? previous_status, next_status = saved_change_to_settlement_status state_changes.create( previous_state: previous_status, next_state: next_status, name: 'settlement_status', user_id: user_id ) end |
#log_state_changes(user_id:) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/models/spree_cm_commissioner/ticket_transfer.rb', line 83 def log_state_changes(user_id:) return unless persisted? && saved_change_to_state? previous_state, next_state = saved_change_to_state state_changes.create( previous_state: previous_state, next_state: next_state, name: 'state', user_id: user_id ) end |