Class: Decidim::ContinuityBadgeTracker

Inherits:
Object
  • Object
show all
Defined in:
app/services/decidim/continuity_badge_tracker.rb

Overview

This class keeps tabs on a user’s consecutive days streak so it can update the score of their continuity badge.

Instance Method Summary collapse

Constructor Details

#initialize(subject) ⇒ ContinuityBadgeTracker

Initializes the class with a polymorphic subject

subject - A in instance of a subclass of ActiveRecord::Base to be tracked



11
12
13
# File 'app/services/decidim/continuity_badge_tracker.rb', line 11

def initialize(subject)
  @subject = subject
end

Instance Method Details

#track!(date) ⇒ Object

Public: Tracks the past activity of a user to update the continuity badge’s score. It will set it to the amount of consecutive days a user has logged into the system.

date - The date of the last user’s activity. Usually ‘Time.zone.today`.

Returns nothing.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/services/decidim/continuity_badge_tracker.rb', line 22

def track!(date)
  @subject.with_lock do
    last_session_at = status.try(:last_session_at) || date
    current_streak = status.try(:current_streak) || 1

    streak = if last_session_at == date
               current_streak
             elsif last_session_at == date - 1.day
               current_streak + 1
             else
               1
             end

    update_status(date, streak)
    update_badge(streak)
  end
end