diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb index 6db16fd..2aad2b2 100644 --- a/app/controllers/transactions_controller.rb +++ b/app/controllers/transactions_controller.rb @@ -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 diff --git a/app/models/transaction.rb b/app/models/transaction.rb index 34746ef..7d82df8 100644 --- a/app/models/transaction.rb +++ b/app/models/transaction.rb @@ -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 diff --git a/db/migrate/20150914095049_add_id_at_client_to_transactions.rb b/db/migrate/20150914095049_add_id_at_client_to_transactions.rb new file mode 100644 index 0000000..a2ceea8 --- /dev/null +++ b/db/migrate/20150914095049_add_id_at_client_to_transactions.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index f6bf104..d784c74 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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| diff --git a/spec/apis/transactions_controller_spec.rb b/spec/apis/transactions_controller_spec.rb index d15aec1..9acc373 100644 --- a/spec/apis/transactions_controller_spec.rb +++ b/spec/apis/transactions_controller_spec.rb @@ -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 diff --git a/spec/factories/transactions.rb b/spec/factories/transactions.rb index 71e7e00..237b789 100644 --- a/spec/factories/transactions.rb +++ b/spec/factories/transactions.rb @@ -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 diff --git a/spec/helpers/pages_helper_spec.rb b/spec/helpers/pages_helper_spec.rb deleted file mode 100644 index 2960941..0000000 --- a/spec/helpers/pages_helper_spec.rb +++ /dev/null @@ -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 diff --git a/spec/models/transaction_spec.rb b/spec/models/transaction_spec.rb index d3cc92c..bed16fc 100644 --- a/spec/models/transaction_spec.rb +++ b/spec/models/transaction_spec.rb @@ -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