Merge branch 'master' into transaction-vc

This commit is contained in:
Felix Van der Jeugt 2015-09-08 19:12:51 +02:00
commit 19a6f4b921
15 changed files with 107 additions and 59 deletions

View file

@ -78,7 +78,7 @@ group :development do
end end
group :production do group :production do
gem 'mysql2' gem 'mysql2', '~> 0.3.0'
end end
gem 'high_voltage', '~> 2.4.0' gem 'high_voltage', '~> 2.4.0'

View file

@ -268,7 +268,7 @@ DEPENDENCIES
jbuilder (~> 2.0) jbuilder (~> 2.0)
jquery-datatables-rails jquery-datatables-rails
jquery-rails jquery-rails
mysql2 mysql2 (~> 0.3.0)
omniauth-oauth2 omniauth-oauth2
purecss-rails purecss-rails
rails (= 4.2.4) rails (= 4.2.4)

View file

@ -14,4 +14,5 @@
*= require dataTables/jquery.dataTables *= require dataTables/jquery.dataTables
*= require select2 *= require select2
*= require_self *= require_self
*= require purecss
*/ */

View file

@ -0,0 +1,9 @@
/*
=require purecss/base
=require purecss/buttons
=require purecss/forms
=require purecss/grids
=require purecss/grids-responsive
=require purecss/menus
=require purecss/tables
*/

View file

@ -2,4 +2,8 @@ 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: :exception
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, alert: exception.message
end
end end

View file

@ -1,2 +1,11 @@
class UsersController < ApplicationController class UsersController < ApplicationController
load_and_authorize_resource
def show
@user = User.find(params[:id])
end
def index
@users = User.all
end
end end

13
app/models/ability.rb Normal file
View file

@ -0,0 +1,13 @@
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.penning?
can :manage, :all
else
can :read, user, id: user.id
end
end
end

View file

@ -12,6 +12,9 @@
class Client < ActiveRecord::Base class Client < ActiveRecord::Base
before_create :generate_key before_create :generate_key
validates :name, presence: true, uniqueness: true
validates :key, presence: true, uniqueness: true
def transactions def transactions
Transaction.where(origin: name) Transaction.where(origin: name)
end end

View file

@ -19,13 +19,21 @@ class Transaction < ActiveRecord::Base
after_save :recalculate_balances after_save :recalculate_balances
after_destroy :recalculate_balances after_destroy :recalculate_balances
validates :amount, numericality: { greater_than: 0 }
validate :different_debtor_creditor
def client def client
Client.find_by name: origin Client.find_by name: origin
end end
private private
def recalculate_balances def recalculate_balances
creditor.calculate_balance! creditor.calculate_balance!
debtor.calculate_balance! debtor.calculate_balance!
end end
def different_debtor_creditor
self.errors.add :base, "Can't write money to yourself" if self.debtor == self.creditor
end
end end

View file

@ -17,6 +17,9 @@ 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'
validates :name, presence: true, uniqueness: true
validates :balance, presence: true
def transactions def transactions
Transaction.where("creditor_id = ? OR debtor_id = ?", id, id) Transaction.where("creditor_id = ? OR debtor_id = ?", id, id)
end end

View file

@ -7,4 +7,5 @@
= javascript_include_tag 'application', 'data-turbolinks-track' => true = javascript_include_tag 'application', 'data-turbolinks-track' => true
= csrf_meta_tags = csrf_meta_tags
%body %body
= content_tag :div, flash[:alert] if flash[:alert]
= yield = yield

View file

@ -0,0 +1,20 @@
<h2>Users</h2>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.id %></td>
<td><%= user.name %></td>
<td><%= user.balance %></td>
</tr>
<% end %>
</tbody>
</table>

View file

@ -0,0 +1,28 @@
<h2><%= @user.name %></h2>
<table>
<thead>
<tr>
<th>ID</th>
<th>Debtor</th>
<th>Creditor</th>
<th>Amount</th>
<th>Origin</th>
<th>Message</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<% @user.transactions.each do |transaction| %>
<tr>
<td><%= transaction.id %></td>
<td><%= transaction.debtor.name %></td>
<td><%= transaction.creditor.name %></td>
<td><%= transaction.amount %></td>
<td><%= transaction.origin %></td>
<td><%= transaction.message %></td>
<td><%= transaction.created_at %></td>
</tr>
<% end %>
</tbody>
</table>

View file

@ -6,59 +6,5 @@ Rails.application.routes.draw do
root to: 'high_voltage/pages#show', id: "landing" root to: 'high_voltage/pages#show', id: "landing"
resources :transactions, only: [:new, :index, :create] resources :transactions, only: [:new, :index, :create]
resources :users, only: [:show, :index]
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end end

View file

@ -1,13 +1,16 @@
class CreateTransactions < ActiveRecord::Migration class CreateTransactions < ActiveRecord::Migration
def change def change
create_table :transactions do |t| create_table :transactions do |t|
t.references :debtor, index: true, foreign_key: true, null: false t.references :debtor, index: true, null: false
t.references :creditor, index: true, foreign_key: true, null: false t.references :creditor, 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 :origin, null: false
t.string :message t.string :message
t.timestamps null: false t.timestamps null: false
end end
add_foreign_key :transactions, :users, column: :creditor_id
add_foreign_key :transactions, :users, column: :debtor_id
end end
end end