1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-28 09:24:24 +01:00

add model WalletTransaction

This commit is contained in:
Peng DU 2016-07-04 19:20:10 +02:00
parent db8a6e8a32
commit 363fd73bc4
7 changed files with 83 additions and 0 deletions

View File

@ -1,4 +1,16 @@
class Wallet < ActiveRecord::Base
belongs_to :user
validates :user, presence: true
validates_numericality_of :amount, greater_than_or_equal_to: 0
def credit(amount)
self.amount += amount
save
end
def debit(amount)
self.amount -= amount
save
end
end

View File

@ -0,0 +1,6 @@
class WalletTransaction < ActiveRecord::Base
belongs_to :user
belongs_to :wallet
belongs_to :reservation
belongs_to :transactable, polymorphic: true
end

View File

@ -0,0 +1,13 @@
class CreateWalletTransactions < ActiveRecord::Migration
def change
create_table :wallet_transactions do |t|
t.belongs_to :user, index: true, foreign_key: true
t.belongs_to :wallet, index: true, foreign_key: true
t.references :transactable, polymorphic: true, index: {name: 'index_wallet_transactions_on_transactable'}
t.string :transaction_type
t.integer :amount
t.timestamps null: false
end
end
end

View File

@ -708,6 +708,21 @@ ActiveRecord::Schema.define(version: 20160714095018) do
add_index "users_roles", ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id", using: :btree
create_table "wallet_transactions", force: :cascade do |t|
t.integer "user_id"
t.integer "wallet_id"
t.integer "transactable_id"
t.string "transactable_type"
t.string "transaction_type"
t.integer "amount"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "wallet_transactions", ["transactable_type", "transactable_id"], name: "index_wallet_transactions_on_transactable", using: :btree
add_index "wallet_transactions", ["user_id"], name: "index_wallet_transactions_on_user_id", using: :btree
add_index "wallet_transactions", ["wallet_id"], name: "index_wallet_transactions_on_wallet_id", using: :btree
create_table "wallets", force: :cascade do |t|
t.integer "user_id"
t.integer "amount", default: 0
@ -727,5 +742,7 @@ ActiveRecord::Schema.define(version: 20160714095018) do
add_foreign_key "prices", "plans"
add_foreign_key "user_tags", "tags"
add_foreign_key "user_tags", "users"
add_foreign_key "wallet_transactions", "users"
add_foreign_key "wallet_transactions", "wallets"
add_foreign_key "wallets", "users"
end

13
test/fixtures/wallet_transactions.yml vendored Normal file
View File

@ -0,0 +1,13 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
user_id:
wallet_id:
transaction_type: MyString
amount: 1
two:
user_id:
wallet_id:
transaction_type: MyString
amount: 1

View File

@ -10,4 +10,19 @@ class WalletTest < ActiveSupport::TestCase
w = Wallet.create
assert w.errors[:user].present?
end
test 'can credit amount' do
w = Wallet.first
expected_amount = w.amount + 5
assert w.credit(5)
assert_equal w.amount, expected_amount
end
test 'can debit amount' do
w = Wallet.first
w.credit(5)
expected_amount = 0
assert w.debit(5)
assert_equal w.amount, expected_amount
end
end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class WalletTransactionTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end