adds notifications for proposals

This commit is contained in:
rgarcia
2016-06-07 22:03:14 +02:00
parent ea90869114
commit a0ddde16e9
8 changed files with 95 additions and 4 deletions

View File

@@ -9,7 +9,7 @@ class NotificationsController < ApplicationController
def show def show
@notification = current_user.notifications.find(params[:id]) @notification = current_user.notifications.find(params[:id])
redirect_to url_for(@notification.notifiable) redirect_to url_for(@notification.linkable_resource)
end end
def mark_all_as_read def mark_all_as_read

View File

@@ -11,6 +11,7 @@ class ProposalNotificationsController < ApplicationController
@proposal = Proposal.find(notification_params[:proposal_id]) @proposal = Proposal.find(notification_params[:proposal_id])
if @notification.save if @notification.save
@proposal.voters.each do |voter| @proposal.voters.each do |voter|
Notification.add(voter.id, @notification)
if voter.email_on_proposal_notification? if voter.email_on_proposal_notification?
Mailer.proposal_notification(@notification, voter).deliver_later Mailer.proposal_notification(@notification, voter).deliver_later
end end

View File

@@ -1,6 +1,13 @@
module NotificationsHelper module NotificationsHelper
def notification_action(notification) def notification_action(notification)
notification.notifiable_type == "Comment" ? "replies_to" : "comments_on" case notification.notifiable_type
when "ProposalNotification"
"proposal_notification"
when "Comment"
"replies_to"
else
"comments_on"
end
end end
end end

View File

@@ -21,4 +21,19 @@ class Notification < ActiveRecord::Base
Notification.create!(user_id: user_id, notifiable: notifiable) Notification.create!(user_id: user_id, notifiable: notifiable)
end end
end end
def notifiable_title
case notifiable.class.name
when "ProposalNotification"
notifiable.proposal.title
when "Comment"
notifiable.commentable.title
else
notifiable.title
end
end
def linkable_resource
notifiable.is_a?(ProposalNotification) ? notifiable.proposal : notifiable
end
end end

View File

@@ -1,9 +1,13 @@
<li id="<%= dom_id(notification) %>" class="notification"> <li id="<%= dom_id(notification) %>" class="notification">
<%= link_to notification do %> <%= link_to notification do %>
<p> <p>
<em><%= t("notifications.index.#{notification_action(notification)}", count: notification.counter) %></em> <em>
<strong><%= notification.notifiable.is_a?(Comment) ? notification.notifiable.commentable.title : notification.notifiable.title %></strong> <%= t("notifications.index.#{notification_action(notification)}",
count: notification.counter) %>
</em>
<strong><%= notification.notifiable_title %></strong>
</p> </p>
<p class="time"><%= l notification.timestamp, format: :datetime %></p> <p class="time"><%= l notification.timestamp, format: :datetime %></p>
<% end %> <% end %>
</li> </li>

View File

@@ -223,6 +223,9 @@ en:
other: There are %{count} new comments on other: There are %{count} new comments on
empty_notifications: You don't have new notifications. empty_notifications: You don't have new notifications.
mark_all_as_read: Mark all as read mark_all_as_read: Mark all as read
proposal_notification:
one: There is one new notification on
other: There are %{count} new notifications on
replies_to: replies_to:
one: Someone replied to your comment on one: Someone replied to your comment on
other: There are %{count} new replies to your comment on other: There are %{count} new replies to your comment on

View File

@@ -223,6 +223,9 @@ es:
other: Hay %{count} comentarios nuevos en other: Hay %{count} comentarios nuevos en
empty_notifications: No tienes notificaciones nuevas. empty_notifications: No tienes notificaciones nuevas.
mark_all_as_read: Marcar todas como leídas mark_all_as_read: Marcar todas como leídas
proposal_notification:
one: Hay una nueva notificación en
other: Hay %{count} nuevas notificaciones en
replies_to: replies_to:
one: Hay una respuesta nueva a tu comentario en one: Hay una respuesta nueva a tu comentario en
other: Hay %{count} nuevas respuestas a tu comentario en other: Hay %{count} nuevas respuestas a tu comentario en

View File

@@ -149,6 +149,64 @@ feature "Notifications" do
expect(page).to have_css ".notification", count: 0 expect(page).to have_css ".notification", count: 0
end end
context "Proposal notification" do
scenario "Voters should receive a notification", :js do
author = create(:user)
user1 = create(:user)
user2 = create(:user)
user3 = create(:user)
proposal = create(:proposal, author: author)
create(:vote, voter: user1, votable: proposal, vote_flag: true)
create(:vote, voter: user2, votable: proposal, vote_flag: true)
login_as(author)
visit root_path
visit new_proposal_notification_path(proposal_id: proposal.id)
fill_in 'proposal_notification_title', with: "Thank you for supporting my proposal"
fill_in 'proposal_notification_body', with: "Please share it with others so we can make it happen!"
click_button "Send message"
expect(page).to have_content "Your message has been sent correctly."
logout
login_as user1
visit root_path
find(".icon-notification").click
expect(page).to have_css ".notification", count: 1
expect(page).to have_content "There is one new notification on #{proposal.title}"
expect(page).to have_xpath "//a[@href='#{notification_path(Notification.last)}']"
logout
login_as user2
visit root_path
find(".icon-notification").click
expect(page).to have_css ".notification", count: 1
expect(page).to have_content "There is one new notification on #{proposal.title}"
expect(page).to have_xpath "//a[@href='#{notification_path(Notification.first)}']"
logout
login_as user3
visit root_path
find(".icon-no-notification").click
expect(page).to have_css ".notification", count: 0
end
pending "group notifications for the same proposal"
end
context "mark as read" do context "mark as read" do
scenario "mark a single notification as read" do scenario "mark a single notification as read" do