From cbb3284622be6f4809b12bc3ffc3ec7375e60132 Mon Sep 17 00:00:00 2001 From: benji Date: Tue, 8 Sep 2015 15:20:28 +0200 Subject: [PATCH 1/9] Install purecss --- app/assets/stylesheets/application.css | 1 + app/assets/stylesheets/purecss.css | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 app/assets/stylesheets/purecss.css diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 28c16f8..f03067c 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -13,4 +13,5 @@ *= require_tree . *= require dataTables/jquery.dataTables *= require_self + *= require purecss */ diff --git a/app/assets/stylesheets/purecss.css b/app/assets/stylesheets/purecss.css new file mode 100644 index 0000000..a8615b6 --- /dev/null +++ b/app/assets/stylesheets/purecss.css @@ -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 +*/ From 81d7b40190432a50883e42698322eca12589c814 Mon Sep 17 00:00:00 2001 From: Tom Naessens Date: Tue, 8 Sep 2015 15:22:26 +0200 Subject: [PATCH 2/9] Bundle update --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 13ddcb2..c9ade6e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -117,7 +117,7 @@ GEM multi_json (1.11.2) multi_xml (0.5.5) multipart-post (2.0.0) - mysql2 (0.3.20) + mysql2 (0.4.0) net-scp (1.2.1) net-ssh (>= 2.6.5) net-ssh (2.9.2) From 9cbd5a141cc7ebd792bdddf959d37707ed4c1177 Mon Sep 17 00:00:00 2001 From: Tom Naessens Date: Tue, 8 Sep 2015 15:24:13 +0200 Subject: [PATCH 3/9] Pin mysql again --- Gemfile | 2 +- Gemfile.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 660532a..9746c25 100644 --- a/Gemfile +++ b/Gemfile @@ -71,7 +71,7 @@ group :development do end group :production do - gem 'mysql2' + gem 'mysql2', '~> 0.3.0' end gem 'high_voltage', '~> 2.4.0' diff --git a/Gemfile.lock b/Gemfile.lock index fe7dbff..b4cde59 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -118,7 +118,7 @@ GEM multi_json (1.11.2) multi_xml (0.5.5) multipart-post (2.0.0) - mysql2 (0.4.0) + mysql2 (0.3.20) net-scp (1.2.1) net-ssh (>= 2.6.5) net-ssh (2.9.2) @@ -246,7 +246,7 @@ DEPENDENCIES jbuilder (~> 2.0) jquery-datatables-rails (~> 3.3.0) jquery-rails - mysql2 + mysql2 (~> 0.3.0) omniauth-oauth2 purecss-rails rails (= 4.2.4) From aadf6151229001c5659f91b353e83b4cad9afbf5 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 8 Sep 2015 15:37:02 +0200 Subject: [PATCH 4/9] fix transaction foreign keys --- db/migrate/20150908091546_create_transactions.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/db/migrate/20150908091546_create_transactions.rb b/db/migrate/20150908091546_create_transactions.rb index de40fa3..6fc8ad4 100644 --- a/db/migrate/20150908091546_create_transactions.rb +++ b/db/migrate/20150908091546_create_transactions.rb @@ -1,13 +1,16 @@ class CreateTransactions < ActiveRecord::Migration def change create_table :transactions do |t| - t.references :debtor, index: true, foreign_key: true, null: false - t.references :creditor, index: true, foreign_key: true, null: false + t.references :debtor, index: true, null: false + t.references :creditor, index: true, null: false t.integer :amount, null: false, default: 0 t.string :origin, null: false t.string :message t.timestamps null: false end + + add_foreign_key :transactions, :users, :creditor_id + add_foreign_key :transactions, :users, :debtor_id end end From ec4f0419869c574966c9f23be280df6114152320 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 8 Sep 2015 15:42:08 +0200 Subject: [PATCH 5/9] fix foreign keys in mysql --- db/migrate/20150908091546_create_transactions.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/migrate/20150908091546_create_transactions.rb b/db/migrate/20150908091546_create_transactions.rb index 6fc8ad4..dda51f7 100644 --- a/db/migrate/20150908091546_create_transactions.rb +++ b/db/migrate/20150908091546_create_transactions.rb @@ -10,7 +10,7 @@ class CreateTransactions < ActiveRecord::Migration t.timestamps null: false end - add_foreign_key :transactions, :users, :creditor_id - add_foreign_key :transactions, :users, :debtor_id + add_foreign_key :transactions, :users, column: :creditor_id + add_foreign_key :transactions, :users, column: :debtor_id end end From 599570f033581d03b317156c3383b247b1f05b62 Mon Sep 17 00:00:00 2001 From: benji Date: Tue, 8 Sep 2015 15:51:23 +0200 Subject: [PATCH 6/9] Add validations --- app/models/client.rb | 3 +++ app/models/transaction.rb | 8 ++++++++ app/models/user.rb | 3 +++ 3 files changed, 14 insertions(+) diff --git a/app/models/client.rb b/app/models/client.rb index bb8f315..bb674c5 100644 --- a/app/models/client.rb +++ b/app/models/client.rb @@ -12,6 +12,9 @@ class Client < ActiveRecord::Base before_create :generate_key + validates :name, presence: true, uniqueness: true + validates :key, presence: true, uniqueness: true + def transactions Transaction.where(origin: name) end diff --git a/app/models/transaction.rb b/app/models/transaction.rb index 6dd1bbb..2e6f346 100644 --- a/app/models/transaction.rb +++ b/app/models/transaction.rb @@ -19,13 +19,21 @@ class Transaction < ActiveRecord::Base after_save :recalculate_balances after_destroy :recalculate_balances + validates :amount, numericality: { greater_than: 0 } + validate :different_debtor_creditor + def client Client.find_by name: origin end private + def recalculate_balances creditor.calculate_balance! debtor.calculate_balance! end + + def different_debtor_creditor + self.errors.add :base, "Can't write money to yourself" if self.debtor == self.creditor + end end diff --git a/app/models/user.rb b/app/models/user.rb index 18f7582..f29c0af 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -17,6 +17,9 @@ class User < ActiveRecord::Base has_many :outgoing_transactions, class_name: 'Transaction', foreign_key: 'debtor_id' + validates :name, presence: true, uniqueness: true + validates :balance, presence: true + def transactions Transaction.where("creditor_id = ? OR debtor_id = ?", id, id) end From 31d5d81c9b1e6e43e7f071092a5329d46609b543 Mon Sep 17 00:00:00 2001 From: benji Date: Tue, 8 Sep 2015 17:18:38 +0200 Subject: [PATCH 7/9] Add ability of cancancan --- app/controllers/application_controller.rb | 4 ++++ app/models/ability.rb | 13 +++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 app/models/ability.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..3ea39cf 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,8 @@ 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 + + rescue_from CanCan::AccessDenied do |exception| + redirect_to root_url, alert: exception.message + end end diff --git a/app/models/ability.rb b/app/models/ability.rb new file mode 100644 index 0000000..f04be4f --- /dev/null +++ b/app/models/ability.rb @@ -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 From 380bce6ed603bab9d511fb906da3f6c3f812176a Mon Sep 17 00:00:00 2001 From: benji Date: Tue, 8 Sep 2015 17:28:46 +0200 Subject: [PATCH 8/9] Add index and show action for users --- app/controllers/users_controller.rb | 7 +++++++ app/views/users/index.html.erb | 20 ++++++++++++++++++++ app/views/users/show.html.erb | 28 ++++++++++++++++++++++++++++ config/routes.rb | 2 ++ 4 files changed, 57 insertions(+) create mode 100644 app/views/users/index.html.erb create mode 100644 app/views/users/show.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3e74dea..3b9390c 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,2 +1,9 @@ class UsersController < ApplicationController + def show + @user = User.find(params[:id]) + end + + def index + @users = User.all + end end diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb new file mode 100644 index 0000000..f673952 --- /dev/null +++ b/app/views/users/index.html.erb @@ -0,0 +1,20 @@ +

