mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-12-01 12:24:28 +01:00
add model WalletTransaction
This commit is contained in:
parent
db8a6e8a32
commit
363fd73bc4
@ -1,4 +1,16 @@
|
|||||||
class Wallet < ActiveRecord::Base
|
class Wallet < ActiveRecord::Base
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
|
||||||
validates :user, presence: true
|
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
|
end
|
||||||
|
6
app/models/wallet_transaction.rb
Normal file
6
app/models/wallet_transaction.rb
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
class WalletTransaction < ActiveRecord::Base
|
||||||
|
belongs_to :user
|
||||||
|
belongs_to :wallet
|
||||||
|
belongs_to :reservation
|
||||||
|
belongs_to :transactable, polymorphic: true
|
||||||
|
end
|
13
db/migrate/20160704165139_create_wallet_transactions.rb
Normal file
13
db/migrate/20160704165139_create_wallet_transactions.rb
Normal 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
|
17
db/schema.rb
17
db/schema.rb
@ -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
|
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|
|
create_table "wallets", force: :cascade do |t|
|
||||||
t.integer "user_id"
|
t.integer "user_id"
|
||||||
t.integer "amount", default: 0
|
t.integer "amount", default: 0
|
||||||
@ -727,5 +742,7 @@ ActiveRecord::Schema.define(version: 20160714095018) do
|
|||||||
add_foreign_key "prices", "plans"
|
add_foreign_key "prices", "plans"
|
||||||
add_foreign_key "user_tags", "tags"
|
add_foreign_key "user_tags", "tags"
|
||||||
add_foreign_key "user_tags", "users"
|
add_foreign_key "user_tags", "users"
|
||||||
|
add_foreign_key "wallet_transactions", "users"
|
||||||
|
add_foreign_key "wallet_transactions", "wallets"
|
||||||
add_foreign_key "wallets", "users"
|
add_foreign_key "wallets", "users"
|
||||||
end
|
end
|
||||||
|
13
test/fixtures/wallet_transactions.yml
vendored
Normal file
13
test/fixtures/wallet_transactions.yml
vendored
Normal 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
|
@ -10,4 +10,19 @@ class WalletTest < ActiveSupport::TestCase
|
|||||||
w = Wallet.create
|
w = Wallet.create
|
||||||
assert w.errors[:user].present?
|
assert w.errors[:user].present?
|
||||||
end
|
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
|
end
|
||||||
|
7
test/models/wallet_transaction_test.rb
Normal file
7
test/models/wallet_transaction_test.rb
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class WalletTransactionTest < ActiveSupport::TestCase
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user