Sha256: 9740d5209d2103775c0d8de97fa033dc7ef684dbfcf7288e1d49bb075aad015a
Contents?: true
Size: 1.76 KB
Versions: 19
Compression:
Stored size: 1.76 KB
Contents
module Kuhsaft # If you use this mixin, your class must implement the following methods # siblings(), returns the siblings of the same type of object module Orderable def self.included(base) base.extend(ClassMethods) base.send :include, InstanceMethods base.after_create :set_position end module InstanceMethods def increment_position update_attribute :position, position + 1 end def decrement_position update_attribute :position, position - 1 end def preceding_sibling siblings.where('position = ?', position - 1).first end def succeeding_sibling siblings.where('position = ?', position + 1).first end def preceding_siblings siblings.where('position <= ?', position).where('id != ?', id) end def succeeding_siblings siblings.where('position >= ?', position).where('id != ?', id) end def position_to_top update_attribute :position, 1 recount_siblings_position_from 1 end def recount_siblings_position_from(position) counter = position succeeding_siblings.each do |s| counter += 1 s.update_attribute(:position, counter) end end def reposition(before_id) if before_id.blank? position_to_top else update_attribute :position, self.class.position_of(before_id) + 1 recount_siblings_position_from position end end def set_position initial_position = siblings.blank? ? 1 : siblings.count + 1 update_attribute(:position, initial_position) end end module ClassMethods def position_of(id) find(id).position rescue 1 end end end end
Version data entries
19 entries across 19 versions & 2 rubygems