Class: ReferenceType

Inherits:
ApplicationRecord show all
Defined in:
app/models/reference_type.rb

Overview

The ReferenceType class represents a reference entity in the application. It includes validations for presence and uniqueness of reference attributes, and manages the position of references within the system.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sort_elements(id_array) ⇒ void

This method returns an undefined value.

Class method to sort elements based on an array of IDs. Updates the position of each ReferenceType according to the order in the array.

Parameters:

  • id_array (Array<Integer>)

    An array of element IDs to be sorted.



32
33
34
35
36
37
38
39
# File 'app/models/reference_type.rb', line 32

def self.sort_elements(id_array)
  transaction do
    logger.debug { id_array.inspect }
    id_array.each_with_index do |elm_id, i|
      ReferenceType.update(elm_id, position: i)
    end
  end
end

Instance Method Details

#set_last_positionvoid

This method returns an undefined value.

Sets the position of the reference to the next available position. If no references exist, it sets the position to 1.



22
23
24
25
# File 'app/models/reference_type.rb', line 22

def set_last_position
  position = ReferenceType.all.order(position: :desc)&.first&.position
  self.position = position.blank? ? 1 : position + 1
end