Add notifications about requests
This commit is contained in:
parent
4748625502
commit
4a631bc0c2
19 changed files with 121 additions and 5 deletions
3
app/assets/javascripts/notifications.coffee
Normal file
3
app/assets/javascripts/notifications.coffee
Normal 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/
|
3
app/assets/stylesheets/notifications.scss
Normal file
3
app/assets/stylesheets/notifications.scss
Normal 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/
|
21
app/controllers/notifications_controller.rb
Normal file
21
app/controllers/notifications_controller.rb
Normal 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
|
|
@ -5,7 +5,7 @@ class RequestsController < ApplicationController
|
|||
authorize_resource :request, only: [:confirm, :decline]
|
||||
|
||||
def index
|
||||
@requests = User.find(params[:user_id]).incoming_requests.group_by(&:status)
|
||||
@requests = @user.incoming_requests.group_by(&:status)
|
||||
end
|
||||
|
||||
def confirm
|
||||
|
|
2
app/helpers/notifications_helper.rb
Normal file
2
app/helpers/notifications_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
module NotificationsHelper
|
||||
end
|
7
app/models/notification.rb
Normal file
7
app/models/notification.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
class Notification < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
|
||||
def read!
|
||||
update_attributes read: true
|
||||
end
|
||||
end
|
|
@ -14,12 +14,16 @@ class Request < ActiveRecord::Base
|
|||
Transaction.create attributes.symbolize_keys.extract!(
|
||||
: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
|
||||
end
|
||||
|
||||
def decline!
|
||||
return unless open?
|
||||
|
||||
creditor.notifications.create message: "#{debtor.name} refuses to pay #{amount} for \"#{message}\"."
|
||||
update_attributes status: :declined
|
||||
end
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ class User < ActiveRecord::Base
|
|||
class_name: 'Request', foreign_key: 'debtor_id'
|
||||
has_many :outgoing_requests,
|
||||
class_name: 'Request', foreign_key: 'debtor_id'
|
||||
has_many :notifications
|
||||
|
||||
has_many :issued_transactions, as: :issuer, class_name: 'Transaction'
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ class UserAbility
|
|||
can :manage, :all if user.penning?
|
||||
can :read, user, id: user.id
|
||||
can :manage, Request, user_id: user.id
|
||||
can :manage, Notification, user_id: user.id
|
||||
can :create, Transaction do |t|
|
||||
t.debtor == user && t.amount <= Rails.application.config.maximum_amount
|
||||
end
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
=link_to "Zeus", User.zeus, class: "pure-menu-link"
|
||||
%li.pure-menu-item
|
||||
= 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
|
||||
= render 'partials/flash'
|
||||
= yield
|
||||
|
|
15
app/views/notifications/_index.html.haml
Normal file
15
app/views/notifications/_index.html.haml
Normal 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
|
2
app/views/notifications/index.html.haml
Normal file
2
app/views/notifications/index.html.haml
Normal 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]
|
|
@ -11,6 +11,9 @@ Rails.application.routes.draw do
|
|||
post :confirm
|
||||
post :decline
|
||||
end
|
||||
resources :notifications, only: [:index], shallow: true do
|
||||
post :read
|
||||
end
|
||||
end
|
||||
|
||||
get 'datatables/:id' => 'datatables#transactions_for_user', as: "user_transactions"
|
||||
|
|
11
db/migrate/20170109150245_create_notifications.rb
Normal file
11
db/migrate/20170109150245_create_notifications.rb
Normal 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
|
12
db/schema.rb
12
db/schema.rb
|
@ -11,7 +11,7 @@
|
|||
#
|
||||
# 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|
|
||||
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", ["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|
|
||||
t.integer "debtor_id", null: false
|
||||
t.integer "creditor_id", null: false
|
||||
|
|
5
spec/controllers/notifications_controller_spec.rb
Normal file
5
spec/controllers/notifications_controller_spec.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe NotificationsController, type: :controller do
|
||||
|
||||
end
|
6
spec/factories/notifications.rb
Normal file
6
spec/factories/notifications.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
FactoryGirl.define do
|
||||
factory :notification do
|
||||
|
||||
end
|
||||
|
||||
end
|
15
spec/helpers/notifications_helper_spec.rb
Normal file
15
spec/helpers/notifications_helper_spec.rb
Normal 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
|
5
spec/models/notification_spec.rb
Normal file
5
spec/models/notification_spec.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Notification, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
Loading…
Reference in a new issue