Merge branch 'api' of https://github.com/ZeusWPI/Tab into api

This commit is contained in:
benji 2015-09-09 12:59:12 +02:00
commit c3bf2b74cc
9 changed files with 19 additions and 18 deletions

View file

@ -1,7 +1,7 @@
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception. # Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead. # 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| rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, alert: exception.message redirect_to root_url, alert: exception.message

View file

@ -1,6 +1,4 @@
class TransactionsController < ApplicationController class TransactionsController < ApplicationController
skip_before_filter :verify_authenticity_token, :only => :create
def index def index
@transactions = Transaction.all @transactions = Transaction.all
end end

View file

@ -10,14 +10,11 @@
# #
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
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

View file

@ -5,8 +5,9 @@
# 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_type :string not null
# amount :integer default(0), not null # amount :integer default(0), not null
# origin :string 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
@ -15,6 +16,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 +24,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 +32,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

View file

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

View file

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

View file

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

View file

@ -5,8 +5,9 @@
# 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_type :string not null
# amount :integer default(0), not null # amount :integer default(0), not null
# origin :string 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
@ -16,8 +17,8 @@ FactoryGirl.define do
factory :transaction do factory :transaction do
association :debtor, factory: :user association :debtor, factory: :user
association :creditor, factory: :user association :creditor, factory: :user
issuer { debtor }
amount { rand(100) } amount { rand(100) }
origin 'FactoryGirl'
message { Faker::Lorem.sentence } message { Faker::Lorem.sentence }
end end
end end

View file

@ -5,8 +5,9 @@
# 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_type :string not null
# amount :integer default(0), not null # amount :integer default(0), not null
# origin :string 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