Module: ActsAsList::Mongoid::InstanceMethods
- Defined in:
- lib/mongoid/acts_as_list.rb
Instance Method Summary collapse
-
#decrement_position ⇒ Object
Decrease the position of this item without adjusting the rest of the list.
-
#do_decrement(conditions, options) ⇒ Object
conditions, { position_column => 1 }.
- #do_increment(conditions, options) ⇒ Object
-
#first? ⇒ Boolean
Return
true
if this object is the first in the list. - #greater_than_me ⇒ Object
-
#higher_item ⇒ Object
Return the next higher item in the list.
-
#in_list? ⇒ Boolean
Test if this record is in a list.
-
#increment_position ⇒ Object
Increase the position of this item without adjusting the rest of the list.
- #insert_at(position = 1) ⇒ Object
-
#insert_in_list_at(position = 1) ⇒ Object
Insert the item at the given position (defaults to the top position of 1).
-
#last? ⇒ Boolean
Return
true
if this object is the last in the list. - #less_than_me ⇒ Object
-
#lower_item ⇒ Object
Return the next lower item in the list.
- #move(command) ⇒ Object
- #move_above(object) ⇒ Object
- #move_below(object) ⇒ Object
-
#move_higher ⇒ Object
Swap positions with the next higher item, if one exists.
-
#move_lower ⇒ Object
Swap positions with the next lower item, if one exists.
- #move_to(position = 1) ⇒ Object
-
#move_to_bottom ⇒ Object
Move to the bottom of the list.
-
#move_to_top ⇒ Object
Move to the top of the list.
- #order_by_position(conditions, extras = []) ⇒ Object
-
#remove_from_list ⇒ Object
Removes the item from the list.
-
#sort ⇒ Object
sorts all items in the list if two items have same position, the one created more recently goes first.
Instance Method Details
#decrement_position ⇒ Object
Decrease the position of this item without adjusting the rest of the list.
224 225 226 227 228 229 230 |
# File 'lib/mongoid/acts_as_list.rb', line 224 def decrement_position return unless in_list? # in_collection.where(:pos => my_position). adjust!(position_key => -1) save! end |
#do_decrement(conditions, options) ⇒ Object
conditions, { position_column => 1 }
132 133 134 |
# File 'lib/mongoid/acts_as_list.rb', line 132 def do_decrement( conditions, ) in_collection.where(conditions).adjust! position_key => -1 end |
#do_increment(conditions, options) ⇒ Object
136 137 138 |
# File 'lib/mongoid/acts_as_list.rb', line 136 def do_increment( conditions, ) in_collection.where(conditions).adjust! position_key => 1 end |
#first? ⇒ Boolean
Return true
if this object is the first in the list.
233 234 235 236 |
# File 'lib/mongoid/acts_as_list.rb', line 233 def first? return false unless in_list? my_position == 1 end |
#greater_than_me ⇒ Object
144 145 146 |
# File 'lib/mongoid/acts_as_list.rb', line 144 def greater_than_me { position_key.gt => my_position.to_i} end |
#higher_item ⇒ Object
Return the next higher item in the list.
246 247 248 249 250 |
# File 'lib/mongoid/acts_as_list.rb', line 246 def higher_item return nil unless in_list? conditions = scope_condition.merge!( less_than_me ) order_by_position(conditions).last end |
#in_list? ⇒ Boolean
Test if this record is in a list
261 262 263 |
# File 'lib/mongoid/acts_as_list.rb', line 261 def in_list? !my_position.nil? end |
#increment_position ⇒ Object
Increase the position of this item without adjusting the rest of the list.
216 217 218 219 220 221 |
# File 'lib/mongoid/acts_as_list.rb', line 216 def increment_position return unless in_list? # in_collection.where(:pos => my_position). adjust!(position_key => 1) save! end |
#insert_at(position = 1) ⇒ Object
148 149 150 |
# File 'lib/mongoid/acts_as_list.rb', line 148 def insert_at(position = 1) insert_in_list_at(position) end |
#insert_in_list_at(position = 1) ⇒ Object
Insert the item at the given position (defaults to the top position of 1).
167 168 169 |
# File 'lib/mongoid/acts_as_list.rb', line 167 def insert_in_list_at(position = 1) insert_at_position(position) end |
#last? ⇒ Boolean
Return true
if this object is the last in the list.
239 240 241 242 243 |
# File 'lib/mongoid/acts_as_list.rb', line 239 def last? return false unless in_list? bottom_pos = bottom_position_in_list my_position == bottom_pos end |
#less_than_me ⇒ Object
140 141 142 |
# File 'lib/mongoid/acts_as_list.rb', line 140 def less_than_me { position_key.lt => my_position.to_i} end |
#lower_item ⇒ Object
Return the next lower item in the list.
253 254 255 256 257 258 |
# File 'lib/mongoid/acts_as_list.rb', line 253 def lower_item return nil unless in_list? conditions = scope_condition.merge!( greater_than_me ) order_by_position(conditions).first end |
#move(command) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mongoid/acts_as_list.rb', line 73 def move command if command.kind_of? Symbol case command when :highest, :top move_to_top when :lowest, :bottom move_to_bottom when :up, :higher move_higher when :down, :lower move_lower else raise ArgumentError, "unknown move command '#{command}', try one of #{self.class.move_commands_available}" end elsif command.kind_of? Hash other = command.values.first cmd = command.keys.first case cmd when :to move_to(other) when :above move_above(other) when :below move_below(other) else raise ArgumentError, "Hash command #{cmd.inspect} not valid, must be one of" end else raise ArgumentError, "move command takes either a Symbol or Hash as an argument, not a #{command.class}" end end |
#move_above(object) ⇒ Object
161 162 163 164 |
# File 'lib/mongoid/acts_as_list.rb', line 161 def move_above(object) new_pos = ( self == object ) ? self.my_position : ((object.my_position > self.my_position) ? object.my_position - 1 : object.my_position) move_to(new_pos) end |
#move_below(object) ⇒ Object
156 157 158 159 |
# File 'lib/mongoid/acts_as_list.rb', line 156 def move_below(object) new_pos = (self == object) ? self.my_position : ((object.my_position > self.my_position) ? object.my_position : object.my_position + 1) move_to(new_pos) end |
#move_higher ⇒ Object
Swap positions with the next higher item, if one exists.
181 182 183 184 185 186 187 |
# File 'lib/mongoid/acts_as_list.rb', line 181 def move_higher high_item = higher_item return unless high_item high_item.increment_position decrement_position end |
#move_lower ⇒ Object
Swap positions with the next lower item, if one exists.
172 173 174 175 176 177 178 |
# File 'lib/mongoid/acts_as_list.rb', line 172 def move_lower low_item = lower_item return unless low_item low_item.decrement_position increment_position end |
#move_to(position = 1) ⇒ Object
152 153 154 |
# File 'lib/mongoid/acts_as_list.rb', line 152 def move_to(position = 1) insert_in_list_at(position) end |
#move_to_bottom ⇒ Object
Move to the bottom of the list. If the item is already in the list, the items below it have their position adjusted accordingly.
191 192 193 194 195 196 |
# File 'lib/mongoid/acts_as_list.rb', line 191 def move_to_bottom return unless in_list? decrement_positions_on_lower_items assume_bottom_position end |
#move_to_top ⇒ Object
Move to the top of the list. If the item is already in the list, the items above it have their position adjusted accordingly.
200 201 202 203 204 205 |
# File 'lib/mongoid/acts_as_list.rb', line 200 def move_to_top return unless in_list? increment_positions_on_higher_items assume_top_position end |
#order_by_position(conditions, extras = []) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/mongoid/acts_as_list.rb', line 106 def order_by_position conditions, extras = [] sub_collection = in_collection.where(conditions) sub_collection = if sub_collection.sort { |x,y| x.my_position <=> y.my_position } else sub_collection.order_by(position_key.to_sym.asc) end if !extras.empty? sub_collection = if sub_collection.sort do |x,y| if x.my_position == y.my_position x.created_at <=> y.created_at else x.my_position <=> y.my_position end end else sub_collection.order_by(extras) end end sub_collection end |
#remove_from_list ⇒ Object
Removes the item from the list.
208 209 210 211 212 213 |
# File 'lib/mongoid/acts_as_list.rb', line 208 def remove_from_list if in_list? decrement_positions_on_lower_items set_my_position nil end end |
#sort ⇒ Object
sorts all items in the list if two items have same position, the one created more recently goes first
267 268 269 270 271 272 273 274 |
# File 'lib/mongoid/acts_as_list.rb', line 267 def sort conditions = scope_condition list_items = order_by_position(conditions, :created_at.desc).to_a list_items.each_with_index do |list_item, index| list_item.set_my_position index + 1 end end |