Sha256: 075ccf43817d00046cfdd0c29ce47269463caaa2a6ae777158ee18cdde499ff0

Contents?: true

Size: 572 Bytes

Versions: 4

Compression:

Stored size: 572 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
    head :ok
  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

4 entries across 4 versions & 1 rubygems

Version Path
rails_sortable-1.1.0 app/controllers/sortable_controller.rb
rails_sortable-1.0.0 app/controllers/sortable_controller.rb
rails_sortable-0.1.1 app/controllers/sortable_controller.rb
rails_sortable-0.1.0 app/controllers/sortable_controller.rb