Sha256: 521996887cf9ba4bc53cc88f907d5221fa35dcd38660d64003c73d2c0696ac23

Contents?: true

Size: 680 Bytes

Versions: 2

Compression:

Stored size: 680 Bytes

Contents

module Positioning
  class Healer
    def initialize(model, column, order)
      @model = model
      @column = column.to_sym
      @order = order
    end

    def heal
      if positioning_columns.present?
        @model.select(*positioning_columns).distinct.each do |scope_record|
          sequence @model.where(scope_record.slice(*positioning_columns))
        end
      else
        sequence @model
      end
    end

    private

    def positioning_columns
      @model.positioning_columns[@column]
    end

    def sequence(scope)
      scope.reorder(@order).each.with_index(1) do |record, index|
        record.update_columns @column => index
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
positioning-0.3.0 lib/positioning/healer.rb
positioning-0.2.6 lib/positioning/healer.rb