I have this class in a ruby on rails application
class ChartData
def self.user_logs_by_day(logs)
days_with_hours = Hash.new{|h, k| h[k] = Hash.new(&h.default_proc)}
logs.each do |log|
days_with_hours[log.user.name][log.log_date] = log.total_hours
end
days_with_hours
end
def self.project_logs_by_day(logs)
days_with_hours = Hash.new{|h, k| h[k] = Hash.new(&h.default_proc)}
logs.each do |log|
if !log.project.nil?
days_with_hours[log.project.name][log.log_date] = log.total_hours
else
days_with_hours[@default][log.log_date] = log.total_hours
end
end
days_with_hours
end
end
I want to make the two methods into one like this, because I want to use this method for many models.
class ChartData
def self.logs_by_day(logs, klass)
days_with_hours = Hash.new{|h, k| h[k] = Hash.new(&h.default_proc)}
logs.each do |log|
if !log.klass.nil?
days_with_hours[log.klass.name][log.log_date] = log.total_hours
else
days_with_hours[@default][log.log_date] = log.total_hours
end
end
days_with_hours
end
end
I want to pass the class or model into the method like this
ChartData.logs_by_day(logs, Project)
How can I handle the parameter Project in the model so it gets to be a instance method on the log?
A rather clumsy attempt
1.9.3p125 :026 > Project.name.downcase
=> "project"
Any better? Other refactor suggestions are welcome
Why do you want to pass the class? You’re not using it anywhere. Pass a symbol.
and then