Permalink
Browse files

Add email sending after an ticket update to all watcher of this ticket

  • Loading branch information...
1 parent f49e587 commit dd6c027f06f9ab0524726e6251fb3571e6711446 @shingara committed Mar 11, 2010
View
@@ -7,6 +7,8 @@
* Add the recover password configuration by your email
* Add remember solution when you logged
* Add information about number of ticket in search
+ * A logged user can watch a ticket. If user watch ticket he received email
+ when ticket is update
== 0.4.1 / 2010-02-14
View
@@ -128,7 +128,7 @@ def generate_update(ticket, user)
t.created_at = Time.now
t.write_event(self)
ticket_updates << t
- save
+ t.send_update_to_watchers if save
end
##
@@ -36,4 +36,11 @@ def to_param
num.to_s
end
+ # TODO: need test
+ def send_update_to_watchers
+ _root_document.watchers.each do |watcher|
+ UserMailer.deliver_ticket_update(_root_document.project, self, watcher)
+ end
+ end
+
end
View
@@ -0,0 +1,14 @@
+class UserMailer < ActionMailer::Base
+
+ helper :tickets
+
+ def ticket_update(project, ticket_update, watcher)
+ recipients watcher.email
+ root_ticket = ticket_update._root_document
+ subject "[#{project.name} ##{root_ticket.num}] #{root_ticket.title}"
+ body :ticket_update => ticket_update,
+ :project => project,
+ :root_ticket => root_ticket
+ end
+
+end
@@ -0,0 +1,27 @@
+<%= "#{@project.name} ##{@root_ticket.num}: #{@root_ticket.title}" %>
+<%= url_for(:controller => 'tickets', :action => 'show', :id => @root_ticket, :project_id => @project.id, :only_path => false) %>
+
+Resume :
+--------
+Author : <%= @root_ticket.creator_user_name %>
+Date of creation ; <%= @root_ticket.created_at %>
+State : <%= @root_ticket.state_name %>
+Priority : <%= @root_ticket.priority_name %>
+Tags : <%= @root_ticket.tag_list %>
+Milestone : <%= @root_ticket.milestone_name %>
+
+<%= @root_ticket.description %>
+
+Update :
+-------
+
+From : <%= @ticket_update.creator_user_name %> on <%= @ticket_update.created_at %>
+
+<% unless @ticket_update.properties_update.empty? -%>
+Change :
+<% @ticket_update.properties_update.each do |prop| -%>
+<%= update_field(prop) %>
+<% end -%>
+<% end -%>
+
+<%= @ticket_update.description %>
@@ -32,3 +32,4 @@
config.gem 'machinist_mongo', :version => '1.0.2', :lib => 'machinist/mongo_mapper'
config.gem 'webrat', :version => '0.5.3'
config.gem 'randexp'
+config.gem "email_spec"
View
@@ -59,7 +59,7 @@ def make_project(params={})
pr
end
-def make_ticket_update(ticket, params={}, user=User.make)
+def make_ticket_update(ticket, params={}, user=ticket.project.project_members.first.user)
ticket.generate_update(
({:tag_list => ticket.tag_list,
:state_id => ticket.state_id.to_s,
View
@@ -229,7 +229,6 @@
end
describe '#generate_update' do
-
def generate_ticket(ticket)
@t = Ticket.make(:project => Project.first || make_project,
:tag_list => TAG_LIST,
@@ -250,6 +249,17 @@ def generate_ticket(ticket)
end
end
+ it 'should send email to all user if update and watcher' do
+ ticket = generate_ticket({:description => 'start', 'tag_list' => TAG_LIST})
+ user = User.make
+ ticket.watchers.build(:user => user)
+ ticket.save
+ ticket.reload
+ UserMailer.should_receive(:deliver_ticket_update).once
+ ticket.generate_update({:description => 'new description',
+ 'tag_list' => TAG_LIST}, ticket.project.project_members.first.user)
+ end
+
describe 'change only description' do
before(:each) do
generate_ticket({:description => 'new description',
@@ -291,7 +301,6 @@ def generate_ticket(ticket)
it 'should generate ticket update with description' do
@t.ticket_updates[0].description.should == 'yahoo'
end
-
end
describe 'change state with description' do
@@ -346,7 +355,6 @@ def generate_ticket(ticket)
nil,
@priority.name])
end
-
end
describe 'about tag change' do
@@ -386,7 +394,6 @@ def generate_ticket(ticket)
# there are 2 events. One after creation one after update
end
end
-
end
describe 'self#get_by_permalink' do
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+describe UserMailer do
+ include EmailSpec::Helpers
+ include EmailSpec::Matchers
+ include ActionController::UrlWriter
+
+ describe '#ticket_update' do
+ before(:all) do
+ @ticket = make_ticket
+ ticket_update = make_ticket_update(@ticket)
+ @email = UserMailer.create_ticket_update(@ticket.project, ticket_update, Watcher.new(:email => 'shingara@gmail.com'))
+ end
+ it "should be set to be delivered to the email passed in" do
+ @email.should deliver_to("shingara@gmail.com")
+ end
+
+ it "should contain the user's message in the mail body" do
+ @email.should have_text(/#{@ticket.title}/)
+ end
+
+ it "should contain a link to ticket" do
+ url = url_for(:controller => 'tickets', :action => 'show', :id => @ticket.num, :project_id => @ticket.project.id,
+ :host => ActionMailer::Base.default_url_options[:host])
+ @email.should have_text(%r|#{url}|)
+ end
+
+ it "should have the correct subject" do
+ @email.should have_subject(/\[#{@ticket.project.title} ##{@ticket.num}\]/)
+ end
+
+ end
+end

0 comments on commit dd6c027

Please sign in to comment.