itmammoth / rails_sortable

Easy drag & drop sorting with persisting the arranged order for rails
MIT License
142 stars 37 forks source link

Added RailsSortable::Model#sortable_id #14

Closed jvanus closed 6 years ago

jvanus commented 6 years ago

A simple sortable_id method on for classes which include RailsSortable::Model. Returns the same sortable_id as Enumerable#each_with_sortable_id but is useful in cases where that enumeration method is inconvenient.

jvanus commented 6 years ago

The use case I ran into for example:

category.rb

has_many :items

item.rb

belongs_to :category

categories/_form.html.erb

<%= form_for(@category)  do |f| %>
...
<%= f.fields_for :items do |item| %>
  <div id="<%= item.object.sortable_id %>">
itmammoth commented 6 years ago

Thank you!

I've made a small modification on Enumerable for DRY and the case that someone overrides model#sortable_id. https://github.com/itmammoth/rails_sortable/pull/15/files#diff-2a284cc28e2c46fa796d46ecc87cddceR4