Sha256: 90cd211c9817a2127c03301c3a44f8250f27e309af2802fb5354919d739d00d6

Contents?: true

Size: 1.83 KB

Versions: 15

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module Renalware
  module Sortable
    extend ActiveSupport::Concern

    # Assumes that the model has the #position attribute and adds 2 features to a model that
    # includes it:
    # 1. Set the :position to the next highest value when a new row is added. If the model has a
    #    position_sorting_scope scope or class method, this is used to narrow the range of records
    #    searched when finding the current max position
    # 2. Add ability to sort rows based on an array of ids.

    included do
      before_create :set_position
    end

    class_methods do
      # A controller #sort action might for instance invoke this class method on an object.
      # The array of ids are the model to sort, and sorting is done by updating each model's
      # position to reflect their place in the array.
      def sort(ids)
        Array(ids).each_with_index do |id, index|
          where(id: id).update_all(["position=?", index + 1])
        end
      end
    end

    # Invoked by the callback when a record is created to set the initial value of #position.
    # If no position_sorting_scope is defined on the model class, we set position to the max
    # position in the whole table. If position_sorting_scope is provided we use it to refine the
    # grouping and hence the max(position) found - for example to get the max position where
    # patient_id = 123
    #
    # Example scope in a model:
    #   scope :position_sorting_scope, ->(problem) { where(patient_id: problem.patient.id) }
    #
    def set_position
      return unless respond_to?(:position)

      scope = if self.class.respond_to?(:position_sorting_scope)
                self.class.position_sorting_scope(self)
              else
                self.class
              end
      self.position = (scope.maximum(:position) || 0) + 1
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/models/concerns/renalware/sortable.rb
renalware-core-2.1.0 app/models/concerns/renalware/sortable.rb
renalware-core-2.0.167 app/models/concerns/renalware/sortable.rb
renalware-core-2.0.166 app/models/concerns/renalware/sortable.rb
renalware-core-2.0.165 app/models/concerns/renalware/sortable.rb
renalware-core-2.0.164 app/models/concerns/renalware/sortable.rb
renalware-core-2.0.163 app/models/concerns/renalware/sortable.rb
renalware-core-2.0.162 app/models/concerns/renalware/sortable.rb
renalware-core-2.0.161 app/models/concerns/renalware/sortable.rb
renalware-core-2.0.160 app/models/concerns/renalware/sortable.rb
renalware-core-2.0.159 app/models/concerns/renalware/sortable.rb
renalware-core-2.0.158 app/models/concerns/renalware/sortable.rb
renalware-core-2.0.157 app/models/concerns/renalware/sortable.rb
renalware-core-2.0.156 app/models/concerns/renalware/sortable.rb
renalware-core-2.0.155 app/models/concerns/renalware/sortable.rb