Skip to content

Commit

Permalink
Merge pull request #105 from robotmay/locale-fixes
Browse files Browse the repository at this point in the history
Bugfixes for comment notifications in wrong locale
  • Loading branch information
Robert May committed Jul 16, 2013
2 parents 4f68b87 + 398f28c commit 7f1a7e0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 21 deletions.
50 changes: 29 additions & 21 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,31 @@ class Comment < ActiveRecord::Base

after_create :notify
def notify
title = comment_thread.threadable.title.blank? ? I18n.t("untitled") : comment_thread.threadable.title

unless user == comment_thread.user
# Notify owner
notifications.create(
send_email: true,
user: comment_thread.user,
subject: I18n.t("comments.notifications.subject", user: user.name, on: title),
body: I18n.t("comments.notifications.body", user: user.name, on: title)
)
unless user == comment_thread.user
I18n.with_locale(comment_thread.user.locale) do
title = comment_thread.threadable.title.blank? ? I18n.t("untitled") : comment_thread.threadable.title

notifications.create(
send_email: true,
user: comment_thread.user,
subject: I18n.t("comments.notifications.subject", user: user.name, on: title),
body: I18n.t("comments.notifications.body", user: user.name, on: title)
)
end
end

# Notify replyee
if child? && parent.user != comment_thread.user
notifications.create(
user: parent.user,
subject: I18n.t("comments.notifications.reply.subject", user: user.name, on: title),
body: I18n.t("comments.notifications.reply.body", user: user.name, on: title)
)
I18n.with_locale(parent.user.locale) do
title = comment_thread.threadable.title.blank? ? I18n.t("untitled") : comment_thread.threadable.title

notifications.create(
user: parent.user,
subject: I18n.t("comments.notifications.reply.subject", user: user.name, on: title),
body: I18n.t("comments.notifications.reply.body", user: user.name, on: title)
)
end
end
end

Expand All @@ -52,13 +58,15 @@ def toggle_visibility
self.published = !published
saved = save

if saved && published
title = comment_thread.threadable.title.blank? ? I18n.t("untitled") : comment_thread.threadable.title
if saved && published && user != comment_thread.user
I18n.with_locale(user.locale) do
title = comment_thread.threadable.title.blank? ? I18n.t("untitled") : comment_thread.threadable.title

notifications.create(
user: user,
subject: I18n.t("comments.notifications.published.subject", on: title)
)
notifications.create(
user: user,
subject: I18n.t("comments.notifications.published.subject", on: title)
)
end
end

if child? && saved && published
Expand Down
60 changes: 60 additions & 0 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,67 @@
require 'spec_helper'

describe Comment do
let(:user) { User.make! }
let(:photograph) { Photograph.make!(user: User.make!) }
let(:comment_thread) { CommentThread.make!(user: user, threadable: photograph) }

it { should belong_to(:user) }
it { should belong_to(:comment_thread) }
it { should have_many(:notifications) }

[:user_id, :comment_thread_id, :body].each do |attr|
it { should validate_presence_of(attr) }
end

describe "scopes" do
let(:published_comment) {
Comment.make!(published: true, comment_thread: comment_thread, user: user)
}

it "returns published comments" do
Comment.published.should include(published_comment)
end
end

describe "notifications" do
let(:comment) { Comment.make(comment_thread: comment_thread) }
let(:notification) { Notification.make(notifiable: comment) }
let(:photo_user) { User.make(locale: "en") }
let(:comment_user) { User.make(locale: "fr") }


describe "#notify" do
it "creates a notification" do
comment.notifications.should_receive(:create)
comment.notify
end

describe "locales" do
let(:comment) { Comment.make(user: comment_user) }

it "uses the target's locale" do
I18n.should_receive(:with_locale).with(photo_user.locale)
comment.notify
end
end
end

describe "#toggle_visibility" do
before { comment.stub(:save) { true } }

it "creates a notification" do
comment.notifications.should_receive(:create)
comment.toggle_visibility
end

describe "locales" do
let(:comment) { Comment.make(user: comment_user) }

it "uses the target's locale" do
I18n.should_receive(:with_locale).with(comment_user.locale)
comment.toggle_visibility
end
end
end
end
end
2 changes: 2 additions & 0 deletions spec/support/blueprints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
end

Comment.blueprint do
user
comment_thread
body { Faker::Lorem.paragraph }
end

CommentThread.blueprint do
Expand Down

0 comments on commit 7f1a7e0

Please sign in to comment.