Instantly share code, notes, and snippets.

Embed
What would you like to do?
require "datagrid"
class String
def singular
singularize
end
end
class TimeEntriesByProjectReport
include Datagrid
scope do
TeamMember.select("team_members.*, sum(time_entries.hours) report_hours").joins(:time_entries).group("team_members.id")
end
attr_accessor :month, :year, :by_month
filter(:from_date, :date) do |value|
self.where(["time_entries.date >= ?", value])
end
filter(:to_date, :date) do |value|
self.where(["time_entries.date <= ?", value])
end
filter(:account_id, :enum, :multiple => true, :select => Account.all.map{|a| [a.name, a.id]}, :include_blank => false) do |values|
where(["time_entries.project_id in (?)", Project.where(:account_id => values).map(&:id)])
end
filter(:project_id, :enum, :multiple => true, :select => [], :include_blank => false) do |values|
where(["time_entries.project_id in (?)", values])
end
column(:name)
column(:title)
column(:report_hours, :header => "Working hours", :order => "report_hours")
column(:vacation_hours) do |team_member, report|
team_member.vacation_hours(report.from_date, report.to_date)
end
def initialize(attributes = {})
self.year = Date.today.year
self.month = Date.today.month
self.by_month = attributes && attributes.has_key?("by_month") ? attributes["by_month"] : true
super(attributes)
form_date
end
def form_date
if self.by_month?
month = self.month.to_i > 0 ? self.month.to_i : nil
year = self.year.to_i > 0 ? self.year.to_i : nil
self.from_date = Date.new(year, month)
self.to_date = self.from_date.end_of_month
end
end
def by_month?
["1", true, 1, "true"].include?(self.by_month)
end
def self.model_name
self.to_s.underscore
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment