From e7cfbfeabbdb4c184a6161549521a68c38c25f3c Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 24 Feb 2016 22:34:40 +0800 Subject: [PATCH] FIX: `Array#first` with block will return the first element if nothing matches. --- app/services/post_alerter.rb | 4 ++-- spec/services/post_alerter_spec.rb | 22 ++++++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb index 29b9ec1aa..ae507d4fe 100644 --- a/app/services/post_alerter.rb +++ b/app/services/post_alerter.rb @@ -261,8 +261,8 @@ class PostAlerter topic_title = post.topic.title # when sending a private message email, keep the original title - if post.topic.private_message? && modifications = post.revisions.map(&:modifications).to_a - if first_title_modification = modifications.first { |m| m.has_key?("title") } + if post.topic.private_message? && modifications = post.revisions.map(&:modifications) + if first_title_modification = modifications.find { |m| m.has_key?("title") } topic_title = first_title_modification["title"][0] end end diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb index 585d4551d..f4cf7699c 100644 --- a/spec/services/post_alerter_spec.rb +++ b/spec/services/post_alerter_spec.rb @@ -3,6 +3,7 @@ require 'rails_helper' describe PostAlerter do let!(:evil_trout) { Fabricate(:evil_trout) } + let(:user) { Fabricate(:user) } def create_post_with_alerts(args={}) post = Fabricate(:post, args) @@ -131,7 +132,6 @@ describe PostAlerter do context '@mentions' do - let(:user) { Fabricate(:user) } let(:mention_post) { create_post_with_alerts(user: user, raw: 'Hello @eviltrout')} let(:topic) { mention_post.topic } @@ -158,25 +158,31 @@ describe PostAlerter do end - describe ".create_notification" do + let(:topic) { Fabricate(:private_message_topic, user: user, created_at: 1.hour.ago) } + let(:post) { Fabricate(:post, topic: topic, created_at: 1.hour.ago) } + + it "creates a notification for PMs" do + post.revise(user, { raw: 'This is the revised post' }, revised_at: Time.zone.now) + + expect { + PostAlerter.new.create_notification(user, Notification.types[:private_message], post) + }.to change { user.notifications.count }.by(1) + + expect(user.notifications.last.data_hash["topic_title"]).to eq(topic.title) + end it "keeps the original title for PMs" do - user = Fabricate(:user) - topic = Fabricate(:private_message_topic, user: user, created_at: 1.hour.ago) - post = Fabricate(:post, topic: topic, created_at: 1.hour.ago) - original_title = topic.title post.revise(user, { title: "This is the revised title" }, revised_at: Time.now) expect { PostAlerter.new.create_notification(user, Notification.types[:private_message], post) - }.to change { user.notifications.count } + }.to change { user.notifications.count }.by(1) expect(user.notifications.last.data_hash["topic_title"]).to eq(original_title) end - end end