Sha256: 8c74cd4cd2d25d83624ebd0746ff164cd7a345bf6f4129c045ba9fad199b3f3f

Contents?: true

Size: 747 Bytes

Versions: 23

Compression:

Stored size: 747 Bytes

Contents

module RailsCoreExtensions
  class PositionInitializer
    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

    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

    private

    def groups
      objects = @model_class.order(@position_column)
      @scope_name ? objects.group_by(&@scope_name.to_sym).values : [objects]
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
rails_core_extensions-0.15.0 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.14.0 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.13.2 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.13.1 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.13.0 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.12.0 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.11.3 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.11.2 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.11.1 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.11.0.beta.11 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.10.0 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.9.0 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.8.0 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.7.1 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.7.0 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.6.1 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.6.0 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.4.0 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.3.0 lib/rails_core_extensions/position_initializer.rb
rails_core_extensions-0.2.0 lib/rails_core_extensions/position_initializer.rb