add issuer to transaction
This commit is contained in:
parent
d32bfc55fb
commit
87c2994cf0
5 changed files with 11 additions and 11 deletions
|
@ -10,15 +10,12 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
class Client < ActiveRecord::Base
|
class Client < ActiveRecord::Base
|
||||||
|
has_many :issued_transactions, as: :issuer, class_name: 'Transaction'
|
||||||
before_create :generate_key
|
before_create :generate_key
|
||||||
|
|
||||||
validates :name, presence: true, uniqueness: true
|
validates :name, presence: true, uniqueness: true
|
||||||
validates :key, presence: true, uniqueness: true
|
validates :key, presence: true, uniqueness: true
|
||||||
|
|
||||||
def transactions
|
|
||||||
Transaction.where(origin: name)
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
def generate_key
|
def generate_key
|
||||||
self.key = SecureRandom.base64(16) unless self.key
|
self.key = SecureRandom.base64(16) unless self.key
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
class Transaction < ActiveRecord::Base
|
class Transaction < ActiveRecord::Base
|
||||||
belongs_to :debtor, class_name: 'User'
|
belongs_to :debtor, class_name: 'User'
|
||||||
belongs_to :creditor, class_name: 'User'
|
belongs_to :creditor, class_name: 'User'
|
||||||
|
belongs_to :issuer, polymorphic: true
|
||||||
|
|
||||||
after_save :recalculate_balances
|
after_save :recalculate_balances
|
||||||
after_destroy :recalculate_balances
|
after_destroy :recalculate_balances
|
||||||
|
@ -22,10 +23,6 @@ class Transaction < ActiveRecord::Base
|
||||||
validates :amount, numericality: { greater_than: 0 }
|
validates :amount, numericality: { greater_than: 0 }
|
||||||
validate :different_debtor_creditor
|
validate :different_debtor_creditor
|
||||||
|
|
||||||
def client
|
|
||||||
Client.find_by name: origin
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def recalculate_balances
|
def recalculate_balances
|
||||||
|
@ -34,6 +31,8 @@ class Transaction < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def different_debtor_creditor
|
def different_debtor_creditor
|
||||||
self.errors.add :base, "Can't write money to yourself" if self.debtor == self.creditor
|
if self.debtor == self.creditor
|
||||||
|
self.errors.add :base, "Can't write money to yourself"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,6 +17,8 @@ class User < ActiveRecord::Base
|
||||||
has_many :outgoing_transactions,
|
has_many :outgoing_transactions,
|
||||||
class_name: 'Transaction', foreign_key: 'debtor_id'
|
class_name: 'Transaction', foreign_key: 'debtor_id'
|
||||||
|
|
||||||
|
has_many :issued_transactions, as: :issuer, class_name: 'Transaction'
|
||||||
|
|
||||||
validates :name, presence: true, uniqueness: true
|
validates :name, presence: true, uniqueness: true
|
||||||
|
|
||||||
def transactions
|
def transactions
|
||||||
|
|
|
@ -3,8 +3,8 @@ class CreateTransactions < ActiveRecord::Migration
|
||||||
create_table :transactions do |t|
|
create_table :transactions do |t|
|
||||||
t.references :debtor, index: true, null: false
|
t.references :debtor, index: true, null: false
|
||||||
t.references :creditor, index: true, null: false
|
t.references :creditor, index: true, null: false
|
||||||
|
t.references :issuer, polymorphic: true, index: true, null: false
|
||||||
t.integer :amount, null: false, default: 0
|
t.integer :amount, null: false, default: 0
|
||||||
t.string :origin, null: false
|
|
||||||
t.string :message
|
t.string :message
|
||||||
|
|
||||||
t.timestamps null: false
|
t.timestamps null: false
|
||||||
|
|
|
@ -26,8 +26,9 @@ ActiveRecord::Schema.define(version: 20150908092731) do
|
||||||
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.string "issuer_type", null: false
|
||||||
t.integer "amount", default: 0, null: false
|
t.integer "amount", default: 0, null: false
|
||||||
t.string "origin", 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
|
||||||
|
@ -35,6 +36,7 @@ ActiveRecord::Schema.define(version: 20150908092731) do
|
||||||
|
|
||||||
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_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|
|
||||||
t.string "name"
|
t.string "name"
|
||||||
|
|
Loading…
Reference in a new issue