Class: SortableController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/sortable_controller.rb

Constant Summary collapse

VERIFIER =
Rails.application.message_verifier(:rails_sortable_generate_sortable_id)

Instance Method Summary collapse

Instance Method Details

#reorderObject

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”.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/sortable_controller.rb', line 12

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