0.0
Low commit activity in last 3 years
No release in over a year
Adds capabilities to create sortable table headers and connect them to an ActiveRecord query
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 5.0
 Project Readme

SortableBy

Add sortable table headers to your views and connect them to ActiveRecord queries in your controller.

Installation

Add this line to your application's Gemfile:

gem 'sortable_by'

And then execute:

$ bundle install

Example Usage

In your view, the sortable_table_header helper method will generate a <thead> element with header tags.

<%= sortable_table_header :users_path do |t| %>
  <%= t.sortable :full_name %>
  <%= t.sortable :email %>
  <%= t.sortable :birth_date %>
  <%= t.header :favorite_color %>
  <th>Plain HTML is fine too</th>
<% end %>

See table_helper.rb for options.

In your controller, use the sortable_by method to configure sorting attributes.

class MyController < ApplicationController
  # Configure the sortable attributes
  sortable_by :email,                               # A basic sort
              full_name: [:last_name, :first_name], # This will sort on two columns
              birth_date: 'birth_dates.date :dir',  # This will sort on a joined table
              default: :email                       # The sort to use when none is passed
              direction: :desc                      # Initial sort direction (defaults to :asc)

  def index
    # Call #sortable_query to produce an ActiveRecord compatible sorting hash
    MyModel.where(...).order(sortable_query)
  end
end

See params.rb for options.

Icon Support

Font Awesome and Glyphicons are both supported by default. You can also add your own icon strategy.

module SortableBy::IconStrategy
  def self.custom(context, dir)
    "test: #{dir}"
  end
end
<%= sortable_table_header :users_path, icon: :custom do |t| %>
  ...
<% end %>