Class: RailsCoreExtensions::PositionInitializer

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_core_extensions/position_initializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(model_class, scope_name = nil, position_column = nil) ⇒ PositionInitializer

Returns a new instance of PositionInitializer.



3
4
5
6
7
8
# File 'lib/rails_core_extensions/position_initializer.rb', line 3

def initialize(model_class, scope_name = nil, position_column = nil)
  @model_class = model_class
  @scope_name = scope_name
  @position_column = position_column
  @position_column ||= :position
end

Instance Method Details

#positionalizeObject



10
11
12
13
14
15
16
17
18
# File 'lib/rails_core_extensions/position_initializer.rb', line 10

def positionalize
  groups.each do |objects|
    objects.each.with_index do |object, index|
      next if object.position == index + 1
      scope = @model_class.where(id: object.id)
      scope.update_all(@position_column => index + 1)
    end
  end
end