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
427 428 429 430 |
# File 'lib/dm-is-list/is/list.rb', line 427 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)
464 465 466 |
# File 'lib/dm-is-list/is/list.rb', line 464 def left_sibling list.reverse.first(:position.lt => position) end |
#list(scope = list_query) ⇒ DataMapper::Collection
returns the list the current item belongs to
390 391 392 |
# File 'lib/dm-is-list/is/list.rb', line 390 def list(scope = list_query) model.all(scope) end |
#list_query ⇒ Hash
returns the query conditions
374 375 376 |
# File 'lib/dm-is-list/is/list.rb', line 374 def list_query list_scope.merge(:order => [ :position ]) end |
#list_scope ⇒ Hash
returns the scope of the current list item
342 343 344 345 |
# File 'lib/dm-is-list/is/list.rb', line 342 def list_scope pairs = model.[:scope].map{ |p| [ p, attribute_get(p) ] } DataMapper::Ext::Array.to_hash(pairs) end |
#move(vector) ⇒ TrueClass, FalseClass
move item to a position in the list. position should only be changed through this
518 519 520 |
# File 'lib/dm-is-list/is/list.rb', line 518 def move(vector) move_without_saving(vector) && save end |
#move_to_list(scope, pos = nil) ⇒ Boolean
moves an item from one list to another
445 446 447 448 449 450 451 |
# File 'lib/dm-is-list/is/list.rb', line 445 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
358 359 360 361 362 363 |
# File 'lib/dm-is-list/is/list.rb', line 358 def original_list_scope pairs = model.[:scope].map{ |p| [ p, (property = properties[p]) && original_attributes.key?(property) ? original_attributes[property] : attribute_get(p) ] } DataMapper::Ext::Array.to_hash(pairs) end |
#reorder_list(order) ⇒ Boolean
reorder the list this item belongs to
413 414 415 |
# File 'lib/dm-is-list/is/list.rb', line 413 def reorder_list(order) model.repair_list(list_scope.merge(:order => order)) end |
#repair_list ⇒ Object
repair the list this item belongs to
398 399 400 |
# File 'lib/dm-is-list/is/list.rb', line 398 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)
481 482 483 |
# File 'lib/dm-is-list/is/list.rb', line 481 def right_sibling list.first(:position.gt => position) end |