Module: Streakable

Defined in:
lib/streakable.rb

Overview

Let’s say I have a User that has_many posts:

class User < ActiveRecord::Base
  has_many :posts
end

I want to track how many days in a row that each user wrote a post. I just have to include Streakable in the model:

class User < ActiveRecord::Base
  include Streakable
end

Now I can display the user’s streak:

user.streak(:posts) # => number of days in a row that this user wrote a post (as determined by the created_at column, by default)

The streak instance method can be called with any association:

user.streak(:other_association)

And you can change the column the streak is calculated on:

user.streak(:posts, :updated_at)

Don’t penalize the current day being absent when determining streaks (the User could write another Post before the day ends):

user.streak(:posts, except_today: true)

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



28
29
30
31
32
# File 'lib/streakable.rb', line 28

def self.included(klass)
  klass.class_eval do
    include InstanceMethods
  end
end