diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 7d2bddb30..9a92a8d18 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -99,7 +99,7 @@ class Proposal < ActiveRecord::Base end def voters - votes_for.voters + User.active.where(id: votes_for.voters) end def editable? diff --git a/app/models/user.rb b/app/models/user.rb index 4bb09c4a0..60bc2364a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -53,6 +53,7 @@ class User < ActiveRecord::Base scope :for_render, -> { includes(:organization) } scope :by_document, -> (document_type, document_number) { where(document_type: document_type, document_number: document_number) } scope :email_digest, -> { where(email_digest: true) } + scope :active, -> { where(erased_at: nil) } before_validation :clean_document_number diff --git a/spec/features/proposal_notifications_spec.rb b/spec/features/proposal_notifications_spec.rb index 6092289f0..a10e41a7d 100644 --- a/spec/features/proposal_notifications_spec.rb +++ b/spec/features/proposal_notifications_spec.rb @@ -24,6 +24,44 @@ feature 'Proposal Notifications' do expect(page).to have_content "Please share it with others so we can make it happen!" end + scenario "Send a notification (Active voter)" do + author = create(:user) + proposal = create(:proposal, author: author) + + voter = create(:user, :level_two) + create(:vote, voter: voter, votable: proposal) + + create_proposal_notification(proposal) + + expect(Notification.count).to eq(1) + end + + scenario "Send a notification (Blocked voter)" do + author = create(:user) + proposal = create(:proposal, author: author) + + voter = create(:user, :level_two) + create(:vote, voter: voter, votable: proposal) + voter.block + + create_proposal_notification(proposal) + + expect(Notification.count).to eq(0) + end + + scenario "Send a notification (Erased voter)" do + author = create(:user) + proposal = create(:proposal, author: author) + + voter = create(:user, :level_two) + create(:vote, voter: voter, votable: proposal) + voter.erase + + create_proposal_notification(proposal) + + expect(Notification.count).to eq(0) + end + scenario "Show notifications" do proposal = create(:proposal) notification1 = create(:proposal_notification, proposal: proposal, title: "Hey guys", body: "Just wanted to let you know that...") diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index 4958204ec..247cd9e66 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -367,6 +367,50 @@ describe Proposal do end end + describe "voters" do + + it "returns users that have voted for the proposal" do + proposal = create(:proposal) + voter1 = create(:user, :level_two) + voter2 = create(:user, :level_two) + voter3 = create(:user, :level_two) + + create(:vote, voter: voter1, votable: proposal) + create(:vote, voter: voter2, votable: proposal) + + expect(proposal.voters).to include(voter1) + expect(proposal.voters).to include(voter2) + expect(proposal.voters).to_not include(voter3) + end + + it "does not return users that have been erased" do + proposal = create(:proposal) + voter1 = create(:user, :level_two) + voter2 = create(:user, :level_two) + + create(:vote, voter: voter1, votable: proposal) + create(:vote, voter: voter2, votable: proposal) + voter2.erase + + expect(proposal.voters).to include(voter1) + expect(proposal.voters).to_not include(voter2) + end + + it "does not return users that have been blocked" do + proposal = create(:proposal) + voter1 = create(:user, :level_two) + voter2 = create(:user, :level_two) + + create(:vote, voter: voter1, votable: proposal) + create(:vote, voter: voter2, votable: proposal) + voter2.block + + expect(proposal.voters).to include(voter1) + expect(proposal.voters).to_not include(voter2) + end + + end + describe "search" do context "attributes" do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index d54f3a42e..b6084a971 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -325,6 +325,34 @@ describe User do end + describe "scopes" do + + describe "active" do + + it "returns users that have not been erased" do + user1 = create(:user, erased_at: nil) + user2 = create(:user, erased_at: nil) + user3 = create(:user, erased_at: Time.now) + + expect(User.active).to include(user1) + expect(User.active).to include(user2) + expect(User.active).to_not include(user3) + end + + it "returns users that have not been blocked" do + user1 = create(:user) + user2 = create(:user) + user3 = create(:user) + user3.block + + expect(User.active).to include(user1) + expect(User.active).to include(user2) + expect(User.active).to_not include(user3) + end + + end + end + describe "self.search" do it "find users by email" do user1 = create(:user, email: "larry@consul.dev")