Code Monkey home page Code Monkey logo

order_as_specified's Introduction

Code Climate Test Coverage Build Status Inline docs Gem Version

OrderAsSpecified

OrderAsSpecified adds the ability to query an ActiveRecord class for results from the database in an arbitrary order, without having to store anything extra in the database.

It's as easy as:

class TestObject
  extend OrderAsSpecified
end

TestObject.order_as_specified(language: ["es", "en", "fr"])
=> #<ActiveRecord::Relation [
     #<TestObject id: 3, language: "es">,
     #<TestObject id: 1, language: "en">,
     #<TestObject id: 4, language: "en">,
     #<TestObject id: 2, language: "fr">
   ]>

Is this like ranked-model?

Other gems like ranked-model, acts_as_sortable, etc. assume you want the same ordering each time, and store data to keep track of this in the database. They're great at what they do, but if your desired ordering changes, or if you don't always want an ordering, this gem is your friend.

Installation

Add this line to your application's Gemfile:

gem 'order_as_specified'

And then execute:

$ bundle

Or install it yourself as:

$ gem install order_as_specified

Usage

Basic usage is simple:

class TestObject
  extend OrderAsSpecified
end

TestObject.order_as_specified(language: ["es", "en", "fr"])
=> #<ActiveRecord::Relation [
     #<TestObject id: 3, language: "es">,
     #<TestObject id: 1, language: "en">,
     #<TestObject id: 4, language: "en">,
     #<TestObject id: 2, language: "fr">
   ]>

This returns all TestObjects in the given language order. Note that this ordering is not possible with a simple ORDER BY. Magic!

Like any other ActiveRecord relation, it can be chained:

TestObject.
  where(language: ["es", "en", "fr"]).
  order_as_specified(language: ["es", "en", "fr"]).
  limit(3)
=> #<ActiveRecord::Relation [
     #<TestObject id: 3, language: "es">,
     #<TestObject id: 1, language: "en">,
     #<TestObject id: 4, language: "en">
   ]>

We can also use this when we want to sort by an attribute in another model:

TestObject.
  joins(:other_object).
  order_as_specified(other_objects: { id: [other1.id, other3.id, other2.id] })

Neat, huh?

In all cases, results with attribute values not in the given list will be sorted as though the attribute is NULL in a typical ORDER BY:

TestObject.order_as_specified(language: ["fr", "es"])
=> #<ActiveRecord::Relation [
     #<TestObject id: 2, language: "fr">,
     #<TestObject id: 3, language: "es">,
     #<TestObject id: 1, language: "en">,
     #<TestObject id: 4, language: "en">
   ]>

In databases that support it (such as PostgreSQL), you can also use an option to add a DISTINCT ON to your query when you would otherwise have duplicates:

TestObject.order_as_specified(distinct_on: true, language: ["fr", "en"])
=> #<ActiveRecord::Relation [
     #<TestObject id: 2, language: "fr">,
     #<TestObject id: 3, language: "en">,
     #<TestObject id: 4, language: "es">
   ]>

Note that if a nil value is passed in the ordering an error is raised, because databases do not have good or consistent support for ordering with NULL values in an arbitrary order, so we don't permit this behavior instead of allowing an unexpected result.

Documentation

We have documentation on RubyDoc.

Contributing

  1. Fork it (https://github.com/panorama-ed/order_as_specified/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

Make sure your changes have appropriate tests (bundle exec rspec) and conform to the Rubocop style specified. We use overcommit to enforce good code.

License

OrderAsSpecified is released under the MIT License.

order_as_specified's People

Contributors

jacobevelyn avatar gauravtiwari avatar

Watchers

Ryunosuke Sato avatar James Cloos 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.