Class: TodoLists::ItemsController
- Inherits:
-
Object
- Object
- TodoLists::ItemsController
- Defined in:
- lib/todo_lists/controllers/items_controller.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#last_input ⇒ Object
Returns the value of attribute last_input.
-
#list ⇒ Object
Returns the value of attribute list.
Instance Method Summary collapse
- #delete ⇒ Object
- #done ⇒ Object
- #edit ⇒ Object
- #get_input ⇒ Object
- #help ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
Instance Attribute Details
#last_input ⇒ Object
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 |
#list ⇒ Object
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
#delete ⇒ Object
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 |
#done ⇒ Object
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 |
#edit ⇒ Object
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_input ⇒ Object
70 71 72 |
# File 'lib/todo_lists/controllers/items_controller.rb', line 70 def get_input @last_input = gets.strip end |
#help ⇒ Object
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 |
#index ⇒ Object
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 |
#new ⇒ Object
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 |