Add notifications about requests

This commit is contained in:
benji 2017-01-09 16:22:58 +01:00
parent 4748625502
commit 4a631bc0c2
19 changed files with 121 additions and 5 deletions

View file

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View file

@ -0,0 +1,3 @@
// Place all the styles related to the notifications controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View file

@ -0,0 +1,21 @@
class NotificationsController < ApplicationController
load_and_authorize_resource :user, only: :index
before_action :load_notification, only: :read
authorize_resource :notification, only: :read
def index
@notifications = @user.notifications.group_by(&:read)
end
def read
@notification.read!
redirect_to user_notifications_path(@notification.user)
end
private
def load_notification
@notification = Notification.find params[:notification_id]
end
end

View file

@ -5,7 +5,7 @@ class RequestsController < ApplicationController
authorize_resource :request, only: [:confirm, :decline] authorize_resource :request, only: [:confirm, :decline]
def index def index
@requests = User.find(params[:user_id]).incoming_requests.group_by(&:status) @requests = @user.incoming_requests.group_by(&:status)
end end
def confirm def confirm

View file

@ -0,0 +1,2 @@
module NotificationsHelper
end

View file

@ -0,0 +1,7 @@
class Notification < ActiveRecord::Base
belongs_to :user
def read!
update_attributes read: true
end
end

View file

@ -14,12 +14,16 @@ class Request < ActiveRecord::Base
Transaction.create attributes.symbolize_keys.extract!( Transaction.create attributes.symbolize_keys.extract!(
:debtor_id, :creditor_id, :issuer_id, :issuer_type, :amount, :message :debtor_id, :creditor_id, :issuer_id, :issuer_type, :amount, :message
) )
creditor.notifications.create message: "Your request for #{amount} for \"#{message}\" has been accepted by #{debtor.name}."
update_attributes status: :confirmed update_attributes status: :confirmed
end end
def decline! def decline!
return unless open? return unless open?
creditor.notifications.create message: "#{debtor.name} refuses to pay #{amount} for \"#{message}\"."
update_attributes status: :declined update_attributes status: :declined
end end

View file

@ -22,6 +22,7 @@ class User < ActiveRecord::Base
class_name: 'Request', foreign_key: 'debtor_id' class_name: 'Request', foreign_key: 'debtor_id'
has_many :outgoing_requests, has_many :outgoing_requests,
class_name: 'Request', foreign_key: 'debtor_id' class_name: 'Request', foreign_key: 'debtor_id'
has_many :notifications
has_many :issued_transactions, as: :issuer, class_name: 'Transaction' has_many :issued_transactions, as: :issuer, class_name: 'Transaction'

View file

@ -7,6 +7,7 @@ class UserAbility
can :manage, :all if user.penning? can :manage, :all if user.penning?
can :read, user, id: user.id can :read, user, id: user.id
can :manage, Request, user_id: user.id can :manage, Request, user_id: user.id
can :manage, Notification, user_id: user.id
can :create, Transaction do |t| can :create, Transaction do |t|
t.debtor == user && t.amount <= Rails.application.config.maximum_amount t.debtor == user && t.amount <= Rails.application.config.maximum_amount
end end

View file

@ -19,6 +19,8 @@
=link_to "Zeus", User.zeus, class: "pure-menu-link" =link_to "Zeus", User.zeus, class: "pure-menu-link"
%li.pure-menu-item %li.pure-menu-item
= link_to 'Requests', user_requests_path(current_user), class: 'pure-menu-link' = link_to 'Requests', user_requests_path(current_user), class: 'pure-menu-link'
%li.pure-menu-item
= link_to 'Notifications', user_notifications_path(current_user), class: 'pure-menu-link'
.pure-u-1 .pure-u-1
= render 'partials/flash' = render 'partials/flash'
= yield = yield

View file

@ -0,0 +1,15 @@
%h4= title
%table.pure-table
%thead
%tr
%th Message
- if actions
%th Mark as read
%tbody
- (notifications || []).each do |n|
%tr
%td= n.message
- if actions
%td
= link_to notification_read_path(n), method: :post do
%span.glyphicon.glyphicon-ok

View file

@ -0,0 +1,2 @@
= render 'index', actions: true, title: 'Unread Notifications', notifications: @notifications[false]
= render 'index', actions: false, title: 'Read Notifications', notifications: @notifications[true]

View file

@ -11,6 +11,9 @@ Rails.application.routes.draw do
post :confirm post :confirm
post :decline post :decline
end end
resources :notifications, only: [:index], shallow: true do
post :read
end
end end
get 'datatables/:id' => 'datatables#transactions_for_user', as: "user_transactions" get 'datatables/:id' => 'datatables#transactions_for_user', as: "user_transactions"

View file

@ -0,0 +1,11 @@
class CreateNotifications < ActiveRecord::Migration
def change
create_table :notifications do |t|
t.references :user, index: true, null: false
t.string :message
t.boolean :read, default: false
t.timestamps null: false
end
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170109123717) do ActiveRecord::Schema.define(version: 20170109150245) do
create_table "clients", force: :cascade do |t| create_table "clients", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
@ -23,6 +23,16 @@ ActiveRecord::Schema.define(version: 20170109123717) do
add_index "clients", ["key"], name: "index_clients_on_key" add_index "clients", ["key"], name: "index_clients_on_key"
add_index "clients", ["name"], name: "index_clients_on_name" add_index "clients", ["name"], name: "index_clients_on_name"
create_table "notifications", force: :cascade do |t|
t.integer "user_id", null: false
t.string "message"
t.boolean "read", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "notifications", ["user_id"], name: "index_notifications_on_user_id"
create_table "requests", force: :cascade do |t| create_table "requests", 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

View file

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe NotificationsController, type: :controller do
end

View file

@ -0,0 +1,6 @@
FactoryGirl.define do
factory :notification do
end
end

View file

@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the NotificationsHelper. For example:
#
# describe NotificationsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe NotificationsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end

View file

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Notification, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end