Easy ordering of Active Record objects.
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'>
>> User.assort('first_name')
=> [#<User first_name: 'A'>, #<User first_name: 'B'>, #<User first_name: 'B'>]
>> User.assort('-first_name')
=> [#<User first_name: 'C'>, #<User first_name: 'B'>, #<User first_name: 'A'>]
>> Booking.assort('-user.first_name')
>> Request.assort('-user.first_name', user: :renter)
>> Booking.assort('-user.first_name,slot_places')
class UsersController < ApplicationController
def index
@users = User.assort(order_param)
end
private
def order_param
params[:sort]
end
end
Add this line to your application's Gemfile:
gem 'easy_orderable'
And then execute:
$ bundle
Or install it yourself as:
$ gem install easy_orderable
- Fork it
- Clone the project
git clone git@github.com:[YOUR GITHUB USERNAME]/easy_orderable.git
cd easy_orderable
- Install dependencies,
bundle install
- Create your feature branch
git checkout -b my-new-feature
- Write your feature, along with tests for your changes
- Run the tests
rake test
, all must be green - Commit your changes
git commit -am 'Added some feature'
- Push to the branch
git push origin my-new-feature
- Create new Pull Request
If you enjoy using eod, you are free consider a small donation! 🙂 Buy me a cup of coffee
The gem is available as open source under the terms of the MIT License.