From a0ddde16e90bd518e038b44ed99fad7d39fd8bd9 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 7 Jun 2016 22:03:14 +0200 Subject: [PATCH] adds notifications for proposals --- app/controllers/notifications_controller.rb | 2 +- .../proposal_notifications_controller.rb | 1 + app/helpers/notifications_helper.rb | 9 ++- app/models/notification.rb | 15 +++++ .../notifications/_notification.html.erb | 8 ++- config/locales/en.yml | 3 + config/locales/es.yml | 3 + spec/features/notifications_spec.rb | 58 +++++++++++++++++++ 8 files changed, 95 insertions(+), 4 deletions(-) diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index a4ec31b50..ab346e802 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -9,7 +9,7 @@ class NotificationsController < ApplicationController def show @notification = current_user.notifications.find(params[:id]) - redirect_to url_for(@notification.notifiable) + redirect_to url_for(@notification.linkable_resource) end def mark_all_as_read diff --git a/app/controllers/proposal_notifications_controller.rb b/app/controllers/proposal_notifications_controller.rb index f1249c707..adfa0fc21 100644 --- a/app/controllers/proposal_notifications_controller.rb +++ b/app/controllers/proposal_notifications_controller.rb @@ -11,6 +11,7 @@ class ProposalNotificationsController < ApplicationController @proposal = Proposal.find(notification_params[:proposal_id]) if @notification.save @proposal.voters.each do |voter| + Notification.add(voter.id, @notification) if voter.email_on_proposal_notification? Mailer.proposal_notification(@notification, voter).deliver_later end diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index 281163380..6ec5f1b9e 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -1,6 +1,13 @@ module NotificationsHelper 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 diff --git a/app/models/notification.rb b/app/models/notification.rb index 1cb500ccf..2e2bedc38 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -21,4 +21,19 @@ class Notification < ActiveRecord::Base Notification.create!(user_id: user_id, notifiable: notifiable) 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 \ No newline at end of file diff --git a/app/views/notifications/_notification.html.erb b/app/views/notifications/_notification.html.erb index 28def42f7..237912a10 100644 --- a/app/views/notifications/_notification.html.erb +++ b/app/views/notifications/_notification.html.erb @@ -1,9 +1,13 @@
  • <%= link_to notification do %>

    - <%= t("notifications.index.#{notification_action(notification)}", count: notification.counter) %> - <%= notification.notifiable.is_a?(Comment) ? notification.notifiable.commentable.title : notification.notifiable.title %> + + <%= t("notifications.index.#{notification_action(notification)}", + count: notification.counter) %> + + <%= notification.notifiable_title %>

    +

    <%= l notification.timestamp, format: :datetime %>

    <% end %>
  • \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 8905e0f00..d3bd68e93 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -223,6 +223,9 @@ en: other: There are %{count} new comments on empty_notifications: You don't have new notifications. 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: one: Someone replied to your comment on other: There are %{count} new replies to your comment on diff --git a/config/locales/es.yml b/config/locales/es.yml index 24da77968..243e6defa 100755 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -223,6 +223,9 @@ es: other: Hay %{count} comentarios nuevos en empty_notifications: No tienes notificaciones nuevas. 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: one: Hay una respuesta nueva a tu comentario en other: Hay %{count} nuevas respuestas a tu comentario en diff --git a/spec/features/notifications_spec.rb b/spec/features/notifications_spec.rb index 1ffc6a854..e16dedf37 100644 --- a/spec/features/notifications_spec.rb +++ b/spec/features/notifications_spec.rb @@ -149,6 +149,64 @@ feature "Notifications" do expect(page).to have_css ".notification", count: 0 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 scenario "mark a single notification as read" do