Required:
- Install gems, use: "rake gems:install"
- Configure mail, create a file at "config/mailer.yml". Use example at
"config/mailer.yml.example"
Signed-off-by: Benjamin LAN-SUN-LUK <benjamin.lan-sun-luk(a)supinfo.com>
---
app/models/user_mailer.rb | 22 ++++++++-
.../user_mailer/no_activity_recorded.html.erb | 6 ++
app/views/user_mailer/no_task_recorded.html.erb | 6 ++
config/environment.rb | 14 ++++-
config/initializers/schedules.rb | 54
++++++++++++++++++++
config/mailer.yml | 6 --
config/mailer.yml.example | 8 +++
test/unit/user_mailer_test.rb | 21 +++++++-
8 files changed, 124 insertions(+), 13 deletions(-)
create mode 100644 app/views/user_mailer/no_activity_recorded.html.erb
create mode 100644 app/views/user_mailer/no_task_recorded.html.erb
create mode 100644 config/initializers/schedules.rb
delete mode 100644 config/mailer.yml
create mode 100644 config/mailer.yml.example
diff --git a/app/models/user_mailer.rb b/app/models/user_mailer.rb
index 73308af..bd42800 100644
--- a/app/models/user_mailer.rb
+++ b/app/models/user_mailer.rb
@@ -3,8 +3,28 @@ class UserMailer < ActionMailer::Base
#
def new_generated_password(user, new_password)
recipients user.email
- from "no_reply(a)projxp.org" # TODO Email must be edit
+ from MAIL_CONFIG[:from]
subject "Your password"
body :user => user, :new_password => new_password
end
+
+ # Send an email to a user to notify him that no activity has been
detected
+ # in his backlog.
+ #
+ def no_activity_recorded(user, backlog_items)
+ recipients user.email
+ from MAIL_CONFIG[:from]
+ subject "No activity has been detected"
+ body :user => user, :backlog_items => backlog_items
+ end
+
+ # Send an email to a user to notify him that no task has been detected
+ # in his backlog.
+ #
+ def no_task_recorded(user, backlog_items)
+ recipients user.email
+ from MAIL_CONFIG[:from]
+ subject "No task has been detected"
+ body :user => user, :backlog_items => backlog_items
+ end
end
diff --git a/app/views/user_mailer/no_activity_recorded.html.erb
b/app/views/user_mailer/no_activity_recorded.html.erb
new file mode 100644
index 0000000..8f7a005
--- /dev/null
+++ b/app/views/user_mailer/no_activity_recorded.html.erb
@@ -0,0 +1,6 @@
+Dear <%= @user.display_name %>,
+
+The following backlog items are active, but no tasks were entered against
them. Please take a moment to update them.
+<% @backlog_items.each do |backlog_item|%>
+<%= backlog_item.user_story.title %>
+<% end %>
diff --git a/app/views/user_mailer/no_task_recorded.html.erb
b/app/views/user_mailer/no_task_recorded.html.erb
new file mode 100644
index 0000000..a912ec1
--- /dev/null
+++ b/app/views/user_mailer/no_task_recorded.html.erb
@@ -0,0 +1,6 @@
+Dear <%= @user.display_name %>,
+
+The following backlog items are active, but tasks are empty. Please take a
moment to update them.
+<% @backlog_items.each do |backlog_item|%>
+<%= backlog_item.user_story.title %>
+<% end %>
diff --git a/config/environment.rb b/config/environment.rb
index af8a593..ed31242 100644
--- a/config/environment.rb
+++ b/config/environment.rb
@@ -72,6 +72,9 @@ Rails::Initializer.run do |config|
# Make Active Record use UTC-base instead of local time
# config.active_record.default_timezone = :utc
+
+ # Gems
+ config.gem 'openwferu-scheduler', :lib => 'openwfe/util/scheduler'
end
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
@@ -84,7 +87,12 @@
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
# Please complete the config file are locate at /config/mailer.yml
#
require "smtp_tls"
+mailer_file_path = "#{RAILS_ROOT}/config/mailer.yml"
-mailer_config = File.open("#{RAILS_ROOT}/config/mailer.yml")
-mailer_options = YAML.load(mailer_config)
-ActionMailer::Base.smtp_settings = mailer_options
+if File.exist?(mailer_file_path)
+ MAIL_CONFIG = YAML.load(File.open(mailer_file_path))
+ ActionMailer::Base.smtp_settings = MAIL_CONFIG[:account]
+else
+ raise "Mail configuration file not found at #{mailer_file_path}"
+end
+MAIL_CONFIG ||= {}
diff --git a/config/initializers/schedules.rb
b/config/initializers/schedules.rb
new file mode 100644
index 0000000..65d4984
--- /dev/null
+++ b/config/initializers/schedules.rb
@@ -0,0 +1,54 @@
+require 'fastthread'
+require 'openwfe/util/scheduler'
+include OpenWFE
+
+# Initialize a threads array
+threads = []
+
+threads << Thread.new do
+ scheduler = Scheduler.new
+ scheduler.start
+
+ # Do this everyday at 05:00 am
+ scheduler.schedule('0 5 * * *') do # TODO Time must be configurable
+
+ user_no_activity_on_backlog_items = {}
+ user_no_tasks_on_backlog_items = {}
+
+ BacklogItem.find_all_by_state(BacklogItem::STATE_ASSIGNED).each do
|backlog_item|
+ if backlog_item.tasks.empty?
+
+ backlog_item_owner = backlog_item.owner
+ user_no_tasks_on_backlog_items[backlog_item_owner] ||= []
+ user_no_tasks_on_backlog_items[backlog_item_owner] <<
backlog_item_owner
+
+ else
+
+ backlog_item.tasks.each do |task|
+ if task.created_at.to_date < Date.yesterday
+ backlog_item_owner = backlog_item.owner
+ user_no_activity_on_backlog_items[backlog_item_owner] ||= []
+ user_no_activity_on_backlog_items[backlog_item_owner] <<
backlog_item
+ end
+ end
+
+ end
+ end
+
+ # Send the e-mail
+
+ user_no_activity_on_backlog_items.each do |owner, backlog_items|
+ UserMailer.deliver_no_activity_recorded(owner, backlog_items)
+ end
+
+ user_no_tasks_on_backlog_items.each do |owner, backlog_items|
+ UserMailer.deliver_no_task_recorded(owner, backlog_items)
+ end
+
+ end
+
+ scheduler.join
+end
+
+# Run all threads
+threads.each { |thread| thread.run }
\ No newline at end of file
diff --git a/config/mailer.yml b/config/mailer.yml
deleted file mode 100644
index f62d51e..0000000
--- a/config/mailer.yml
+++ /dev/null
@@ -1,6 +0,0 @@
----
- :address:
smtp.gmail.com
- :port: 587
- :user_name: username(a)gmail.com
- :password: password
- :authentication: :plain
diff --git a/config/mailer.yml.example b/config/mailer.yml.example
new file mode 100644
index 0000000..66644a9
--- /dev/null
+++ b/config/mailer.yml.example
@@ -0,0 +1,8 @@
+---
+ :account:
+ :address:
smtp.gmail.com
+ :port: 587
+ :user_name: username(a)gmail.com
+ :password: password
+ :authentication: :plain
+ :from: no-reply(a)projxp.org
\ No newline at end of file
diff --git a/test/unit/user_mailer_test.rb b/test/unit/user_mailer_test.rb
index d68a249..7d5fc15 100644
--- a/test/unit/user_mailer_test.rb
+++ b/test/unit/user_mailer_test.rb
@@ -2,8 +2,23 @@ require 'test_helper'
class UserMailerTest < ActionMailer::TestCase
tests UserMailer
- # TODO Test user_mailer
- def test_truth
- assert true
+
+ def setup
+ @user = User.new(:email => 'test(a)projxp.org', :display_name =>
'Test')
+ backlog_item = BacklogItem.new
+ backlog_item.user_story = UserStory.new(:title => 'Test')
+ @backlog_items = [backlog_item]
+ end
+
+ def test_new_generated_password
+ assert UserMailer.deliver_new_generated_password(@user, 'password')
+ end
+
+ def test_no_activity_recorded
+ assert UserMailer.deliver_no_activity_recorded(@user, @backlog_items)
+ end
+
+ def test_no_task_recorded
+ assert UserMailer.deliver_no_task_recorded(@user, @backlog_items)
end
end
--
1.5.2.4