diff --git a/app/models/wallet.rb b/app/models/wallet.rb index c78dcdce5..8c2374e64 100644 --- a/app/models/wallet.rb +++ b/app/models/wallet.rb @@ -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 diff --git a/app/models/wallet_transaction.rb b/app/models/wallet_transaction.rb new file mode 100644 index 000000000..040c4d4d3 --- /dev/null +++ b/app/models/wallet_transaction.rb @@ -0,0 +1,6 @@ +class WalletTransaction < ActiveRecord::Base + belongs_to :user + belongs_to :wallet + belongs_to :reservation + belongs_to :transactable, polymorphic: true +end diff --git a/db/migrate/20160704165139_create_wallet_transactions.rb b/db/migrate/20160704165139_create_wallet_transactions.rb new file mode 100644 index 000000000..a74b878ab --- /dev/null +++ b/db/migrate/20160704165139_create_wallet_transactions.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index b6fa65dc1..0a50f1196 100644 --- a/db/schema.rb +++ b/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 + 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 diff --git a/test/fixtures/wallet_transactions.yml b/test/fixtures/wallet_transactions.yml new file mode 100644 index 000000000..8fef445f6 --- /dev/null +++ b/test/fixtures/wallet_transactions.yml @@ -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 diff --git a/test/models/wallet_test.rb b/test/models/wallet_test.rb index d878f1800..ef4d6b52b 100644 --- a/test/models/wallet_test.rb +++ b/test/models/wallet_test.rb @@ -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 diff --git a/test/models/wallet_transaction_test.rb b/test/models/wallet_transaction_test.rb new file mode 100644 index 000000000..01796026f --- /dev/null +++ b/test/models/wallet_transaction_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class WalletTransactionTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end