0.0
Repository is archived
No release in over 3 years
Low commit activity in last 3 years
Provide ordering interface for clients with minimal configuration in Active Record models
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 5.0, >= 5.0.2
>= 4.2
~> 3.8, >= 3.8.1
~> 1.3, >= 1.3.13

Runtime

 Project Readme

EasyOrderable

Easy ordering of Active Record objects.

Usage

class User < ApplicationRecord
  include EasyOrderable

  has_many :bookings
  has_many :requests
end

class Booking < ApplicationRecord
  include EasyOrdearble

  belongs_to :user
end

class Request < ApplicationRecord
  include EasyOrderable

  belongs_to :renter, class_name: 'User', foreign_key: :user_id
end

#<User id: 1, first_name: 'A'>
#<User id: 2, first_name: 'B'>
#<User id: 3, first_name: 'C'>

Sort by column name asc

>> User.assort('first_name')
=> [#<User first_name: 'A'>, #<User first_name: 'B'>, #<User first_name: 'B'>]

Sort by column name desc

>> User.assort('-first_name')
=> [#<User first_name: 'C'>, #<User first_name: 'B'>, #<User first_name: 'A'>]

Sort by joined column name

>> Booking.assort('-user.first_name')

Sort by joined column name when associations name is custom

>> Request.assort('-user.first_name', user: :renter)

Sort by multiple params

>> Booking.assort('-user.first_name,slot_places')

You can use it from rails controller to handle json api compliant sort requests

class UsersController < ApplicationController
  def index
    @users = User.assort(order_param)
  end

  private

  def order_param
    params[:sort]
  end
end

Installation

Add this line to your application's Gemfile:

gem 'easy_orderable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install easy_orderable

Contributing

  1. Fork it
  2. Clone the project git clone git@github.com:[YOUR GITHUB USERNAME]/easy_orderable.git
  3. cd easy_orderable
  4. Install dependencies, bundle install
  5. Create your feature branch git checkout -b my-new-feature
  6. Write your feature, along with tests for your changes
  7. Run the tests rake test, all must be green
  8. Commit your changes git commit -am 'Added some feature'
  9. Push to the branch git push origin my-new-feature
  10. Create new Pull Request

Donating

If you enjoy using eod, you are free consider a small donation! 🙂 Buy me a cup of coffee

License

The gem is available as open source under the terms of the MIT License.