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!, except: :create
before_action :authenticate_user_or_client!, only: :create before_action :authenticate_user_or_client!, only: :create
respond_to :js, only: :create respond_to :js, only: :create
def create def create
@ -11,7 +12,7 @@ class TransactionsController < ApplicationController
authorize!(:create, @transaction) authorize!(:create, @transaction)
if @transaction.save if @transaction.save
head :created render json: @transaction, status: :created
else else
render json: @transaction.errors.full_messages, render json: @transaction.errors.full_messages,
status: :unprocessable_entity status: :unprocessable_entity
@ -22,14 +23,14 @@ class TransactionsController < ApplicationController
def transaction_params def transaction_params
t = params.require(:transaction) 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, 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, creditor: t[:creditor] ? User.find_or_create_by(name: t[:creditor]) : User.zeus,
issuer: current_client || current_user, issuer: current_client || current_user,
amount: (t[:euros].to_f*100 + t[:cents].to_f).to_i, 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
end end

View file

@ -2,15 +2,16 @@
# #
# Table name: transactions # Table name: transactions
# #
# id :integer not null, primary key # id :integer not null, primary key
# debtor_id :integer not null # debtor_id :integer not null
# creditor_id :integer not null # creditor_id :integer not null
# issuer_id :integer not null # issuer_id :integer not null
# issuer_type :string not null # issuer_type :string not null
# amount :integer default(0), not null # amount :integer default(0), not null
# message :string # message :string
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# id_at_client :integer
# #
class Transaction < ActiveRecord::Base class Transaction < ActiveRecord::Base
@ -22,6 +23,7 @@ class Transaction < ActiveRecord::Base
after_destroy :recalculate_balances after_destroy :recalculate_balances
validates :amount, numericality: { greater_than: 0 } validates :amount, numericality: { greater_than: 0 }
validates :id_at_client, presence: true, uniqueness: { scope: :issuer_id }, if: :is_client_transaction?
validate :different_debtor_creditor validate :different_debtor_creditor
@ -52,4 +54,8 @@ class Transaction < ActiveRecord::Base
self.errors.add :base, "Can't write money to yourself" self.errors.add :base, "Can't write money to yourself"
end end
end end
def is_client_transaction?
issuer_type == "Client"
end
end end

View file

@ -40,6 +40,6 @@ class User < ActiveRecord::Base
end end
def self.zeus def self.zeus
@@zeus ||= find_or_create_by name: 'Zeus' find_or_create_by name: 'Zeus'
end end
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. # 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| create_table "clients", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
@ -24,18 +24,20 @@ ActiveRecord::Schema.define(version: 20150908092731) do
add_index "clients", ["name"], name: "index_clients_on_name" add_index "clients", ["name"], name: "index_clients_on_name"
create_table "transactions", force: :cascade do |t| create_table "transactions", force: :cascade do |t|
t.integer "debtor_id", null: false t.integer "debtor_id", null: false
t.integer "creditor_id", null: false t.integer "creditor_id", null: false
t.integer "issuer_id", null: false t.integer "issuer_id", null: false
t.string "issuer_type", null: false t.string "issuer_type", null: false
t.integer "amount", default: 0, null: false t.integer "amount", default: 0, null: false
t.string "message" t.string "message"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "id_at_client"
end end
add_index "transactions", ["creditor_id"], name: "index_transactions_on_creditor_id" 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", ["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" add_index "transactions", ["issuer_type", "issuer_id"], name: "index_transactions_on_issuer_type_and_issuer_id"
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|

View file

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

View file

@ -2,15 +2,16 @@
# #
# Table name: transactions # Table name: transactions
# #
# id :integer not null, primary key # id :integer not null, primary key
# debtor_id :integer not null # debtor_id :integer not null
# creditor_id :integer not null # creditor_id :integer not null
# issuer_id :integer not null # issuer_id :integer not null
# issuer_type :string not null # issuer_type :string not null
# amount :integer default(0), not null # amount :integer default(0), not null
# message :string # message :string
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# id_at_client :integer
# #
FactoryGirl.define do FactoryGirl.define do
@ -20,5 +21,9 @@ FactoryGirl.define do
issuer { debtor } issuer { debtor }
amount { 1 + rand(100) } amount { 1 + rand(100) }
message { Faker::Lorem.sentence } message { Faker::Lorem.sentence }
factory :client_transaction do
association :issuer, factory: :client
sequence :id_at_client
end
end 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 # Table name: transactions
# #
# id :integer not null, primary key # id :integer not null, primary key
# debtor_id :integer not null # debtor_id :integer not null
# creditor_id :integer not null # creditor_id :integer not null
# issuer_id :integer not null # issuer_id :integer not null
# issuer_type :string not null # issuer_type :string not null
# amount :integer default(0), not null # amount :integer default(0), not null
# message :string # message :string
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# id_at_client :integer
# #
describe Transaction, type: :model do 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 expect(build :transaction, debtor: @user, creditor: @user).to_not be_valid
end end
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 end