Giter Site home page Giter Site logo

season's Introduction

Season

Gem Version Code Climate Build Status

Season automatically creates scopes for your models' datetime columns. How many times have you done ugly things like where("users.created_at" > ?", some_date)? If you ever built a REST API with endpoints that return a list of records, I'm sure you've done plenty of this. And that's why Season exists.

Installation

Add this line to your application's Gemfile:

gem 'season', '~> 0.2'

And then execute:

$ bundle

Or install it yourself as:

$ gem install season

Requirements

In this first version Season only supports ActiveRecord, but we plan to also support Mongoid in a very short period of time.

Usage

To use Season scopes just append _before, _after or _between to your datetime column names and pass the arguments accordingly. See this:

First, include Season in your model(s):

class User < ActiveRecord::Base
  include Season
  ...
end

Now, considering that our User class has three datetime columns named :created_at, :updated_at and :confirmed_at, the following scopes will be automatically available:

# * Time/DateTime/String instances are allowed as arguments.

User.created_at_before(Time.now)
User.created_at_after(DateTime.now)
User.created_at_between(Time.now - 1.week, '31-01-2015')

User.updated_at_before(DateTime.now)
User.updated_at_after('01-01-2015')
User.updated_at_between(Time.now - 1.week, Time.now)

User.confirmed_at_before('01-01-2015')
User.confirmed_at_after(DateTime.now)
User.confirmed_at_between(Time.now - 1.year, Time.now - 1.week)

They are chainable, so you can also do things like this:

User.where(id: [1, 2, 3]).created_before(Time.now)
User.updated_after('01-01-2015').order(created_at: :asc)

To Do

  • Even more tests
  • Support other ORMs (Mongoid, 'insert-more-here')
  • Add Error Handling
  • Add helpers for instances (like User.first.created_before?('01-02-2015'))
  • Add support for queries with joins

Contributing

  1. Fork it ( https://github.com/joaodiogocosta/season/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

season's People

Watchers

 avatar  avatar

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.