Module: DataMapper::Is::List::InstanceMethods
- Defined in:
- lib/dm-is-list/is/list.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#detach(scope = list_scope) ⇒ DataMapper::Collection
detaches a list item from the list, essentially setting the position as nil.
-
#left_sibling ⇒ Model
(also: #higher_item, #previous_item)
finds the previous higher item in the list (lower in number position).
-
#list(scope = list_query) ⇒ DataMapper::Collection
returns the list the current item belongs to.
-
#list_query ⇒ Hash
returns the query conditions.
-
#list_scope ⇒ Hash
returns the scope of the current list item.
-
#move(vector) ⇒ TrueClass, FalseClass
move item to a position in the list.
-
#move_to_list(scope, pos = nil) ⇒ Boolean
moves an item from one list to another.
-
#original_list_scope ⇒ Hash
returns the original scope of the current list item.
-
#reorder_list(order) ⇒ Boolean
reorder the list this item belongs to.
-
#repair_list ⇒ Object
repair the list this item belongs to.
-
#right_sibling ⇒ Model
(also: #lower_item, #next_item)
finds the next lower item in the list (higher in number position).
Instance Attribute Details
#moved ⇒ Object
330 331 332 |
# File 'lib/dm-is-list/is/list.rb', line 330 def moved @moved end |
Instance Method Details
#detach(scope = list_scope) ⇒ DataMapper::Collection
detaches a list item from the list, essentially setting the position as nil
426 427 428 429 |
# File 'lib/dm-is-list/is/list.rb', line 426 def detach(scope = list_scope) list(scope).all(:position.gt => position).adjust!({ :position => -1 },true) self.position = nil end |
#left_sibling ⇒ Model Also known as: higher_item, previous_item
finds the previous higher item in the list (lower in number position)
463 464 465 |
# File 'lib/dm-is-list/is/list.rb', line 463 def left_sibling list.reverse.first(:position.lt => position) end |
#list(scope = list_query) ⇒ DataMapper::Collection
returns the list the current item belongs to
389 390 391 |
# File 'lib/dm-is-list/is/list.rb', line 389 def list(scope = list_query) model.all(scope) end |
#list_query ⇒ Hash
returns the query conditions
373 374 375 |
# File 'lib/dm-is-list/is/list.rb', line 373 def list_query list_scope.merge(:order => [ :position ]) end |
#list_scope ⇒ Hash
returns the scope of the current list item
342 343 344 |
# File 'lib/dm-is-list/is/list.rb', line 342 def list_scope Hash[ model.[:scope].map { |p| [ p, attribute_get(p) ] } ] end |
#move(vector) ⇒ TrueClass, FalseClass
move item to a position in the list. position should only be changed through this
517 518 519 |
# File 'lib/dm-is-list/is/list.rb', line 517 def move(vector) move_without_saving(vector) && save end |
#move_to_list(scope, pos = nil) ⇒ Boolean
moves an item from one list to another
444 445 446 447 448 449 450 |
# File 'lib/dm-is-list/is/list.rb', line 444 def move_to_list(scope, pos = nil) detach # remove from current list attribute_set(model.[:scope][0], scope.to_i) # set new scope save # save progress. Needed to get the positions correct. reload # get a fresh new start move(pos) unless pos.nil? end |
#original_list_scope ⇒ Hash
returns the original scope of the current list item
357 358 359 360 361 362 |
# File 'lib/dm-is-list/is/list.rb', line 357 def original_list_scope pairs = model.[:scope].map do |p| [ p, (property = properties[p]) && original_attributes.key?(property) ? original_attributes[property] : attribute_get(p) ] end Hash[ pairs ] end |
#reorder_list(order) ⇒ Boolean
reorder the list this item belongs to
412 413 414 |
# File 'lib/dm-is-list/is/list.rb', line 412 def reorder_list(order) model.repair_list(list_scope.merge(:order => order)) end |
#repair_list ⇒ Object
repair the list this item belongs to
397 398 399 |
# File 'lib/dm-is-list/is/list.rb', line 397 def repair_list model.repair_list(list_scope) end |
#right_sibling ⇒ Model Also known as: lower_item, next_item
finds the next lower item in the list (higher in number position)
480 481 482 |
# File 'lib/dm-is-list/is/list.rb', line 480 def right_sibling list.first(:position.gt => position) end |