Send proposals notifications to followers.

This commit is contained in:
taitus
2017-07-04 13:36:31 +02:00
committed by Senén Rodero Rodríguez
parent 35f7f11177
commit b29bf62f56
6 changed files with 135 additions and 7 deletions

View File

@@ -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
def notification_users
(@proposal.voters + @proposal.followers).uniq
end
end

View File

@@ -3,6 +3,7 @@ module Followable
included do
has_many :follows, as: :followable, dependent: :destroy
has_many :followers, through: :follows, source: :user
end
end

View File

@@ -7,7 +7,7 @@
<div class="callout primary">
<p>
<%= 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 %>
</p>

View File

@@ -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

View File

@@ -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

View File

@@ -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