Sha256: d38845eaf3aeb30d310e2716f085ea5a9766c02fb1ae4e59b9dcfc56b492dddf

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

class SortableController < ApplicationController

  VERIFIER = Rails.application.message_verifier(:rails_sortable_generate_sortable_id)

  #
  # post /sortable/reorder, rails_sortable: [
  #   "BAhJIhVjbGFzcz1JdGVtLGlkPTUwBjoGRVQ=--b48adfad6d6d7764e4106c44fc090fcad15d721e",
  #   "BAhJIhVjbGFzcz1JdGVtLGlkPTQxBjoGRVQ=--ac1c2d3b8eae8dd72e49fae302005e5ae4fc00a4", ...]
  # Param `rails_sorable` is an array object containing encoded tokens,
  # and each token must be able to be decoded with VERIFIER to a string formatted as "class={CLASS_NAME},id={ID}".
  #
  def reorder
    ActiveRecord::Base.transaction do
      params['rails_sortable'].each_with_index do |token, new_sort|
        model = find_model(token)
        current_sort = model.read_attribute(model.class.sort_attribute)
        model.update_sort!(new_sort) if current_sort != new_sort
      end
    end

    head :ok
  end

private

  def find_model(token)
    klass, id = VERIFIER.verify(token).match(/class=(.+),id=(.+)/)[1..2]
    klass.constantize.find(id.to_i)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_sortable-1.3.1 app/controllers/sortable_controller.rb
rails_sortable-1.3.0 app/controllers/sortable_controller.rb