From 894423e66fd55ed33dd23b78e5af4db5b4d86a01 Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Sat, 3 May 2014 21:41:33 -0400 Subject: [PATCH] Fixed bug in user credits available MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Personal work was counting toward credits, now it’s possible to add a column to user actions to denote if a user action can count to credits. --- app/models/user.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 59a468f..d51757b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -74,6 +74,8 @@ class User < ActiveRecord::Base def total_earned_credits log_action = ::ActsAsLoggable::UserAction.find_by_action("CHECKIN") + volunteer_id = 1 + staff_id = 3 # Find the first credit conversion which has a created_at date before the # log's created_at date and join it to the log's row so we can calculate @@ -82,7 +84,7 @@ class User < ActiveRecord::Base # # The DISTINCT ON, and ORDER BY are important to getting the # single conversion rate that applies to the respective log. - ::ActsAsLoggable::Log.find_by_sql(" + ::ActsAsLoggable::Log.find_by_sql([" SELECT DISTINCT ON (logs.created_at) start_date, end_date, conversion.conversion, conversion.created_at FROM logs @@ -90,10 +92,13 @@ class User < ActiveRecord::Base SELECT conversion, created_at FROM credit_conversions ) AS conversion ON logs.created_at > conversion.created_at - WHERE logs.loggable_id = #{self.id} + WHERE logs.loggable_id = :id AND logs.loggable_type = 'User' - AND (log_action_id != #{log_action.id} AND log_action_type = '#{log_action.class.to_s}') - ORDER BY logs.created_at, conversion.created_at DESC"). + AND (log_action_id IN (:credit_actions) AND log_action_type = :log_action_type) + ORDER BY logs.created_at, conversion.created_at DESC", + {id: self.id, + credit_actions: [volunteer_id, staff_id], + log_action_type: log_action.class.to_s}]). sum{ |l| ((l.end_date - l.start_date)/3600) * l.conversion.to_i}.round(2) end