Streakable
Track consecutive day streaks on your ActiveRecord models. Fork of has_streak by Garrett Martin. This version has a different include interface and more options.
Installation
Add this line to your application's Gemfile:
gem 'streakable'
And then execute:
$ bundle
Or install it yourself as:
$ gem install streakable
Usage
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 add streakable to 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
user.streak(:posts, except_today: true)
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request