Add id_at_client to transactions + tests

This commit is contained in:
benji 2015-09-14 13:16:01 +02:00
parent 6c04a90f68
commit 0cf5872a98
8 changed files with 85 additions and 58 deletions

View file

@ -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

View file

@ -2,15 +2,16 @@
#
# Table name: transactions
#
# id :integer not null, primary key
# debtor_id :integer not null
# creditor_id :integer not null
# issuer_id :integer not null
# issuer_type :string not null
# amount :integer default(0), not null
# message :string
# created_at :datetime not null
# updated_at :datetime not null
# id :integer not null, primary key
# debtor_id :integer not null
# creditor_id :integer not null
# issuer_id :integer not null
# issuer_type :string not null
# amount :integer default(0), not null
# 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

View file

@ -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

View file

@ -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
@ -24,18 +24,20 @@ ActiveRecord::Schema.define(version: 20150908092731) do
add_index "clients", ["name"], name: "index_clients_on_name"
create_table "transactions", force: :cascade do |t|
t.integer "debtor_id", null: false
t.integer "creditor_id", null: false
t.integer "issuer_id", null: false
t.string "issuer_type", null: false
t.integer "amount", default: 0, null: false
t.integer "debtor_id", null: false
t.integer "creditor_id", null: false
t.integer "issuer_id", null: false
t.string "issuer_type", null: false
t.integer "amount", default: 0, null: false
t.string "message"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
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|

View file

@ -3,11 +3,12 @@ describe TransactionsController, type: :api do
@debtor = create :user
@creditor = create :user
@api_attributes = {
debtor: @debtor.name,
creditor: @creditor.name,
message: Faker::Lorem.sentence,
euros: 1,
cents: 25
debtor: @debtor.name,
creditor: @creditor.name,
message: Faker::Lorem.sentence,
euros: 1,
cents: 25,
id_at_client: 1
}
@client = Client.create name: "Tap"
@key = @client.key

View file

@ -2,15 +2,16 @@
#
# Table name: transactions
#
# id :integer not null, primary key
# debtor_id :integer not null
# creditor_id :integer not null
# issuer_id :integer not null
# issuer_type :string not null
# amount :integer default(0), not null
# message :string
# created_at :datetime not null
# updated_at :datetime not null
# id :integer not null, primary key
# debtor_id :integer not null
# creditor_id :integer not null
# issuer_id :integer not null
# issuer_type :string not null
# amount :integer default(0), not null
# 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

View file

@ -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

View file

@ -2,15 +2,16 @@
#
# Table name: transactions
#
# id :integer not null, primary key
# debtor_id :integer not null
# creditor_id :integer not null
# issuer_id :integer not null
# issuer_type :string not null
# amount :integer default(0), not null
# message :string
# created_at :datetime not null
# updated_at :datetime not null
# id :integer not null, primary key
# debtor_id :integer not null
# creditor_id :integer not null
# issuer_id :integer not null
# issuer_type :string not null
# amount :integer default(0), not null
# 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