aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoachim Filip Ignacy Bartosik <jbartosik@gmail.com>2010-06-08 21:37:34 +0200
committerJoachim Filip Ignacy Bartosik <jbartosik@gmail.com>2010-06-09 12:09:10 +0200
commit8e8f126d91966884299d06d2bbaf7ba2c306a254 (patch)
treed45e92481507b388a79b53e0a7aa55aa9729ec4f
parentMark answer as unapproved if content was changed (diff)
downloadrecruiting-webapp-8e8f126d91966884299d06d2bbaf7ba2c306a254.tar.gz
recruiting-webapp-8e8f126d91966884299d06d2bbaf7ba2c306a254.tar.bz2
recruiting-webapp-8e8f126d91966884299d06d2bbaf7ba2c306a254.zip
Notify mentors about new answers of their recruits
-rw-r--r--app/models/answer.rb7
-rw-r--r--app/models/user_mailer.rb29
-rw-r--r--app/views/user_mailer/new_answer.erb3
-rw-r--r--config/environments/development.rb4
-rw-r--r--config/environments/test.rb3
-rw-r--r--spec/models/user_mailer_spec.rb23
-rw-r--r--spec/spec_helper.rb1
7 files changed, 62 insertions, 8 deletions
diff --git a/app/models/answer.rb b/app/models/answer.rb
index 76fc37d..406be4d 100644
--- a/app/models/answer.rb
+++ b/app/models/answer.rb
@@ -22,6 +22,8 @@ class Answer < ActiveRecord::Base
record.approved = false if record.content_changed?
end
+ after_create :notify_new_answer
+
multi_permission :update, :destroy do
(owned? && !reference && !approved) ||
(reference && acting_user.role.is_recruiter?) ||
@@ -52,4 +54,9 @@ class Answer < ActiveRecord::Base
User.user_is_recruiter?(acting_user)||
User.user_is_mentor_of?(acting_user, owner)
end
+
+ protected
+ def notify_new_answer
+ UserMailer.deliver_new_answer(owner.mentor, self)unless owner.mentor.nil?
+ end
end
diff --git a/app/models/user_mailer.rb b/app/models/user_mailer.rb
index f42ee62..89f44fd 100644
--- a/app/models/user_mailer.rb
+++ b/app/models/user_mailer.rb
@@ -1,14 +1,29 @@
class UserMailer < ActionMailer::Base
-
- def forgot_password(user, key)
- host = Hobo::Controller.request_host
- app_name = Hobo::Controller.app_name || host
- @subject = "#{app_name} -- forgotten password"
- @body = { :user => user, :key => key, :host => host, :app_name => app_name }
+ def common(user)
@recipients = user.email_address
- @from = "no-reply@#{host}"
+ @from = "no-reply@#{ ActionMailer::Base.default_url_options[:host] }"
@sent_on = Time.now
@headers = {}
end
+ def question_title(answer)
+ if answer.question.nil?
+ "none"
+ else
+ answer.question.title
+ end
+ end
+
+ def forgot_password(user, key)
+ common(user)
+ @subject = "#{@app_name} -- forgotten password"
+ @body = { :user => user, :key => key, :app_name => app_name }
+ end
+
+ def new_answer(user, answer)
+ common(user)
+ @subject = "New answer"
+ @body = { :question_title=> question_title(answer), :recruit_name =>
+ answer.owner.name, :id => answer.id}
+ end
end
diff --git a/app/views/user_mailer/new_answer.erb b/app/views/user_mailer/new_answer.erb
new file mode 100644
index 0000000..81222d0
--- /dev/null
+++ b/app/views/user_mailer/new_answer.erb
@@ -0,0 +1,3 @@
+Recruit you are mentoring - <%= @recruit_name %> answered question "<%= @question_title %>".
+
+<%= answer_url(@id) %>
diff --git a/config/environments/development.rb b/config/environments/development.rb
index 85c9a60..5478fb6 100644
--- a/config/environments/development.rb
+++ b/config/environments/development.rb
@@ -14,4 +14,6 @@ config.action_view.debug_rjs = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
-config.action_mailer.raise_delivery_errors = false \ No newline at end of file
+config.action_mailer.raise_delivery_errors = false
+
+config.action_mailer.default_url_options = { :host => 'localhost', :port => '3000' }
diff --git a/config/environments/test.rb b/config/environments/test.rb
index ec98184..7b551a9 100644
--- a/config/environments/test.rb
+++ b/config/environments/test.rb
@@ -30,3 +30,6 @@ config.action_mailer.delivery_method = :test
config.gem "rspec", :lib => false, :version => ">= 1.2.0"
config.gem "rspec-rails", :lib => false, :version => ">= 1.2.0"
config.gem 'shoulda', :lib => false
+config.gem 'email_spec', :lib => 'email_spec'
+
+config.action_mailer.default_url_options = { :host => 'localhost', :port => '3000' }
diff --git a/spec/models/user_mailer_spec.rb b/spec/models/user_mailer_spec.rb
new file mode 100644
index 0000000..94010e3
--- /dev/null
+++ b/spec/models/user_mailer_spec.rb
@@ -0,0 +1,23 @@
+require 'spec_helper.rb'
+describe UserMailer do
+ include EmailSpec::Helpers
+ include EmailSpec::Matchers
+
+ fixtures :users, :questions, :answers
+
+ before (:each) do
+ @recruit = users(:ron)
+ @question = questions(:banana)
+ @answer = answers(:banana)
+ end
+
+ it "should prepare proper new answer notification" do
+ notification = UserMailer.create_new_answer(@recruit.mentor, @answer)
+ notification.should deliver_to(@recruit.mentor.email_address)
+ notification.should deliver_from("no-reply@localhost")
+ notification.should have_text(/Recruit you are mentoring - #{@recruit.name}/)
+ notification.should have_text(/answered question "#{@answer.question.title}"/)
+ notification.should have_text(/http:\/\/localhost:3000\/answers\/#{@answer.id}/)
+ notification.should have_subject('New answer')
+ end
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 062d649..bda66e4 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -5,6 +5,7 @@ require File.expand_path(File.join(File.dirname(__FILE__),'..','config','environ
require 'spec/autorun'
require 'spec/rails'
require 'shoulda'
+require "email_spec"
# Uncomment the next line to use webrat's matchers
#require 'webrat/integrations/rspec-rails'