Skip to content

Commit

Permalink
Merge pull request #54 from ZeusWPI/feature/50/cancel-request
Browse files Browse the repository at this point in the history
Allow the issuer of a request to cancel it (closes #50)
  • Loading branch information
redfast00 committed May 31, 2019
2 parents 91e9ae2 + f93e588 commit 7ccc2e5
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 14 deletions.
11 changes: 8 additions & 3 deletions app/controllers/requests_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class RequestsController < ApplicationController
load_and_authorize_resource :user, find_by: :name

before_action :load_request, only: [:confirm, :decline]
authorize_resource :request, id_param: :request_id, only: [:confirm, :decline]
before_action :load_request, only: [:confirm, :decline, :cancel]
authorize_resource :request, id_param: :request_id, only: [:confirm, :decline, :cancel]

def index
@requests = @user.incoming_requests.group_by(&:status)
@requests = @user.requests.group_by(&:status)
respond_to do |format|
format.html { }
format.json { render json: @requests }
Expand All @@ -22,6 +22,11 @@ def decline
redirect_to root_path
end

def cancel
@request.cancel!
redirect_to root_path
end

private

def load_request
Expand Down
15 changes: 14 additions & 1 deletion app/models/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class Request < ActiveRecord::Base
include BaseTransaction

enum status: [:open, :confirmed, :declined]
enum status: [:open, :confirmed, :declined, :cancelled]

def confirm!
return unless open?
Expand All @@ -36,6 +36,15 @@ def decline!
update_attributes status: :declined
end

def cancel!
return unless open?

Notification.create user: creditor, message: cancelled_message unless issuer == creditor
Notification.create user: debtor, message: cancelled_message unless issuer == debtor

update_attributes status: :cancelled
end

private

def confirmed_message
Expand All @@ -45,4 +54,8 @@ def confirmed_message
def declined_message
"#{debtor.name} refuses to pay €#{amount/100.0} for \"#{message}\"."
end

def cancelled_message
"#{issuer.name} cancelled the request to pay #{debtor.name}#{amount/100.0} for \"#{message}\" to #{creditor.name}."
end
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def transactions
Transaction.where('creditor_id = ?', id).or(Transaction.where('debtor_id = ?', id))
end

def requests
Request.where('debtor_id = ?', id).or(Request.where('creditor_id = ?', id).or(Request.where('issuer_id = ?', id)))
end

def calculate_balance!
balance = incoming_transactions.sum(:amount) -
outgoing_transactions.sum(:amount)
Expand Down
1 change: 1 addition & 0 deletions app/models/user_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def initialize(user)
can :manage, :all if user.penning?
can :create, Request, creditor_id: user.id
can [:confirm, :decline], Request, debtor_id: user.id
can :cancel, Request, issuer_id: user.id
can [:read, :reset_key, :add_registration_token], User, id: user.id
can :manage, Notification, user_id: user.id
can :create, Transaction do |t|
Expand Down
28 changes: 19 additions & 9 deletions app/views/requests/_index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@
%table.pure-table
%thead
%tr
%th Peer
%th Issuer
%th Creditor
%th Debtor
%th Amount
%th Message
- if actions
%th Accept
%th Decline
%th Actions
%tbody
- (requests || []).each do |r|
%tr
%td= r.creditor.name
%td= r.issuer.name
%td= r.creditor.name
%td= r.debtor.name
%td= r.amount_f
%td= r.message
- if actions
%td
= link_to request_confirm_path(r), method: :post do
%span.glyphicon.glyphicon-ok
%td
= link_to request_decline_path(r), method: :post do
%span.glyphicon.glyphicon-remove
- if can?(:confirm, r)
= link_to request_confirm_path(r), method: :post, class: "btn btn-success btn-xs" do
%i.glyphicon.glyphicon-ok
Accept
&nbsp;
- if can?(:decline, r)
= link_to request_decline_path(r), method: :post, class: "btn btn-danger btn-xs" do
%i.glyphicon.glyphicon-remove
Decline
&nbsp;
- if can?(:cancel, r)
= link_to request_cancel_path(r), method: :post, class: "btn btn-warning btn-xs" do
%i.glyphicon.glyphicon-trash
Cancel
3 changes: 2 additions & 1 deletion app/views/requests/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
= render 'index', actions: true, title: 'Open Requests', requests: @requests['open']
= render 'index', actions: false, title: 'Confirmed Requests', requests: @requests['confirmed']
= render 'index', actions: false, title: 'Confirmed Requests', requests: @requests['confirmed']
= render 'index', actions: false, title: 'Declined Requests', requests: @requests['declined']
= render 'index', actions: false, title: 'Cancelled Requests', requests: @requests['cancelled']
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
resources :requests, only: [:index], shallow: true do
post :confirm
post :decline
post :cancel
end
resources :notifications, only: [:index], shallow: true do
post :read
Expand Down

0 comments on commit 7ccc2e5

Please sign in to comment.