Merge pull request #4036 from tgxworld/fix_nil_class_error

FIX: `Array#first` with block will return the first element if nothin…
This commit is contained in:
Guo Xiang Tan 2016-02-24 23:22:59 +08:00
commit 84919ca43a
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