Explorar el Código

Notifications (#19)

* add notifications

* add notifications to User
Scott Christensen hace 6 años
padre
commit
f187fcd7c0

+ 7 - 0
app/controllers/notifications_controller.rb

@@ -0,0 +1,7 @@
+class NotificationsController < ApplicationController
+  before_action :authenticate_user!
+
+  def index
+    @notifications = Notification.where(recipient: current_user)
+  end
+end

+ 22 - 0
app/models/notification.rb

@@ -0,0 +1,22 @@
+class Notification < ApplicationRecord
+  belongs_to :recipient, class_name: "User"
+  belongs_to :actor, class_name: "User"
+  belongs_to :notifiable, polymorphic: true
+
+  def self.post(to:, from:, action:, notifiable:)
+    recipients = Array.wrap(to)
+    notifications = []
+
+    Notification.transaction do
+      notifications = recipients.uniq.each do |recipient|
+        Notification.create(
+          notifiable: notifiable,
+          action:     action,
+          recipient:  recipient,
+          actor:      from
+        )
+      end
+    end
+    notifications
+  end
+end

+ 2 - 0
app/models/user.rb

@@ -1,4 +1,6 @@
 class User < ApplicationRecord
+  has_many :notifications, foreign_key: :recipient_id
+  
   # Include default devise modules. Others available are:
   # :confirmable, :lockable, :timeoutable and :omniauthable
   devise :masqueradable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable

+ 7 - 0
app/views/notifications/index.html.erb

@@ -0,0 +1,7 @@
+<h1>Notifications</h1>
+
+<ul>
+  <% @notifications.each do |notification| %>
+    <li><%= render "#{notification.action}_to_current_user", actor: notification.actor, notifiable: notification.notifiable  %></li>
+  <% end %>
+</ul>

+ 6 - 0
app/views/shared/_navbar.html.erb

@@ -21,6 +21,12 @@
         <li class="nav-item"><%= link_to "What's New", announcements_path, class: "nav-link #{unread_announcements(current_user)}" %></li>
         <% if user_signed_in? %>
 
+        <li class="nav-item">
+          <%= link_to notifications_path, class: "nav-link" do %>
+            <span><i class="fa fa-flag-o" aria-hidden="true"></i></span>
+          <% end %>
+         </li>
+
           <li class="nav-item dropdown">
             <%= link_to root_path, id: "navbar-dropdown", class: "nav-link dropdown-toggle", data: { toggle: "dropdown" }, aria: { haspopup: true, expanded: false } do %>
               <%= image_tag gravatar_image_url(current_user.email, size: 40), height: 20, width: 20, class: "rounded" %>

+ 7 - 1
template.rb

@@ -93,6 +93,11 @@ def add_announcements
   route "resources :announcements, only: [:index]"
 end
 
+def add_notifications
+  generate "model Notification recipient_id:integer actor_id:integer read_at:datetime action:string notifiable_id:integer notifiable_type:string"
+  route "resources :notifications, only: [:index]"
+end
+
 def add_administrate
   generate "administrate:install"
 
@@ -108,7 +113,7 @@ def add_multiple_authentication
 
     insert_into_file "app/models/user.rb",
     ', :omniauthable',
-    after: '         :recoverable, :rememberable, :trackable, :validatable'    
+    after: '         :recoverable, :rememberable, :trackable, :validatable'
 
     generate "model Service user:references provider uid access_token access_token_secret refresh_token expires_at:datetime auth:text"
 end
@@ -123,6 +128,7 @@ after_bundle do
   add_foreman
   add_webpack
   add_announcements
+  add_notifications
   add_multiple_authentication
 
   # Migrate