Module: Renalware::Sortable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Admissions::Request, HD::Station, Problems::Problem
- Defined in:
- app/models/concerns/renalware/sortable.rb
Instance Method Summary collapse
-
#set_position ⇒ Object
Invoked by the callback when a record is created to set the initial value of #position.
Instance Method Details
#set_position ⇒ Object
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) }
38 39 40 41 42 43 44 45 46 47 |
# File 'app/models/concerns/renalware/sortable.rb', line 38 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 |