Class: SupplejackApi::UserActivity

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/supplejack_api/user_activity.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_from_user(daily_activity) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/supplejack_api/user_activity.rb', line 26

def self.build_from_user(daily_activity)
  user_activity = new
  daily_activity ||= {}

  %w(user_sets search records custom_searches).each do |group|
    group_activity = daily_activity[group]

    if group_activity
      user_activity[group] = group_activity
      user_activity.calculate_total_for(group)
    end
  end

  user_activity.calculate_total
  user_activity
end

Instance Method Details

#calculate_totalObject


56
57
58
59
60
61
62
63
# File 'app/models/supplejack_api/user_activity.rb', line 56

def calculate_total
  count = 0
  %w(user_sets search records custom_searches).each do |group|
    values = send(group)
    count += values['total'].to_i if values && values.is_a?(Hash)
  end
  self.total = count
end

#calculate_total_for(field) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/supplejack_api/user_activity.rb', line 43

def calculate_total_for(field)
  total = 0

  if activities = self[field]
    activities.each { |_action, count| total += count.to_i }
  else
    activities = {}
  end

  activities['total'] = total
  self[field] = activities
end