diff --git a/app/models/comment.rb b/app/models/comment.rb index 3533f8c4..2fcd1f1d 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 @@ -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 diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index a42705f2..175c362d 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -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 diff --git a/spec/support/blueprints.rb b/spec/support/blueprints.rb index bec51049..f2eef864 100644 --- a/spec/support/blueprints.rb +++ b/spec/support/blueprints.rb @@ -6,7 +6,9 @@ end Comment.blueprint do + user comment_thread + body { Faker::Lorem.paragraph } end CommentThread.blueprint do