Module: Streakable::InstanceMethods

Defined in:
lib/streakable.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#streak(association, column = :created_at, except_today: false, longest: false) ⇒ Object

Calculate a calendar streak. That is to say, the number of consecutive days that exist for some date column, on an asociation with this object.

For example if you have a User with many :posts, and one was created today that would be a streak of 1. If that user also created a Post yesterday, then the streak would be 2. If he created another Post the day before that one, he'd have a streak of 3, etc.

On the other hand imagine that the User hasn't created a Post yet today, but he did create one yesterday. Is that a streak? By default, it would be a streak of 0, so no. If you want to exclude the current day from this calculation, and count from yesterday, set except_today to true.

Parameters:

  • association (Symbol)

    the ActiveRecord association on the instance

  • column (Symbol) (defaults to: :created_at)

    the column on the association that you want to calculate the streak against.

  • except_today (Boolean) (defaults to: false)

    whether to include today in the streak length calculation or not. If this is true, then you are assuming there is still time today for the streak to be extended

  • longest (Boolean) (defaults to: false)

    if true, calculate the longest day streak in the sequence, not just the current one


52
53
54
# File 'lib/streakable.rb', line 52

def streak(association, column=:created_at, except_today: false, longest: false)
  build_streak(association, column, except_today: except_today).length(longest: longest)
end

#streaks(association, column = :created_at) ⇒ Object

Calculate all calendar streaks. Returns a list of Date arrays. That is to say, a listing of consecutive days counts that exist for some date column, on an asociation with this object.

Parameters:

  • association (Symbol)

    the ActiveRecord association on the instance

  • column (Symbol) (defaults to: :created_at)

    the column on the association that you want to calculate the streak against.

  • except_today (Boolean)

    whether to include today in the streak length calculation or not. If this is true, then you are assuming there is still time today for the streak to be extended

  • longest (Boolean)

    if true, calculate the longest day streak in the sequence, not just the current one


63
64
65
# File 'lib/streakable.rb', line 63

def streaks(association, column=:created_at)
  build_streak(association, column, except_today: true).streaks
end