diff --git a/app/controllers/proposal_notifications_controller.rb b/app/controllers/proposal_notifications_controller.rb index 36e265e38..2ab2f811e 100644 --- a/app/controllers/proposal_notifications_controller.rb +++ b/app/controllers/proposal_notifications_controller.rb @@ -11,8 +11,8 @@ class ProposalNotificationsController < ApplicationController @notification = ProposalNotification.new(proposal_notification_params) @proposal = Proposal.find(proposal_notification_params[:proposal_id]) if @notification.save - @proposal.voters.each do |voter| - Notification.add(voter.id, @notification) + notification_users.each do |user| + Notification.add(user.id, @notification) end redirect_to @notification, notice: I18n.t("flash.actions.create.proposal_notification") else @@ -30,4 +30,8 @@ class ProposalNotificationsController < ApplicationController params.require(:proposal_notification).permit(:title, :body, :proposal_id) end -end \ No newline at end of file + def notification_users + (@proposal.voters + @proposal.followers).uniq + end + +end diff --git a/app/models/concerns/followable.rb b/app/models/concerns/followable.rb index 94d301e3e..51469698e 100644 --- a/app/models/concerns/followable.rb +++ b/app/models/concerns/followable.rb @@ -3,6 +3,7 @@ module Followable included do has_many :follows, as: :followable, dependent: :destroy + has_many :followers, through: :follows, source: :user end end diff --git a/app/views/proposal_notifications/new.html.erb b/app/views/proposal_notifications/new.html.erb index 678e00088..d75447c79 100644 --- a/app/views/proposal_notifications/new.html.erb +++ b/app/views/proposal_notifications/new.html.erb @@ -7,7 +7,7 @@

<%= t("proposal_notifications.new.info_about_receivers_html", - count: @proposal.voters.count, + count: (@proposal.voters + @proposal.followers).uniq.count, proposal_page: link_to(t("proposal_notifications.new.proposal_page"), proposal_path(@proposal, anchor: "comments"))).html_safe %>

diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index 2f095233a..a398c4d1f 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -503,7 +503,7 @@ es: collective: Colectivo flag: Denunciar como inapropiado follow: "Seguir" - follow_entity: "Seguir %{entity}" + follow_entity: "Seguir %{entity}: Podrás participar y recibir notificaciones de cualquier suceso relacionado." hide: Ocultar print: print_button: Imprimir esta información diff --git a/spec/features/notifications_spec.rb b/spec/features/notifications_spec.rb index b5a78c38c..a53024109 100644 --- a/spec/features/notifications_spec.rb +++ b/spec/features/notifications_spec.rb @@ -236,6 +236,60 @@ feature "Notifications" do expect(page).to have_css ".notification", count: 0 end + scenario "Followers should receive a notification", :js do + author = create(:user) + + user1 = create(:user) + user2 = create(:user) + user3 = create(:user) + + proposal = create(:proposal, author: author) + + create(:follow, :followed_proposal, user: user1, followable: proposal) + create(:follow, :followed_proposal, user: user2, followable: proposal) + + 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 + + notification_for_user1 = Notification.where(user: user1).first + 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_for_user1)}']" + + logout + login_as user2 + visit root_path + + find(".icon-notification").click + + notification_for_user2 = Notification.where(user: user2).first + 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_for_user2)}']" + + 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 diff --git a/spec/features/proposal_notifications_spec.rb b/spec/features/proposal_notifications_spec.rb index a10e41a7d..0e7a1c6f7 100644 --- a/spec/features/proposal_notifications_spec.rb +++ b/spec/features/proposal_notifications_spec.rb @@ -36,6 +36,33 @@ feature 'Proposal Notifications' do expect(Notification.count).to eq(1) end + scenario "Send a notification (Follower user)" do + author = create(:user) + proposal = create(:proposal, author: author) + user_follower = create(:user) + create(:follow, :followed_proposal, user: user_follower, followable: proposal) + + create_proposal_notification(proposal) + + expect(Notification.count).to eq(1) + end + + scenario "Send a notification (Follower user and Active voter)" do + author = create(:user) + proposal = create(:proposal, author: author) + + user_voter_follower = create(:user) + create(:follow, :followed_proposal, user: user_voter_follower, followable: proposal) + create(:vote, voter: user_voter_follower, votable: proposal) + + user_follower = create(:user) + create(:follow, :followed_proposal, user: user_follower, followable: proposal) + + create_proposal_notification(proposal) + + expect(Notification.count).to eq(2) + end + scenario "Send a notification (Blocked voter)" do author = create(:user) proposal = create(:proposal, author: author) @@ -76,7 +103,7 @@ feature 'Proposal Notifications' do expect(page).to have_content "We are almost there please share with your peoples!" end - scenario "Message about receivers" do + scenario "Message about receivers (Voters)" do author = create(:user) proposal = create(:proposal, author: author) @@ -89,6 +116,48 @@ feature 'Proposal Notifications' do expect(page).to have_link("the proposal's page", href: proposal_path(proposal, anchor: 'comments')) end + scenario "Message about receivers (Followers)" do + author = create(:user) + proposal = create(:proposal, author: author) + + 7.times { create(:follow, :followed_proposal, followable: proposal) } + + login_as(author) + visit new_proposal_notification_path(proposal_id: proposal.id) + + expect(page).to have_content "This message will be send to 7 people and it will be visible in the proposal's page" + expect(page).to have_link("the proposal's page", href: proposal_path(proposal, anchor: 'comments')) + end + + scenario "Message about receivers (Disctinct Followers and Voters)" do + author = create(:user) + proposal = create(:proposal, author: author) + + 7.times { create(:follow, :followed_proposal, followable: proposal) } + 7.times { create(:vote, votable: proposal, vote_flag: true) } + + login_as(author) + visit new_proposal_notification_path(proposal_id: proposal.id) + + expect(page).to have_content "This message will be send to 14 people and it will be visible in the proposal's page" + expect(page).to have_link("the proposal's page", href: proposal_path(proposal, anchor: 'comments')) + end + + scenario "Message about receivers (Same Followers and Voters)" do + author = create(:user) + proposal = create(:proposal, author: author) + + user_voter_follower = create(:user) + create(:follow, :followed_proposal, user: user_voter_follower, followable: proposal) + create(:vote, voter: user_voter_follower, votable: proposal) + + login_as(author) + visit new_proposal_notification_path(proposal_id: proposal.id) + + expect(page).to have_content "This message will be send to 1 people and it will be visible in the proposal's page" + expect(page).to have_link("the proposal's page", href: proposal_path(proposal, anchor: 'comments')) + end + context "Permissions" do scenario "Link to send the message" do @@ -144,4 +213,4 @@ feature 'Proposal Notifications' do end -end \ No newline at end of file +end