Browse Source

Fixed bug in user credits available

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.
denney-fix-saving-dates
Jason Denney 11 years ago
parent
commit
894423e66f
  1. 13
      app/models/user.rb

13
app/models/user.rb

@ -74,6 +74,8 @@ class User < ActiveRecord::Base
def total_earned_credits def total_earned_credits
log_action = ::ActsAsLoggable::UserAction.find_by_action("CHECKIN") 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 # 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 # 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 # The DISTINCT ON, and ORDER BY are important to getting the
# single conversion rate that applies to the respective log. # 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, SELECT DISTINCT ON (logs.created_at) start_date, end_date,
conversion.conversion, conversion.created_at conversion.conversion, conversion.created_at
FROM logs FROM logs
@ -90,10 +92,13 @@ class User < ActiveRecord::Base
SELECT conversion, created_at SELECT conversion, created_at
FROM credit_conversions FROM credit_conversions
) AS conversion ON logs.created_at > conversion.created_at ) 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 logs.loggable_type = 'User'
AND (log_action_id != #{log_action.id} AND log_action_type = '#{log_action.class.to_s}') AND (log_action_id IN (:credit_actions) AND log_action_type = :log_action_type)
ORDER BY logs.created_at, conversion.created_at DESC"). 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) sum{ |l| ((l.end_date - l.start_date)/3600) * l.conversion.to_i}.round(2)
end end

Loading…
Cancel
Save