Add id_at_client to transactions + tests
This commit is contained in:
parent
6c04a90f68
commit
0cf5872a98
8 changed files with 85 additions and 58 deletions
|
@ -23,14 +23,14 @@ class TransactionsController < ApplicationController
|
|||
|
||||
def transaction_params
|
||||
t = params.require(:transaction)
|
||||
.permit(:debtor, :creditor, :message, :euros, :cents)
|
||||
.permit(:debtor, :creditor, :message, :euros, :cents, :id_at_client)
|
||||
|
||||
{
|
||||
debtor: t[:debtor] ? User.find_or_create_by(name: t[:debtor]) : User.zeus,
|
||||
creditor: t[:creditor] ? User.find_or_create_by(name: t[:creditor]) : User.zeus,
|
||||
issuer: current_client || current_user,
|
||||
amount: (t[:euros].to_f * 100 + t[:cents].to_f).to_i,
|
||||
message: t[:message]
|
||||
}
|
||||
message: t[:message],
|
||||
}.merge(current_client ? { id_at_client: t[:id_at_client] } : {})
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
# message :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# id_at_client :integer
|
||||
#
|
||||
|
||||
class Transaction < ActiveRecord::Base
|
||||
|
@ -22,6 +23,7 @@ class Transaction < ActiveRecord::Base
|
|||
after_destroy :recalculate_balances
|
||||
|
||||
validates :amount, numericality: { greater_than: 0 }
|
||||
validates :id_at_client, presence: true, uniqueness: { scope: :issuer_id }, if: :is_client_transaction?
|
||||
validate :different_debtor_creditor
|
||||
|
||||
|
||||
|
@ -52,4 +54,8 @@ class Transaction < ActiveRecord::Base
|
|||
self.errors.add :base, "Can't write money to yourself"
|
||||
end
|
||||
end
|
||||
|
||||
def is_client_transaction?
|
||||
issuer_type == "Client"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
class AddIdAtClientToTransactions < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :transactions, :id_at_client, :int
|
||||
add_index :transactions, [:issuer_id, :id_at_client]
|
||||
end
|
||||
end
|
|
@ -11,7 +11,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20150908092731) do
|
||||
ActiveRecord::Schema.define(version: 20150914095049) do
|
||||
|
||||
create_table "clients", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
|
@ -32,10 +32,12 @@ ActiveRecord::Schema.define(version: 20150908092731) do
|
|||
t.string "message"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "id_at_client"
|
||||
end
|
||||
|
||||
add_index "transactions", ["creditor_id"], name: "index_transactions_on_creditor_id"
|
||||
add_index "transactions", ["debtor_id"], name: "index_transactions_on_debtor_id"
|
||||
add_index "transactions", ["issuer_id", "id_at_client"], name: "index_transactions_on_issuer_id_and_id_at_client"
|
||||
add_index "transactions", ["issuer_type", "issuer_id"], name: "index_transactions_on_issuer_type_and_issuer_id"
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
|
|
|
@ -7,7 +7,8 @@ describe TransactionsController, type: :api do
|
|||
creditor: @creditor.name,
|
||||
message: Faker::Lorem.sentence,
|
||||
euros: 1,
|
||||
cents: 25
|
||||
cents: 25,
|
||||
id_at_client: 1
|
||||
}
|
||||
@client = Client.create name: "Tap"
|
||||
@key = @client.key
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
# message :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# id_at_client :integer
|
||||
#
|
||||
|
||||
FactoryGirl.define do
|
||||
|
@ -20,5 +21,9 @@ FactoryGirl.define do
|
|||
issuer { debtor }
|
||||
amount { 1 + rand(100) }
|
||||
message { Faker::Lorem.sentence }
|
||||
factory :client_transaction do
|
||||
association :issuer, factory: :client
|
||||
sequence :id_at_client
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
require 'rails_helper'
|
||||
|
||||
# Specs in this file have access to a helper object that includes
|
||||
# the PagesHelper. For example:
|
||||
#
|
||||
# describe PagesHelper do
|
||||
# describe "string concat" do
|
||||
# it "concats two strings with spaces" do
|
||||
# expect(helper.concat_strings("this","that")).to eq("this that")
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
RSpec.describe PagesHelper, type: :helper do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
|
@ -11,6 +11,7 @@
|
|||
# message :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# id_at_client :integer
|
||||
#
|
||||
|
||||
describe Transaction, type: :model do
|
||||
|
@ -50,4 +51,25 @@ describe Transaction, type: :model do
|
|||
expect(build :transaction, debtor: @user, creditor: @user).to_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
describe "client as issuer" do
|
||||
before :each do
|
||||
@transaction = create :client_transaction
|
||||
end
|
||||
|
||||
it "should have a id_at_client" do
|
||||
@transaction.id_at_client = nil
|
||||
expect(@transaction).to_not be_valid
|
||||
end
|
||||
|
||||
it "should have a unique id_at_client" do
|
||||
t = build :client_transaction, issuer: @transaction.issuer, id_at_client: @transaction.id_at_client
|
||||
expect(t).to_not be_valid
|
||||
end
|
||||
|
||||
it "should have a unique id_at_client per client" do
|
||||
t = build :client_transaction, id_at_client: @transaction.id_at_client
|
||||
expect(t).to be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue