Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/itmammoth/rails_sortable
Easy drag & drop sorting with persisting the arranged order for rails
https://github.com/itmammoth/rails_sortable
drag-and-drop gem rails ruby sort
Last synced: about 1 month ago
JSON representation
Easy drag & drop sorting with persisting the arranged order for rails
- Host: GitHub
- URL: https://github.com/itmammoth/rails_sortable
- Owner: itmammoth
- License: mit
- Created: 2013-12-27T13:28:38.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2023-03-27T08:34:35.000Z (over 1 year ago)
- Last Synced: 2024-09-19T05:40:22.013Z (about 2 months ago)
- Topics: drag-and-drop, gem, rails, ruby, sort
- Language: Ruby
- Homepage:
- Size: 1.12 MB
- Stars: 142
- Watchers: 2
- Forks: 37
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# RailsSortable
[![Build Status](https://travis-ci.org/itmammoth/rails_sortable.svg?branch=use_travis_ci)](https://travis-ci.org/itmammoth/rails_sortable)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](https://raw.githubusercontent.com/itmammoth/rails_sortable/master/rails_sortable.gif "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.
```ruby
class CreateItems < ActiveRecord::Migration[5.1]
def change
create_table :items do |t|
t.string :title
t.integer :sort # for RailsSortablet.timestamps
end
end
end
```
and `Item` model as
```ruby
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 sortingend
```
and `ItemsController` as
```ruby
class ItemsController < ApplicationController
def index
@items = Item.order(:sort).all
end
end
```and the listing view (typically - index.html.erb) as
```erb
...
<% @items.each_with_sortable_id do |item, sortable_id| %>
<%= item.title %>
<%= item.sort %>
<%= link_to 'Show', item %>
<%= link_to 'Edit', edit_item_path(item) %>
<%= link_to 'Destroy', item, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
...
<% @items.each do |item| %>
...
```finally, apply sortable with Javascript.
```javascript
$(function() {
$('.sortable').railsSortable();
});
```## More
Please have a look at [Use cases@wiki](https://github.com/itmammoth/rails_sortable/wiki#use-cases) 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/](http://api.jqueryui.com/sortable/) to get the details.
# Contribution
Fork it, then install required gems like below.
```bash
$ bundle install
```Please give me a PR freely.
### Testing
```bash
# 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.