Code Monkey home page Code Monkey logo

streakable's Introduction

Gem Version Build Status Coverage Status Inline docs Maintainability MIT License Gem GitHub stars

Streakable Logo

Streakable is a Ruby gem to track consecutive day streaks ๐Ÿ“† on your Rails/ActiveRecord models. Hard fork of has_streak by Garrett Martin with a different include interface and more features. Requires Ruby >= 2.1 and ActiveRecord >= 3.2.22.

Github Project Page

Installation

Add this line to your application's Gemfile:

gem 'streakable'

And then execute:

$ bundle

Or install it directly with:

$ 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 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)

Find the longest streak, not just the current one:

user.streak(:posts, longest: true)

To get all of the streaks, not just the current one:

user.streaks(:posts)

TODO

  • Add class methods/scopes for calculating streaks on records not in memory

Specs

To run the specs for the currently running Ruby version, run bundle install and then bundle exec rspec. To run specs for every supported version of ActionPack, run bundle exec appraisal install and then bundle exec appraisal rspec.

Gem release

Make sure the specs pass, bump the version number in streakable.gemspec, build the gem with gem build streakable.gemspec. Commit your changes and push to Github, then tag the commit with the current release number using Github's Releases interface (use the format vx.x.x, where x is the semantic version number). You can pull the latest tags to your local repo with git pull --tags. Finally, push the gem with gem push streakable-version-number-here.gem.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b feature/my-new-feature) or bugfix branch (git checkout -b bugfix/my-helpful-bugfix)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin feature/my-new-feature)
  5. Make sure specs are passing (bundle exec rspec)
  6. Create new Pull Request

License

See the LICENSE file.

streakable's People

Contributors

georgebrock avatar sztheory avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

jayshreeanand

streakable's Issues

longest streak bug

Given the following streaks:

[["2013-01-14", "2013-01-13", "2013-01-12"], ["2013-01-10", "2013-01-09"]]

The current code used to determine the longest streak being:

streaks.sort.first.size

For the above case, the current code will return 2 (wrong) instead of 3 (correct), because it's sorting the array by its first values and not by its size.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.