Module: MongoMapper::Plugins::SortableItem::InstanceMethods
- Defined in:
- lib/mm_sortable_item.rb
Instance Method Summary collapse
- #add_to_list(position = bottom_of_list) ⇒ Object
- #add_to_list_bottom ⇒ Object
- #add_to_list_top ⇒ Object
- #bottom_of_list ⇒ Object
- #decrement_positions_on_lower_items(position = self.position) ⇒ Object
- #in_list? ⇒ Boolean
- #increment_positions_on_lower_items(position = self.position) ⇒ Object
- #lower_than_conditions(position = self.position) ⇒ Object
- #remove_from_list ⇒ Object
- #scoped_list_id ⇒ Object
- #set_position(new_position) ⇒ Object
Instance Method Details
#add_to_list(position = bottom_of_list) ⇒ Object
69 70 71 72 73 |
# File 'lib/mm_sortable_item.rb', line 69 def add_to_list position = bottom_of_list remove_from_list increment_positions_on_lower_items position set_position position end |
#add_to_list_bottom ⇒ Object
61 62 63 |
# File 'lib/mm_sortable_item.rb', line 61 def add_to_list_bottom add_to_list end |
#add_to_list_top ⇒ Object
65 66 67 |
# File 'lib/mm_sortable_item.rb', line 65 def add_to_list_top add_to_list 1 end |
#bottom_of_list ⇒ Object
75 76 77 |
# File 'lib/mm_sortable_item.rb', line 75 def bottom_of_list self.class.in_list( scoped_list_id ).count + 1 end |
#decrement_positions_on_lower_items(position = self.position) ⇒ Object
84 85 86 |
# File 'lib/mm_sortable_item.rb', line 84 def decrement_positions_on_lower_items position = self.position self.class.decrement( lower_than_conditions(position), { :position => 1 } ) end |
#in_list? ⇒ Boolean
52 53 54 |
# File 'lib/mm_sortable_item.rb', line 52 def in_list? !send(:position).nil? end |
#increment_positions_on_lower_items(position = self.position) ⇒ Object
88 89 90 |
# File 'lib/mm_sortable_item.rb', line 88 def increment_positions_on_lower_items position = self.position self.class.increment( lower_than_conditions(position), { :position => 1 } ) end |
#lower_than_conditions(position = self.position) ⇒ Object
79 80 81 82 |
# File 'lib/mm_sortable_item.rb', line 79 def lower_than_conditions position = self.position query = self.class.conditions_for_list_scope scoped_list_id query.merge( :position.gte => position ) end |
#remove_from_list ⇒ Object
92 93 94 95 96 97 |
# File 'lib/mm_sortable_item.rb', line 92 def remove_from_list if in_list? decrement_positions_on_lower_items self.position = nil end end |
#scoped_list_id ⇒ Object
56 57 58 59 |
# File 'lib/mm_sortable_item.rb', line 56 def scoped_list_id the_column = self.class.list_scope_column the_column ? self.send(the_column) : nil end |
#set_position(new_position) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/mm_sortable_item.rb', line 99 def set_position new_position remove_from_list increment_positions_on_lower_items new_position if new_position != self.position self.position = new_position save unless new_record? end end |