Class: MyTodo::Todo

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/my_todo.rb

Overview

Todo tasks using thor gem

Instance Method Summary collapse

Instance Method Details

#createObject



82
83
84
85
86
87
88
89
90
# File 'lib/my_todo.rb', line 82

def create
  begin
    say 'ToDo CREATED!'
    create_item(options)
    output @item
  rescue ActiveRecord::RecordInvalid => e
    say e.message
  end
end

#delete(id) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/my_todo.rb', line 108

def delete(id)
  begin
    item = Item.find_by_id(id)
    output item
    item.destroy!
    say 'ToDo DESTROYED!'
  rescue StandardError => e
    say e.message
  end
end

#list(status = nil) ⇒ Object



71
72
73
74
75
# File 'lib/my_todo.rb', line 71

def list(status=nil)
  items = all_items(status)
  say "ToDos FOUND: #{items.count}"
  items.each {|item| output(item)}
end

#noteObject



154
155
156
157
158
159
160
161
# File 'lib/my_todo.rb', line 154

def note
  begin
    item.notes.create(body: options[:body])
    output item.reload
  rescue StandardError => e
    say e.message
  end
end

#rm_noteObject



166
167
168
169
170
171
172
173
# File 'lib/my_todo.rb', line 166

def rm_note
  begin
    item.notes.where(id: options[:noteid]).first.destroy!
    output item.reload
  rescue StandardError => e
    say e.message
  end
end

#rm_tagObject



142
143
144
145
146
147
148
149
# File 'lib/my_todo.rb', line 142

def rm_tag
  begin
    item.tags.where(name: options[:tag]).first.destroy!
    output item.reload
  rescue StandardError => e
    say e.message
  end
end

#search(text) ⇒ Object



120
121
122
123
124
125
# File 'lib/my_todo.rb', line 120

def search(text)
  items = Item.ransack(body_or_detailed_status_or_tags_name_or_notes_body_cont: text).result
  say "ToDos FOUND: #{items.count}"
  say "Search based on ransack search: body_or_detailed_status_or_tags_name_or_notes_body_cont"
  items.each {|item| output item}
end

#tagObject



130
131
132
133
134
135
136
137
# File 'lib/my_todo.rb', line 130

def tag
  begin
    item.tags.create!(name: options[:tag])
    output item.reload
  rescue StandardError => e
    say e.message
  end
end

#updateObject



97
98
99
100
101
102
103
104
105
# File 'lib/my_todo.rb', line 97

def update
  begin
    update_item(options)
    say 'ToDo UPDATED!'
    output item
  rescue ActiveRecord::RecordInvalid => e
    say e.message
  end
end