Module: Kuhsaft::Orderable::InstanceMethods
- Defined in:
- lib/kuhsaft/orderable.rb
Instance Method Summary collapse
- #decrement_position ⇒ Object
- #increment_position ⇒ Object
- #position_to_top ⇒ Object
- #preceding_sibling ⇒ Object
- #preceding_siblings ⇒ Object
- #recount_siblings_position_from(position) ⇒ Object
- #reposition(before_id) ⇒ Object
- #set_position ⇒ Object
- #succeeding_sibling ⇒ Object
- #succeeding_siblings ⇒ Object
Instance Method Details
#decrement_position ⇒ Object
17 18 19 |
# File 'lib/kuhsaft/orderable.rb', line 17 def decrement_position update_attribute :position, position - 1 end |
#increment_position ⇒ Object
13 14 15 |
# File 'lib/kuhsaft/orderable.rb', line 13 def increment_position update_attribute :position, position + 1 end |
#position_to_top ⇒ Object
37 38 39 40 |
# File 'lib/kuhsaft/orderable.rb', line 37 def position_to_top update_attribute :position, 1 recount_siblings_position_from 1 end |
#preceding_sibling ⇒ Object
21 22 23 |
# File 'lib/kuhsaft/orderable.rb', line 21 def preceding_sibling siblings.where('position = ?', position - 1).first end |
#preceding_siblings ⇒ Object
29 30 31 |
# File 'lib/kuhsaft/orderable.rb', line 29 def preceding_siblings siblings.where('position <= ?', position).where('id != ?', id) end |
#recount_siblings_position_from(position) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/kuhsaft/orderable.rb', line 42 def recount_siblings_position_from(position) counter = position succeeding_siblings.each do |s| counter += 1 s.update_attribute(:position, counter) end end |
#reposition(before_id) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/kuhsaft/orderable.rb', line 50 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 |
#set_position ⇒ Object
59 60 61 62 |
# File 'lib/kuhsaft/orderable.rb', line 59 def set_position initial_position = siblings.blank? ? 1 : siblings.count + 1 update_attribute(:position, initial_position) end |
#succeeding_sibling ⇒ Object
25 26 27 |
# File 'lib/kuhsaft/orderable.rb', line 25 def succeeding_sibling siblings.where('position = ?', position + 1).first end |
#succeeding_siblings ⇒ Object
33 34 35 |
# File 'lib/kuhsaft/orderable.rb', line 33 def succeeding_siblings siblings.where('position >= ?', position).where('id != ?', id) end |