Sha256: 2c67aa3f4b0c5b516f63391dd63c36acbf95e483648c4fed141f1329d8d797d7

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 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|
        next unless token.present?
        
        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)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_sortable-1.6.0 app/controllers/sortable_controller.rb
rails_sortable-1.5.0 app/controllers/sortable_controller.rb
rails_sortable-1.4.1 app/controllers/sortable_controller.rb