Module: Streakable::InstanceMethods
- Defined in:
- lib/streakable.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#streak(association, column = :created_at, except_today: false, longest: false) ⇒ Object
Calculate a calendar streak.
-
#streaks(association, column = :created_at) ⇒ Object
Calculate all calendar streaks.
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
.
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.
63 64 65 |
# File 'lib/streakable.rb', line 63 def streaks(association, column=:created_at) build_streak(association, column, except_today: true).streaks end |