No commit activity in last 3 years
No release in over 3 years
Allow easy sorting tables tables in rails
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
~> 5.0
~> 10.0

Runtime

>= 5.2
 Project Readme

FlexibleSorting

Description

Simple gem for sorting data in HTML tables

Installation

Add this line to your application's Gemfile:

gem "rails_flexible_sorting"

And then execute:

$ bundle

Or install it yourself as:

$ gem install rails_flexible_sorting

Usage

class User < ApplicationRecord
  include FlexibleSorting::Model
  
  belongs_to :service
  scope :sorted_by_service, -> { all.sort_by { |service| service.method } }

  sortable :id, :updated_at, :email
  sortable :profile_name, -> { joins(:profile) }, column: "profiles.name"
  sortable :network, -> { sorted_by_service }, virtual: true
  sortable :company, -> (obj, name) { obj.company_label_for(name) }, virtual: true, as_instance_method: true
  sortable :state, -> (obj) { obj.state_label }, virtual: true, as_instance_method: true

  def company_label_for(name)
    # do something and return value
  end

  def state_label
    # do something and return value
  end  
end

In Views

<th><%= sortable :email, default: true %></th>
<th><%= sortable :updated_at, direction: :desc %></th>
<th>
  <%= sortable :profile_name do %>
    Custom label
  <% end %>
</th>

If you want customize how arrows are generated copy views/lexible_sorting/_label.html.erb and change how you want.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Marcin81/rails_flexible_sorting. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

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

Code of Conduct

Everyone interacting in the SortableForRails project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.