Class: TodoLists::ItemsController

Inherits:
Object
  • Object
show all
Defined in:
lib/todo_lists/controllers/items_controller.rb

Constant Summary collapse

Item =
TodoLists::Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_inputObject

Returns the value of attribute last_input.



3
4
5
# File 'lib/todo_lists/controllers/items_controller.rb', line 3

def last_input
  @last_input
end

#listObject

Returns the value of attribute list.



3
4
5
# File 'lib/todo_lists/controllers/items_controller.rb', line 3

def list
  @list
end

Instance Method Details

#deleteObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/todo_lists/controllers/items_controller.rb', line 36

def delete
  item_index = last_input.split(' ')[1]
  item = Item.find_by_index_and_list_id(index: item_index, list_id: list.id)
  if item
    print 'Are you sure? Enter \'y/n\': '
    get_input

    if last_input.downcase == 'y'
      item.destroy
    end

  end
end

#doneObject



16
17
18
19
20
21
22
23
# File 'lib/todo_lists/controllers/items_controller.rb', line 16

def done
  item_index = last_input.split(' ')[1]
  item = Item.find_by_index_and_list_id(index: item_index, list_id: list.id)
  if item
    item.done = true
    item.save
  end
end

#editObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/todo_lists/controllers/items_controller.rb', line 25

def edit
  item_index = last_input.split(' ')[1]
  item = Item.find_by_index_and_list_id(index: item_index, list_id: list.id)
  if item
    print 'Enter new content: '
    get_input
    item.content = last_input
    item.save
  end
end

#get_inputObject



70
71
72
# File 'lib/todo_lists/controllers/items_controller.rb', line 70

def get_input
  @last_input = gets.strip
end

#helpObject



7
8
9
10
11
12
13
14
# File 'lib/todo_lists/controllers/items_controller.rb', line 7

def help
  puts "\nOptions available"
  puts '/edit {item_id}'
  puts '/delete {item_id}'
  puts '/done {item_id} (mark item complete)'
  puts '/exit'
  puts
end

#indexObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/todo_lists/controllers/items_controller.rb', line 50

def index
  if list.items.count == 0
    puts "\nYou have no items in '#{@list.title.capitalize}'."
  else
    puts "\n---------------#{list.title.capitalize}---------------"
    puts '#   item'
    puts '--   -----'
    list.items.reload.each.with_index(1) do |item, index|
      puts "#{index}: #{item.content.capitalize} [#{'X' if item.done?}]"
    end
  end
end

#newObject



63
64
65
66
67
68
# File 'lib/todo_lists/controllers/items_controller.rb', line 63

def new

  item = Item.new(list: list, content: last_input)

  item.save
end