Module: Controllers::Sortable

Overview

ActiveSupport::Concern mixin: sortable.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#perform_sort(record_class) ⇒ void

This method returns an undefined value.

Updates each record's position to its index in the submitted id list.

Parameters:

  • record_class (Class<ActiveRecord::Base>)

    model class to sort



21
22
23
24
25
26
27
# File 'app/concerns/controllers/sortable.rb', line 21

def perform_sort(record_class)
  param_name = record_class.name.tableize.singularize
  params[param_name].each_with_index do |id, index|
    record_class.where(id: id).update_all(position: index + 1)
  end
  head :ok
end

#sortvoid

This method returns an undefined value.

Sorts records of the current controller's model by submitted position order.



14
15
16
# File 'app/concerns/controllers/sortable.rb', line 14

def sort
  perform_sort controller_name.classify.constantize
end