• Stars
    star
    140
  • Rank 260,005 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easy drag & drop sorting with persisting the arranged order for rails

RailsSortable

Build Status

RailsSortable is a simple Rails gem that allows you to create a listing view with drag & drop sorting. The arranged order will be persisted in the table without any pain.

RailsSortable

Setup

Add the following to your Gemfile then run bundle to install them.

gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'rails_sortable'

And then add the following to the asset pipeline in the application.js:

//= require jquery
//= require jquery_ujs
//= require jquery-ui/widgets/sortable
//= require rails_sortable

Usage

RailsSortable requires a specific column on the ActiveRecord Model for its implementation.

For instance, the following migration indicates the case that you are attempting to make Item model sortable.

class CreateItems < ActiveRecord::Migration[5.1]
  def change
    create_table :items do |t|
      t.string :title
      t.integer :sort  # for RailsSortable

      t.timestamps
    end
  end
end

and Item model as

class Item < ApplicationRecord
  include RailsSortable::Model
  set_sortable :sort  # Indicate a sort column
  # without_updating_timestamps: true, # If you do NOT want timestamps to be updated on sorting
  # without_validations: true # If you do NOT want validations to be run on sorting

end

and ItemsController as

class ItemsController < ApplicationController
  def index
    @items = Item.order(:sort).all
  end
end

and the listing view (typically - index.html.erb) as

...
<table>
  <tbody class="sortable">  <!-- sortable target -->
    <% @items.each_with_sortable_id do |item, sortable_id| %>
      <tr id="<%= sortable_id %>">  <!-- Needs id tag on sorting elements -->
        <td><%= item.title %></td>
        <td><%= item.sort %></td>
        <td><%= link_to 'Show', item %></td>
        <td><%= link_to 'Edit', edit_item_path(item) %></td>
        <td><%= link_to 'Destroy', item, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<!-- or just invoke model#sortable_id to get the id for sotable -->
...
<% @items.each do |item| %>
  <tr id="<%= item.sortable_id %>">
...

finally, apply sortable with Javascript.

$(function() {
  $('.sortable').railsSortable();
});

More

Please have a look at Use cases@wiki for further information.

Javascript options

jQuery plugin railsSortable is just a wrapper of jquery.ui.sortable. therefore it accepts all of sortable options.

see the http://api.jqueryui.com/sortable/ to get the details.

Contribution

Fork it, then install required gems like below.

$ bundle install

Please give me a PR freely.

Testing

# Test with a dummy application
$ spec/dummy/bin/rails db:migrate
$ spec/dummy/bin/rails s
# Insert test data
$ RAILS_ENV=test spec/dummy/bin/rails db:migrate
$ spec/dummy/bin/rails db:seed

# Run specs
$ bundle exec rspec

Licence

MIT Licence.

More Repositories

1

Tamotsu

Object-Spreadsheet Mapping for Google Apps Script
JavaScript
121
star
2

doorboy.vim

Vim plugin for auto closing brackets ( => (|) and quotations " => "|" , and more
Vim Script
20
star
3

run-rspec.vim

Lovely rspec runner for vim
Vim Script
19
star
4

toneletter

toneletter.js is a javascript plugin that allows you to put phonetic/tone symbols into text field with simple key bindings.
JavaScript
5
star
5

Tamotsu_Test

JavaScript
5
star
6

jquery.multipleSortable

JavaScript
4
star
7

bon-voyage.vim

A vim plugin to set up your workspace on starting vim.
Vim Script
4
star
8

with_popup

A rubygem for rails application to open and manage a popup window
Ruby
2
star
9

maximize.vim

Maximizing and restoring window for GUI Vim
Vim Script
2
star
10

constantinopolis

Setting constants solution for ruby applications.
Ruby
2
star
11

miraclesisters

ここがあのクリエイティブ忍者バンドMIRACLE SISTERSの公式サイトだ。
2
star
12

jquery.showuptrigger

A jQuery plugin that allows you to set a callback function when an element shows up.
JavaScript
2
star
13

SLogger

Logging into a sheet library for Google apps script
JavaScript
2
star
14

rebirth.vim

Restore window size and position on starting gvim
Vim Script
1
star
15

pysync

A simple backup/sync tool with python & rsync
Python
1
star
16

jqueryPluginTemplateUsingClass

A template for developing jQuery plugin (using javascript class)
JavaScript
1
star
17

ome-excel_report

[Oh! My Enter!] Excelレポート出力Railsアプリサンプル
Ruby
1
star
18

ReleaseAnimal

An android library to display release notes dialog without pain.
Java
1
star
19

ome_scrollable_positioned_list

Flutter scrollable_positioned_list sample
Dart
1
star
20

ome-tuning

Oh My Enter! Rails「アプリを浅くパフォーマンス・チューニングする」のデモアプリ
Ruby
1
star
21

calendar-example.vue

Calendar app example for vue.js
Vue
1
star
22

SpecRunner

Spec runner for Google Apps Script
JavaScript
1
star