Sha256: dfc7bb78448aa71a664cade9e34cb9c8f071f6d98783a475bdb3b5a514c21f5b

Contents?: true

Size: 1.15 KB

Versions: 5

Compression:

Stored size: 1.15 KB

Contents

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

    def heal
      if scope_columns.present?
        @model.select(*scope_columns).distinct.each do |scope_record|
          @model.transaction do
            if scope_associations.present?
              scope_associations.each do |scope_association|
                scope_record.send(scope_association).lock!
              end
            else
              @model.where(scope_record.slice(*scope_columns)).lock!
            end

            sequence @model.where(scope_record.slice(*scope_columns))
          end
        end
      else
        @model.transaction do
          @model.all.lock!
          sequence @model
        end
      end
    end

    private

    def scope_columns
      @model.positioning_columns[@column][:scope_columns]
    end

    def scope_associations
      @model.positioning_columns[@column][:scope_associations]
    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

5 entries across 5 versions & 1 rubygems

Version Path
positioning-0.4.4 lib/positioning/healer.rb
positioning-0.4.3 lib/positioning/healer.rb
positioning-0.4.2 lib/positioning/healer.rb
positioning-0.4.1 lib/positioning/healer.rb
positioning-0.4.0 lib/positioning/healer.rb