Module: Positionable::InstanceMethods
- Defined in:
- lib/positionable.rb
Overview
All the methods available to a record that has had acts_as_positionable specified. Each method works by assuming the object to be the item in the list, so chapter.move_lower would move that chapter lower in the list of all chapters. Likewise, chapter.first? would return true if that chapter is the first in the list of all chapters.
Instance Method Summary collapse
-
#first?(list_name = :default) ⇒ Boolean
True if the record is the first in the list.
-
#higher_item(list_name = :default) ⇒ Object
(also: #previous_item)
Returns the next higher item in the list.
-
#in_list?(list_name = :default) ⇒ Boolean
True if the record is in the list.
-
#insert_at(position = :top, list_name = :default) ⇒ Object
Insert the item at the given position (defaults to the top position).
- #insert_at_bottom(list_name = :default) ⇒ Object
- #insert_at_top(list_name = :default) ⇒ Object
-
#last?(list_name = :default) ⇒ Boolean
True if the record is the last in the list.
-
#list_position(list_name = :default) ⇒ Object
Returns the record list position for the list.
-
#lower_item(list_name = :default) ⇒ Object
(also: #next_item)
Returns the next lower item in the list.
-
#move_higher(list_name = :default) ⇒ Object
(also: #move_up)
Swap positions with the next higher item, if one exists.
-
#move_lower(list_name = :default) ⇒ Object
(also: #move_down)
Swap positions with the next lower item, if one exists.
-
#move_to_bottom(list_name = :default) ⇒ Object
Move to the bottom of the list.
-
#move_to_top(list_name = :default) ⇒ Object
Move to the top of the list.
-
#remove_from_list(list_name = :default) ⇒ Object
Removes the record from the list and shift other items accordingly.
Instance Method Details
#first?(list_name = :default) ⇒ Boolean
True if the record is the first in the list.
167 168 169 |
# File 'lib/positionable.rb', line 167 def first?(list_name = :default) in_list? && list_position == top_position_in_list(list_name) end |
#higher_item(list_name = :default) ⇒ Object Also known as: previous_item
Returns the next higher item in the list.
177 178 179 180 |
# File 'lib/positionable.rb', line 177 def higher_item(list_name = :default) return nil unless in_list? scoped(list_name).descending(list_name).first(:conditions => (list_name, '<')) end |
#in_list?(list_name = :default) ⇒ Boolean
True if the record is in the list.
191 192 193 |
# File 'lib/positionable.rb', line 191 def in_list?(list_name = :default) !list_position(list_name).nil? end |
#insert_at(position = :top, list_name = :default) ⇒ Object
Insert the item at the given position (defaults to the top position).
125 126 127 128 129 130 131 132 |
# File 'lib/positionable.rb', line 125 def insert_at(position = :top, list_name = :default) if !position.is_a?(Symbol) && position <= 0 position = :top elsif !position.is_a?(Symbol) && position > bottom_position_in_list(list_name) && position != 1 position = :bottom end insert_at_position(list_name, position) end |
#insert_at_bottom(list_name = :default) ⇒ Object
138 139 140 |
# File 'lib/positionable.rb', line 138 def insert_at_bottom(list_name = :default) insert_at_position(list_name, :bottom) end |
#insert_at_top(list_name = :default) ⇒ Object
134 135 136 |
# File 'lib/positionable.rb', line 134 def insert_at_top(list_name = :default) insert_at_position(list_name, :top) end |
#last?(list_name = :default) ⇒ Boolean
True if the record is the last in the list.
172 173 174 |
# File 'lib/positionable.rb', line 172 def last?(list_name = :default) in_list? && list_position == bottom_position_in_list(list_name) end |
#list_position(list_name = :default) ⇒ Object
Returns the record list position for the list.
196 197 198 |
# File 'lib/positionable.rb', line 196 def list_position(list_name = :default) send position_column(list_name) end |
#lower_item(list_name = :default) ⇒ Object Also known as: next_item
Returns the next lower item in the list.
184 185 186 187 |
# File 'lib/positionable.rb', line 184 def lower_item(list_name = :default) return nil unless in_list? scoped(list_name).ascending(list_name).first(:conditions => (list_name, '>')) end |
#move_higher(list_name = :default) ⇒ Object Also known as: move_up
Swap positions with the next higher item, if one exists.
150 151 152 153 |
# File 'lib/positionable.rb', line 150 def move_higher(list_name = :default) higher = higher_item insert_at_position(list_name, higher.list_position) if higher end |
#move_lower(list_name = :default) ⇒ Object Also known as: move_down
Swap positions with the next lower item, if one exists.
143 144 145 146 |
# File 'lib/positionable.rb', line 143 def move_lower(list_name = :default) lower = lower_item insert_at_position(list_name, lower.list_position) if lower end |
#move_to_bottom(list_name = :default) ⇒ Object
Move to the bottom of the list.
157 158 159 |
# File 'lib/positionable.rb', line 157 def move_to_bottom(list_name = :default) insert_at_position(list_name, :bottom) if in_list? end |
#move_to_top(list_name = :default) ⇒ Object
Move to the top of the list.
162 163 164 |
# File 'lib/positionable.rb', line 162 def move_to_top(list_name = :default) insert_at_position(list_name, :top) if in_list? end |
#remove_from_list(list_name = :default) ⇒ Object
Removes the record from the list and shift other items accordingly.
201 202 203 204 205 206 207 208 |
# File 'lib/positionable.rb', line 201 def remove_from_list(list_name = :default) if in_list? acts_as_positionable_class.transaction do decrement_positions_on_lower_items(list_name) update_attribute position_column(list_name), nil end end end |