Giter Site home page Giter Site logo

range_scopes's Introduction

RangeScopes

This gem adds scopes that can be used for selecting objects, that contains scoped values.

Still didn’t get it?

Consider this example: you have active record model that has price (or any other field) range: price_from and price_till. You have three objects: # item #1: price_from: 50, price_to: 100 # item #2: price_from: 100, price_till: 150 # item #3: price_from: 150, price_till: 200

  • If you look for items that has price less than 110, you will expect to get item #1 and #2.

  • If you look for items that has price more than 110, you will expect to get item #2 and #3.

And how about if item contains nil values?

By default range_scopes threat nil as no value - model without any value are always returned

Usage

The only thing you need to do is to include Range scopes in active record model where would like to have range scopes:

class Item < ActiveRecord::Base
  include RangeScopes
end

This will add scope named “range_between”.

To get items with price between 100 and 200:

Item.range_between("price_from" => 100, "price_till" => 200)

You should always provide two key values. If you want to get items that has price bigger than 200, you should do like this:

Item.range_between("price_from" => nil, "price_till" => 200)

Keys “price_from” and “price_till” should always be in same order: first key is field name that starts range, second key is field name that ends scope.

You can use range scopes for associations too, like this:

#this will return groups that contain friends with age between 15 and 25
Group.include(:friends).range_between("friends.age_from" => 15, "friends.age_till" => 25)

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.