Module: RubyTodo::StatusFilteringHelpers

Included in:
AIAssistantCommand
Defined in:
lib/ruby_todo/commands/ai_assistant.rb

Overview

Module for status-based task filtering

Instance Method Summary collapse

Instance Method Details

#handle_filtered_tasks(cli, status_text) ⇒ Object

Helper method to process the status and delegate to handle_status_filtered_tasks



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 80

def handle_filtered_tasks(cli, status_text)
  # For debugging
  puts "Debug - Handling filtered tasks with status: #{status_text}"

  # List available notebooks to help debug
  notebooks = RubyTodo::Notebook.all
  puts "Debug - Available notebooks: #{notebooks.map(&:name).join(", ")}"

  # Normalize the status by removing extra spaces and replacing dashes
  status = normalize_status(status_text)
  puts "Debug - Normalized status: #{status}"

  handle_status_filtered_tasks(cli, status)
end

#handle_status_filtered_tasks(cli, status) ⇒ Object

Helper method to handle tasks filtered by status



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 114

def handle_status_filtered_tasks(cli, status)
  # Normalize status to ensure 'in progress' becomes 'in_progress'
  normalized_status = normalize_status(status)

  # Set options for filtering by status - this is expected by the tests
  cli.options = { status: normalized_status }

  # Get default notebook
  notebook = RubyTodo::Notebook.default_notebook || RubyTodo::Notebook.first

  if notebook
    # Use the CLI's task_list method to ensure consistent output format
    cli.task_list(notebook.name)

    # If no tasks were found in the default notebook, search across all notebooks
    all_matching_tasks = RubyTodo::Task.where(status: normalized_status)

    if all_matching_tasks.any?
      # Group tasks by notebook
      tasks_by_notebook = {}
      all_matching_tasks.each do |task|
        matching_notebook = RubyTodo::Notebook.find_by(id: task.notebook_id)
        next unless matching_notebook && matching_notebook.id != notebook.id

        tasks_by_notebook[matching_notebook.name] ||= []
        tasks_by_notebook[matching_notebook.name] << task
      end

      # Show tasks from other notebooks
      tasks_by_notebook.each do |notebook_name, tasks|
        say "Additional tasks in '#{notebook_name}' with status '#{status}':"

        # Use a format that matches the CLI's task_list output
        # which has the ID: Title (Status) format expected by the tests
        tasks.each do |task|
          say "#{task.id}: #{task.title} (#{task.status})"
        end
      end
    end
  else
    say "No notebooks found. Create a notebook first.".yellow
  end
end

#handle_status_filtering(prompt, cli) ⇒ Object

Methods for filtering tasks by status



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 159

def handle_status_filtering(prompt, cli)
  # Status patterns - simple helper to extract these checks
  if prompt.match?(tasks_with_status_regex)
    status_match = prompt.match(tasks_with_status_regex)
    handle_filtered_tasks(cli, status_match[1])
    return true
  elsif prompt.match?(tasks_by_status_regex)
    status_match = prompt.match(tasks_by_status_regex)
    handle_filtered_tasks(cli, status_match[1])
    return true
  elsif prompt.match?(status_prefix_tasks_regex)
    status_match = prompt.match(status_prefix_tasks_regex)
    handle_filtered_tasks(cli, status_match[1])
    return true
  end

  false
end

#normalize_status(status) ⇒ Object

Normalize status string (convert “in progress” to “in_progress”, etc.)



179
180
181
182
183
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 179

def normalize_status(status)
  status.to_s.downcase.strip
        .gsub(/[-\s]+/, "_") # Replace dashes or spaces with underscore
        .gsub(/^in_?_?progress$/, "in_progress") # Normalize in_progress variations
end

#status_prefix_tasks_regexObject



108
109
110
111
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 108

def status_prefix_tasks_regex
  /(?:list|show|get|display|see).*(?:all)?\s*
   (in[\s_-]?progress|todo|done|archived)(?:\s+status)?\s+tasks/ix
end

#tasks_by_status_regexObject



102
103
104
105
106
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 102

def tasks_by_status_regex
  /(?:list|show|get|display|see).*(?:all)?\s*tasks\s+
   (?:with|by|having)?\s*status\s+
   (in[\s_-]?progress|todo|done|archived)/ix
end

#tasks_with_status_regexObject

Status-based task filtering patterns



96
97
98
99
100
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 96

def tasks_with_status_regex
  /(?:list|show|get|display|see).*(?:all)?\s*tasks\s+
   (?:with|that\s+(?:are|have)|having|in|that\s+are\s+in)\s+
   (in[\s_-]?progress|todo|done|archived)(?:\s+status)?/ix
end