Merge branch 'master' into cute-little-statistics

This commit is contained in:
Felix Van der Jeugt 2015-09-14 16:32:45 +02:00
commit 32e1ebfb94
9 changed files with 89 additions and 61 deletions

View file

@ -3,6 +3,7 @@ class TransactionsController < ApplicationController
before_action :authenticate_user!, except: :create
before_action :authenticate_user_or_client!, only: :create
respond_to :js, only: :create
def create
@ -11,7 +12,7 @@ class TransactionsController < ApplicationController
authorize!(:create, @transaction)
if @transaction.save
head :created
render json: @transaction, status: :created
else
render json: @transaction.errors.full_messages,
status: :unprocessable_entity
@ -22,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

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

View file

@ -40,6 +40,6 @@ class User < ActiveRecord::Base
end
def self.zeus
@@zeus ||= find_or_create_by name: 'Zeus'
find_or_create_by name: 'Zeus'
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
@ -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|

View file

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

View file

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

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

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