Skip to content

moser-inc/sortable_by

Folders and files

NameName
Last commit message
Last commit date

Latest commit

4f49c99 · Sep 27, 2022

History

16 Commits
Sep 23, 2022
Aug 4, 2017
Sep 27, 2022
Sep 23, 2022
Aug 4, 2017
Aug 4, 2017
Sep 23, 2022
Aug 4, 2017
Sep 27, 2022
Aug 4, 2017
Sep 27, 2022
Sep 23, 2022
Sep 23, 2022

Repository files navigation

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 %>

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published