Merge branch 'api' of https://github.com/ZeusWPI/Tab into api
This commit is contained in:
commit
c3bf2b74cc
9 changed files with 19 additions and 18 deletions
|
@ -1,7 +1,7 @@
|
|||
class ApplicationController < ActionController::Base
|
||||
# Prevent CSRF attacks by raising an exception.
|
||||
# For APIs, you may want to use :null_session instead.
|
||||
protect_from_forgery with: :exception
|
||||
protect_from_forgery with: :null_session
|
||||
|
||||
rescue_from CanCan::AccessDenied do |exception|
|
||||
redirect_to root_url, alert: exception.message
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
class TransactionsController < ApplicationController
|
||||
skip_before_filter :verify_authenticity_token, :only => :create
|
||||
|
||||
def index
|
||||
@transactions = Transaction.all
|
||||
end
|
||||
|
|
|
@ -10,14 +10,11 @@
|
|||
#
|
||||
|
||||
class Client < ActiveRecord::Base
|
||||
has_many :issued_transactions, as: :issuer, class_name: 'Transaction'
|
||||
before_create :generate_key
|
||||
|
||||
validates :name, presence: true, uniqueness: true
|
||||
|
||||
def transactions
|
||||
Transaction.where(origin: name)
|
||||
end
|
||||
|
||||
private
|
||||
def generate_key
|
||||
self.key = SecureRandom.base64(16) unless self.key
|
||||
|
|
|
@ -5,8 +5,9 @@
|
|||
# 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
|
||||
# origin :string not null
|
||||
# message :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
|
@ -15,6 +16,7 @@
|
|||
class Transaction < ActiveRecord::Base
|
||||
belongs_to :debtor, class_name: 'User'
|
||||
belongs_to :creditor, class_name: 'User'
|
||||
belongs_to :issuer, polymorphic: true
|
||||
|
||||
after_save :recalculate_balances
|
||||
after_destroy :recalculate_balances
|
||||
|
@ -22,10 +24,6 @@ class Transaction < ActiveRecord::Base
|
|||
validates :amount, numericality: { greater_than: 0 }
|
||||
validate :different_debtor_creditor
|
||||
|
||||
def client
|
||||
Client.find_by name: origin
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def recalculate_balances
|
||||
|
@ -34,6 +32,8 @@ class Transaction < ActiveRecord::Base
|
|||
end
|
||||
|
||||
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
|
||||
|
|
|
@ -17,6 +17,8 @@ class User < ActiveRecord::Base
|
|||
has_many :outgoing_transactions,
|
||||
class_name: 'Transaction', foreign_key: 'debtor_id'
|
||||
|
||||
has_many :issued_transactions, as: :issuer, class_name: 'Transaction'
|
||||
|
||||
validates :name, presence: true, uniqueness: true
|
||||
|
||||
def transactions
|
||||
|
|
|
@ -3,8 +3,8 @@ class CreateTransactions < ActiveRecord::Migration
|
|||
create_table :transactions do |t|
|
||||
t.references :debtor, 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.string :origin, null: false
|
||||
t.string :message
|
||||
|
||||
t.timestamps null: false
|
||||
|
|
|
@ -26,8 +26,9 @@ ActiveRecord::Schema.define(version: 20150908092731) do
|
|||
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.string "origin", null: false
|
||||
t.string "message"
|
||||
t.datetime "created_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", ["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|
|
||||
t.string "name"
|
||||
|
|
|
@ -5,8 +5,9 @@
|
|||
# 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
|
||||
# origin :string not null
|
||||
# message :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
|
@ -16,8 +17,8 @@ FactoryGirl.define do
|
|||
factory :transaction do
|
||||
association :debtor, factory: :user
|
||||
association :creditor, factory: :user
|
||||
issuer { debtor }
|
||||
amount { rand(100) }
|
||||
origin 'FactoryGirl'
|
||||
message { Faker::Lorem.sentence }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,8 +5,9 @@
|
|||
# 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
|
||||
# origin :string not null
|
||||
# message :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
|
|
Loading…
Reference in a new issue