Sha256: 22e80cd29ed22d57b632dfbace89a60fc9494b1ec6ec3bbcaf0db27427bb45b8

Contents?: true

Size: 502 Bytes

Versions: 1

Compression:

Stored size: 502 Bytes

Contents

module Sortables
  module Sortable
    def self.included(base)
      base.extend ClassMethods
      base.send(:default_scope, :order => :position)
      base.send(:before_create, :set_minimum_position)
      base.attr_accessible :position
    end

    module ClassMethods
      def minimum_position
        (maximum(:position) || -1) + 1
      end
    end

    private
    def set_minimum_position
      return unless position.blank?
      self.position = self.class.minimum_position
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sortables-0.0.1 lib/sortables/sortable.rb