Users

+ + + + + + + + + + + <% @users.each do |user| %> + + + + + + <% end %> + +
IDNameBalance
<%= user.id %><%= user.name %><%= user.balance %>
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb new file mode 100644 index 0000000..6719a08 --- /dev/null +++ b/app/views/users/show.html.erb @@ -0,0 +1,28 @@ +

<%= @user.name %>

+ + + + + + + + + + + + + + + <% @user.transactions.each do |transaction| %> + + + + + + + + + + <% end %> + +
IDDebtorCreditorAmountOriginMessageTime
<%= transaction.id %><%= transaction.debtor.name %><%= transaction.creditor.name %><%= transaction.amount %><%= transaction.origin %><%= transaction.message %><%= transaction.created_at %>
diff --git a/config/routes.rb b/config/routes.rb index c800bae..c5ca861 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,8 @@ Rails.application.routes.draw do root to: 'high_voltage/pages#show', id: "landing" + 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". From 2f07856b67504cc0da9d031ce47c6bf212a49cb7 Mon Sep 17 00:00:00 2001 From: benji Date: Tue, 8 Sep 2015 17:40:40 +0200 Subject: [PATCH 9/9] Add authorization to user controller --- app/controllers/users_controller.rb | 2 ++ app/views/layouts/application.html.erb | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3b9390c..d191303 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,6 @@ class UsersController < ApplicationController + load_and_authorize_resource + def show @user = User.find(params[:id]) end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 1bf645f..376e9b4 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -8,7 +8,9 @@ -<%= yield %> + <%= content_tag :div, flash[:alert] if flash[:alert] %> + + <%= yield %>