Module: SimpleActsAsList::ModelAdditions

Extended by:
ActiveSupport::Concern
Defined in:
lib/simple_acts_as_list/model_additions.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#first_itemObject

Returns first list item



52
53
54
# File 'lib/simple_acts_as_list/model_additions.rb', line 52

def first_item
  self.simple_acts_as_list_scope.first
end

#first_item?Boolean

Returns true if the item is the first one of the list

Returns:

  • (Boolean)


42
43
44
# File 'lib/simple_acts_as_list/model_additions.rb', line 42

def first_item?
  self.simple_acts_as_list_scope.first == self
end

#item_at_offset(offset) ⇒ Object

Returns the item with offset position based on current item

Example:

@todo = Todo.create
@todo_2 = Todo.create
@todo.item_at_offset(1) # returns @todo_2
@todo_2.item_at_offset(-1) # returns @todo


80
81
82
83
84
# File 'lib/simple_acts_as_list/model_additions.rb', line 80

def item_at_offset(offset)
  item_index = self.simple_acts_as_list_scope.index(self)
  index = item_index + offset
  index < 0 ? nil : self.simple_acts_as_list_scope[index]
end

#last_itemObject

Returns last list item



47
48
49
# File 'lib/simple_acts_as_list/model_additions.rb', line 47

def last_item
  self.simple_acts_as_list_scope.last
end

#last_item?Boolean

Returns true if the item is the last one of the list

Returns:

  • (Boolean)


37
38
39
# File 'lib/simple_acts_as_list/model_additions.rb', line 37

def last_item?
  self.simple_acts_as_list_scope.last == self
end

#next_itemObject

Returns next list item

Returns nil if the item is the last one of the list



59
60
61
62
# File 'lib/simple_acts_as_list/model_additions.rb', line 59

def next_item
  index = self.simple_acts_as_list_scope.index(self)
  self.simple_acts_as_list_scope[index + 1]
end

#previous_itemObject

Returns previous list item

Returns nil if the item is the first one of the list



67
68
69
70
# File 'lib/simple_acts_as_list/model_additions.rb', line 67

def previous_item
  index = self.simple_acts_as_list_scope.index(self)
  index == 0 ? nil : self.simple_acts_as_list_scope[index - 1]
end