This patch supercedes the last one.

Required:
- Install gems, use: "rake gems:install"
- Configure mail, create a file at "config/mailer.yml". Use example at "config/mailer.yml.example"

If you want to use sendmail for the server. You have just to set "ActionMailer::Base.delivery_method = :sendmail" in "config/initializers/mailer.rb".

Signed-off-by: Benjamin LAN-SUN-LUK <benjamin.lan-sun-luk@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                              |   12 +---
 config/initializers/mailer.rb                      |   20 +++++++
 config/initializers/schedules.rb                   |   54 ++++++++++++++++++++
 config/mailer.yml                                  |    6 --
 config/mailer.yml.example                          |    9 +++
 test/unit/user_mailer_test.rb                      |   21 +++++++-
 9 files changed, 137 insertions(+), 19 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/mailer.rb
 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@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..e6685d5 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!(
@@ -79,12 +82,3 @@ ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
     :date_time12  => "%m/%d/%Y %I:%M%p",
     :date_time24  => "%m/%d/%Y %H:%M"
 )
-
-# Mail configuration.
-# Please complete the config file are locate at /config/mailer.yml
-#
-require "smtp_tls"
-
-mailer_config = File.open("#{RAILS_ROOT}/config/mailer.yml")
-mailer_options = YAML.load(mailer_config)
-ActionMailer::Base.smtp_settings = mailer_options
diff --git a/config/initializers/mailer.rb b/config/initializers/mailer.rb
new file mode 100644
index 0000000..f47c5cb
--- /dev/null
+++ b/config/initializers/mailer.rb
@@ -0,0 +1,20 @@
+# Mail configuration.
+# Please complete the config file that are locate at /config/mailer.yml
+#
+mailer_file_path = "#{RAILS_ROOT}/config/mailer.yml"
+
+# Defines a delivery method. Possible values are :smtp (default), :sendmail,
+# and :test.
+ActionMailer::Base.delivery_method = :smtp
+
+if File.exist?(mailer_file_path)
+
+  MAIL_CONFIG = YAML.load(File.open(mailer_file_path))
+  require "smtp_tls" if MAIL_CONFIG[:require_smtp_tls]
+  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..fb58101
--- /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
+
+      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@gmail.com
-  :password: password
-  :authentication: :plain
diff --git a/config/mailer.yml.example b/config/mailer.yml.example
new file mode 100644
index 0000000..d6a6f18
--- /dev/null
+++ b/config/mailer.yml.example
@@ -0,0 +1,9 @@
+---
+  :require_smtp_tls: false
+  :account:
+    :address: smtp.localhost.localdomain
+    :port: 25
+    :user_name: username
+    :password: password
+    :authentication: :login
+  :from: no-reply@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@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