Sha256: d90bca36bca97ae2a3bb6b5a2c75584c77b68e77666405e4d1f8c30c4f09f99c

Contents?: true

Size: 584 Bytes

Versions: 1

Compression:

Stored size: 584 Bytes

Contents

class SortableController < ApplicationController
  #
  # post /sortable/reorder, :klass => ["1", "3", "2"]
  #
  def reorder
    klass, ids = parse_params
    attr = klass.sort_attribute
    models = klass.order(attr).to_a
    ids.each_with_index do |id, new_sort|
      model = models.find {|m| m.id == id }
      model.update_sort!(new_sort) if model.read_attribute(attr) != new_sort
    end
    render nothing: true
  end

private

  def parse_params
    klass_name = params.keys.first
    ids = params[klass_name].map {|id| id.to_i }
    [ klass_name.constantize, ids ]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_sortable-0.0.5 app/controllers/sortable_controller.rb