diff --git a/app/assets/javascripts/notifications.coffee b/app/assets/javascripts/notifications.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/notifications.coffee @@ -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/ diff --git a/app/assets/stylesheets/notifications.scss b/app/assets/stylesheets/notifications.scss new file mode 100644 index 0000000..c7ddf91 --- /dev/null +++ b/app/assets/stylesheets/notifications.scss @@ -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/ diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb new file mode 100644 index 0000000..9f86424 --- /dev/null +++ b/app/controllers/notifications_controller.rb @@ -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 diff --git a/app/controllers/requests_controller.rb b/app/controllers/requests_controller.rb index 3c606b2..57d68ad 100644 --- a/app/controllers/requests_controller.rb +++ b/app/controllers/requests_controller.rb @@ -1,11 +1,11 @@ class RequestsController < ApplicationController - load_and_authorize_resource :user, only: :index + load_and_authorize_resource :user, only: :index before_action :load_request, only: [:confirm, :decline] 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 diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb new file mode 100644 index 0000000..7342393 --- /dev/null +++ b/app/helpers/notifications_helper.rb @@ -0,0 +1,2 @@ +module NotificationsHelper +end diff --git a/app/models/notification.rb b/app/models/notification.rb new file mode 100644 index 0000000..ddad234 --- /dev/null +++ b/app/models/notification.rb @@ -0,0 +1,7 @@ +class Notification < ActiveRecord::Base + belongs_to :user + + def read! + update_attributes read: true + end +end diff --git a/app/models/request.rb b/app/models/request.rb index f6c33d7..5132b3d 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 65afd6a..28dfa11 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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' diff --git a/app/models/user_ability.rb b/app/models/user_ability.rb index 51fff98..49fe98b 100644 --- a/app/models/user_ability.rb +++ b/app/models/user_ability.rb @@ -6,7 +6,8 @@ class UserAbility can :manage, :all if user.penning? 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| t.debtor == user && t.amount <= Rails.application.config.maximum_amount end diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 249bd97..47a812d 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -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 diff --git a/app/views/notifications/_index.html.haml b/app/views/notifications/_index.html.haml new file mode 100644 index 0000000..7d52c26 --- /dev/null +++ b/app/views/notifications/_index.html.haml @@ -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 diff --git a/app/views/notifications/index.html.haml b/app/views/notifications/index.html.haml new file mode 100644 index 0000000..15012ea --- /dev/null +++ b/app/views/notifications/index.html.haml @@ -0,0 +1,2 @@ += render 'index', actions: true, title: 'Unread Notifications', notifications: @notifications[false] += render 'index', actions: false, title: 'Read Notifications', notifications: @notifications[true] diff --git a/config/routes.rb b/config/routes.rb index 9c68f28..bf9a17a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,10 +7,13 @@ Rails.application.routes.draw do resources :transactions, only: [:index, :create] resources :users, only: [:index, :show] do - resources :requests, only: [:index], shallow: true do + resources :requests, only: [:index], shallow: true 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" diff --git a/db/migrate/20170109150245_create_notifications.rb b/db/migrate/20170109150245_create_notifications.rb new file mode 100644 index 0000000..b8b3717 --- /dev/null +++ b/db/migrate/20170109150245_create_notifications.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 7bbf575..3441a84 100644 --- a/db/schema.rb +++ b/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 diff --git a/spec/controllers/notifications_controller_spec.rb b/spec/controllers/notifications_controller_spec.rb new file mode 100644 index 0000000..4212687 --- /dev/null +++ b/spec/controllers/notifications_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe NotificationsController, type: :controller do + +end diff --git a/spec/factories/notifications.rb b/spec/factories/notifications.rb new file mode 100644 index 0000000..23b8f71 --- /dev/null +++ b/spec/factories/notifications.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :notification do + + end + +end diff --git a/spec/helpers/notifications_helper_spec.rb b/spec/helpers/notifications_helper_spec.rb new file mode 100644 index 0000000..1473b13 --- /dev/null +++ b/spec/helpers/notifications_helper_spec.rb @@ -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 diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb new file mode 100644 index 0000000..bbdc6e0 --- /dev/null +++ b/spec/models/notification_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Notification, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end