Created
June 20, 2016 14:20
-
-
Save k41n/0cb4f9d42c94ef357a1c80ca4f97844a to your computer and use it in GitHub Desktop.
Tasks report script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/local/bin/ruby | |
| class AbstractTask < ActiveRecord::Base | |
| self.table_name = 'tasks' | |
| self.inheritance_column = nil | |
| has_many :owners, through: :task_owners, source: :user | |
| has_many :task_owners, dependent: :destroy, foreign_key: 'task_id' | |
| has_many :work_logs, -> { order('started_at asc') }, dependent: :destroy, foreign_key: 'task_id' | |
| COMPANY_ID = 953 | |
| LAST_WEEK = (Time.now - 1.week)..Time.now | |
| REPORT_STATUSES = {0 => :open, 1 => :closed, 2 => :high, 3 => :invalid, 4 => :duplicate} | |
| REPORT_PRIORITY = {0 => :critical, 1 => :urgent, 2 => :high, 3 => :normal, 4 => :low, 5 => :lowest} | |
| scope :opened, -> { where(status: REPORT_STATUSES.key(:open), company_id: COMPANY_ID, created_at: LAST_WEEK)} | |
| scope :closed, -> { where(status: REPORT_STATUSES.key(:closed), company_id: COMPANY_ID, completed_at: LAST_WEEK)} | |
| scope :other, -> { where(company_id: COMPANY_ID, completed_at: nil).where.not(created_at: LAST_WEEK) } | |
| end | |
| class TaskRecord < AbstractTask | |
| end | |
| class TaskUser < ActiveRecord::Base | |
| belongs_to :user | |
| belongs_to :task, class_name: 'AbstractTask' | |
| end | |
| class TaskOwner < TaskUser | |
| end | |
| class User < ActiveRecord::Base | |
| has_many :tasks, through: :task_owners, class_name: 'TaskRecord' | |
| has_many :task_owners, dependent: :destroy | |
| has_many :work_logs | |
| end | |
| class WorkLog < ActiveRecord::Base | |
| belongs_to :task, class_name: 'AbstractTask', foreign_key: 'task_id' | |
| belongs_to :user, class_name: 'User', foreign_key: 'user_id' | |
| end | |
| erb = ERB.new(File.open("#{__dir__}/tasks.html.erb").read) | |
| ActionMailer::Base.mail(from: 'support@ish.com.au', | |
| to:'user@example.com.au', | |
| subject: 'Task Report', | |
| body: erb.result).deliver_now | |
| puts 'Report was sent.' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div style="margin-left: 400px; margin-right: 400px"> | |
| <h2 style="color: #660000; text-decoration: underline;">Recently opened</h2> | |
| <table> | |
| <% AbstractTask.opened.each do |task| %> | |
| <div><h3><%= task.try(:name) %></h3></div> | |
| <div><h5><%= task.try(:description) %></h5></div> | |
| <table width="100%" border="0" cellpadding="0" cellspacing="5"> | |
| <tbody> | |
| <tr> | |
| <td width="20%"><span style="color:#666666">Tasks </span><a href="<%=ENV['SITE_DOMAIN']%>/tasks/<%=task.id%>">#<%=task.id%></a></td> | |
| <td width="80%"><span style="color:#666666">Priority </span><%= AbstractTask::REPORT_PRIORITY[task.priority].capitalize %></td> | |
| </tr> | |
| <tr> | |
| <td><span style="color:#666666">Resolution </span><%= AbstractTask::REPORT_STATUSES[task.status].capitalize %></td> | |
| <% if task.owners.any? %> | |
| <td><span style="color:#666666">Opened </span><%= task.owners.first.try(:name) %> - <%= task.try(:created_at).strftime("%d %b %Y") %></td> | |
| <% end %> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <% if task.work_logs.any? %> | |
| <table width="100%" border="0" cellpadding="0" cellspacing="5"> | |
| <tbody> | |
| <tr> | |
| <td width="80%"><span style="color:#666666">Last comment </span><%= task.work_logs.last.user.try(:name) %> | |
| <%= task.work_logs.last.try(:started_at).strftime("%d %b %Y") %></td> | |
| <td></td> | |
| </tr> | |
| <tr> | |
| <td><%= task.work_logs.last.try(:body) %></td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <% end %> | |
| <hr> | |
| <% end %> | |
| </table> | |
| <h2 style="color: #660000; text-decoration: underline;">Recently closed</h2> | |
| <table> | |
| <% AbstractTask.closed.each do |task| %> | |
| <div><h3><%= task.try(:name) %></h3></div> | |
| <div><h5><%= task.try(:description) %></h5></div> | |
| <table width="100%" border="0" cellpadding="0" cellspacing="5"> | |
| <tbody> | |
| <tr> | |
| <td width="20%"><span style="color:#666666">Tasks </span><a href="<%=ENV['SITE_DOMAIN']%>/tasks/<%=task.id%>">#<%=task.id%></a></td> | |
| <td width="80%"><span style="color:#666666">Priority </span><%= AbstractTask::REPORT_PRIORITY[task.priority].capitalize %></td> | |
| </tr> | |
| <tr> | |
| <td><span style="color:#666666">Resolution </span><%= AbstractTask::REPORT_STATUSES[task.status].capitalize %></td> | |
| <% if task.owners.any? %> | |
| <td><span style="color:#666666">Opened </span><%= task.owners.first.try(:name) %> - <%= task.try(:created_at).strftime("%d %b %Y") %></td> | |
| <% end %> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <% if task.work_logs.any? %> | |
| <table width="100%" border="0" cellpadding="0" cellspacing="5"> | |
| <tbody> | |
| <tr> | |
| <td width="80%"><span style="color:#666666">Last comment </span><%= task.work_logs.last.user.try(:name) %> | |
| <%= task.work_logs.last.try(:started_at).strftime("%d %b %Y") %></td> | |
| <td></td> | |
| </tr> | |
| <tr> | |
| <td><%= task.work_logs.last.try(:body) %></td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <% end %> | |
| <hr> | |
| <% end %> | |
| </table> | |
| <h2 style="color: #660000; text-decoration: underline;">Other open tasks</h2> | |
| <table> | |
| <% AbstractTask.other.each do |task| %> | |
| <div><h3><%= task.try(:name) %></h3></div> | |
| <div><h5><%= task.try(:description) %></h5></div> | |
| <table width="100%" border="0" cellpadding="0" cellspacing="5"> | |
| <tbody> | |
| <tr> | |
| <td width="20%"><span style="color:#666666">Tasks </span><a href="<%=ENV['SITE_DOMAIN']%>/tasks/<%=task.id%>">#<%=task.id%></a></td> | |
| <td width="80%"><span style="color:#666666">Priority </span><%= AbstractTask::REPORT_PRIORITY[task.priority].capitalize %></td> | |
| </tr> | |
| <tr> | |
| <td><span style="color:#666666">Resolution </span><%= AbstractTask::REPORT_STATUSES[task.status].capitalize %></td> | |
| <% if task.owners.any? %> | |
| <td><span style="color:#666666">Opened </span><%= task.owners.first.try(:name) %> - <%= task.try(:created_at).strftime("%d %b %Y") %></td> | |
| <% end %> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <% if task.work_logs.any? %> | |
| <table width="100%" border="0" cellpadding="0" cellspacing="5"> | |
| <tbody> | |
| <tr> | |
| <td width="80%"><span style="color:#666666">Last comment </span><%= task.work_logs.last.user.try(:name) %> | |
| <%= task.work_logs.last.try(:started_at).strftime("%d %b %Y") %></td> | |
| <td></td> | |
| </tr> | |
| <tr> | |
| <td><%= task.work_logs.last.try(:body) %></td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <% end %> | |
| <hr> | |
| <% end %> | |
| </table> | |
| </div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment