Sha256: 9c047fc4b111de21684e74b2bf8075ac0b4c487d5370c9dfab9c8ed6aa16b0c0

Contents?: true

Size: 736 Bytes

Versions: 1

Compression:

Stored size: 736 Bytes

Contents

require 'active_support/concern'
require 'active_support/core_ext/module/attribute_accessors'
require_relative 'acts_as_positionable/version'

module ActsAsPositionable

  def acts_as_positionable(attribute_name = :position)
    class_eval <<-CODE
      scope :ordered, -> { order(:#{attribute_name}) }
      after_initialize :_increment_position

      def _increment_position
        return if persisted?
        return if #{attribute_name}?


        if self.class.any?
          self.#{attribute_name} = self.class.ordered.last.#{attribute_name}
        end

        increment(:#{attribute_name}, 1)
      end

      private :_increment_position
    CODE
  end

end

require_relative 'acts_as_positionable/railtie' if defined? Rails

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_positionable-0.1.0 lib/acts_as_positionable.rb