Rebuild usage_credits on wallets ledger core (v1.0.0)#30
Rebuild usage_credits on wallets ledger core (v1.0.0)#30
Conversation
This release rebuilds usage_credits on top of the new `wallets` gem, which provides the ledger core: balances, transactions, allocations, transfers, and expiration handling. Architecture: - UsageCredits::Wallet, Transaction, Allocation, Transfer now extend their Wallets::* counterparts using the embeddability hooks - Each subclass sets embedded_table_name, config_provider, callbacks_module, and related model class names to maintain full isolation - Both gems can coexist in the same Rails app without table/config collision Key changes: - Add `wallets` gem dependency - Wallet/Transaction/Allocation models now extend Wallets::* base classes - Add Transfer model for credit transfers between users - Add upgrade generator for pre-1.0 installs (asset_code, bigint columns, transfers table, transfer_id on transactions) - Migration templates updated with new schema (expiration_policy on transfers, no singular outbound/inbound transaction FKs) - Coexistence test verifies both gems work independently in same app Backwards compatibility: - All existing API preserved: give_credits, deduct_credits, spend_credits_on - credits, credit_history, has_enough_credits_to? unchanged - Existing installs run upgrade migration to add new columns/tables New capabilities from wallets core: - Transfer expiration policies (preserve/none/fixed) - Multi-bucket transfer splitting for expiration preservation - Row-level locking for concurrent operations - Balance snapshots on transactions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
placeholder test comment - will be replaced |
|
ignore this test comment |
|
ignore |
|
multi line |
|
heading test: |
- Properly set self.credit_wallet after find/create in ensure_credit_wallet - Gemspec: better file excludes, add Rails < 9.0 ceiling Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
line 1 |
|
line 1 HTML Heading Testline 3 content |
Code Review β PR #30: Rebuild on
|
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Code Review: PR #30 β Rebuild usage_credits on wallets ledger core (v1.0.0)This is a substantial, well-structured refactor. The layering strategy (extending π΄ High Priority1.
def credits
balance
endThe comment above it says:
But the implementation doesn't enforce this floor. If the wallets layer allows .yield_self { |sum| [sum, 0].max }.to_iIf the intent is truly to preserve that contract, it should be: def credits
[balance, 0].max
endβ¦or the comment should be revised to say the floor is intentionally removed. 2.
def add_credits(amount, metadata: {}, category: :credit_added, expires_at: nil, fulfillment: nil)
credit(
amount,
metadata: metadata,
category: category,
expires_at: expires_at,
fulfillment: fulfillment # β is Wallets::Wallet#credit expecting this?
)
endIf 3. Upgrade migration has no
def down
raise ActiveRecord::IrreversibleMigration, "Cannot roll back the 1.0 upgrade. Restore from a database backup."
endπ‘ Medium Priority4. Locking behavior in The original 5.
def table_prefix
"usage_credits_"
endThe comment says this is for wallets gem compatibility and is always 6. The README documents the wallet-level API as π’ Low / Nits7. Schema version vs. migration version mismatch
8. wallet = original_credit_wallet || UsageCredits::Wallet.find_by(owner: self, asset_code: "credits")
if wallet.present?
self.credit_wallet = wallet unless original_credit_wallet == wallet
return wallet
endWhen 9. Old The β What's working well
Overall this is a clean layering. The |
Summary
This release rebuilds
usage_creditson top of the newwalletsgem, which provides the ledger core: balances, transactions, allocations, transfers, and expiration handling.Architecture
UsageCredits::Wallet,Transaction,Allocation,Transfernow extend theirWallets::*counterparts using the embeddability hooksembedded_table_name,config_provider,callbacks_module, and related model class namesUsageCredits::Walletcannot transfer toWallets::Wallet)Key Changes
New Files
lib/usage_credits/models/transfer.rbβ credit transfers between userslib/generators/usage_credits/upgrade_generator.rbβ upgrade migration for pre-1.0 installstest/integration/coexistence_test.rbβ verifies both gems work independentlySchema Updates
expiration_policycolumn on transfers (preserve/none/fixed)transfer_idFK on transactions (no singular outbound/inbound FKs)asset_codecolumn on wallets (default: "credits")bigintModel Changes
Wallets::*base classesUsageCredits::*classescreditedβcredits_added,debitedβcredits_deducted, etc.Backwards Compatibility
All existing API preserved:
Existing installs run the upgrade migration to add new columns/tables.
New Capabilities from Wallets Core
:preserve(default),:none,:fixedbalance_before/balance_afteron every transactionTest Results
Test Plan
π€ Generated with Claude Code