Skip to content

Commit

Permalink
Send email notification when creating a note
Browse files Browse the repository at this point in the history
  • Loading branch information
Malcolm Locke committed Oct 27, 2011
1 parent 4c4126d commit 1190ff0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/mailers/notifications.rb
Expand Up @@ -23,4 +23,15 @@ def rejected(story, rejected_by)
mail :to => story.owned_by.email, :from => rejected_by.email,
:subject => "[#{story.project.name}] #{rejected_by.name} REJECTED your story '#{story.title}'."
end

# Send notification to of a new note to the listed users
def new_note(note, notify_users)
@note = note
@story = note.story

@notify_emails = notify_users.map(&:email)

mail :to => @notify_emails, :from => @note.user.email,
:subject => "[#{@story.project.name}] New comment on '#{@story.title}'"
end
end
9 changes: 8 additions & 1 deletion app/models/note.rb
Expand Up @@ -9,7 +9,14 @@ class Note < ActiveRecord::Base
validates :note, :presence => true

def create_changeset
story.changesets.create! if story
story.changesets.create!

# Notify all stakeholders in the story, but not the user who made the
# note.
notify_users = story.notify_users
notify_users.delete(user)

Notifications.new_note(self, notify_users).deliver unless notify_users.empty?
end

# Defines the attributes and methods that are included when calling to_json
Expand Down
4 changes: 4 additions & 0 deletions app/views/notifications/new_note.text.erb
@@ -0,0 +1,4 @@
<%= @note.user.name %> added the following comment to the story
"<%= @story.title %>":

<%= @note.note %>
18 changes: 18 additions & 0 deletions test/unit/note_test.rb
Expand Up @@ -20,6 +20,24 @@ def setup
end
end

test "creating a note sends a notification" do
user = Factory.create(:user)
@project.users << user
@story.requested_by = user
assert_difference 'ActionMailer::Base.deliveries.count' do
Factory.create(:note, :story => @story, :user => @user)
end
assert_equal [user.email], ActionMailer::Base.deliveries.first.to
assert_equal [@user.email], ActionMailer::Base.deliveries.first.from
end

test "creating a note does not send a notification for the current user" do
assert_equal [@user], @story.notify_users
assert_no_difference 'ActionMailer::Base.deliveries.count' do
Factory.create(:note, :story => @story, :user => @user)
end
end

test "returns JSON" do
attrs = [
"id", "created_at", "updated_at", "user_id", "story_id", "note", "errors"
Expand Down

0 comments on commit 1190ff0

Please sign in to comment.