FIX: `Array#first` with block will return the first element if nothing matches.

This commit is contained in:
Guo Xiang Tan 2016-02-24 22:34:40 +08:00
parent f259d7a7b9
commit e7cfbfeabb
2 changed files with 16 additions and 10 deletions

View File

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

View File

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