From 8358f87a6b978b60d38b7d3cbdbcd24ba70c4ab5 Mon Sep 17 00:00:00 2001 From: fujiwara-e Date: Wed, 15 Oct 2025 14:01:48 +0900 Subject: [PATCH 1/2] Refactor task retrieval logic to handle 'only_todo' filter --- app/controllers/tasks_controller.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index ea0b5cfb..cafc3abb 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -30,11 +30,12 @@ def index @q = Task.joins(:state).ransack({combinator: 'and', groupings: search_check(params[:q][:content_or_assigner_screen_name_or_description_or_project_name_cont])}) @q.sorts = sort_check(params[:q][:s]) end - @tasks = @q.result.page(params[:page]).per(50).includes(:user, :state) - return unless params[:only_todo] == '1' - - @tasks = @tasks.where(task_state_id: TaskState.find_by(name: 'todo').id) + if params[:only_todo] == '1' + @tasks = Task.active.page(params[:page]).per(50).includes(:user, :state) + else + @tasks = @q.result.page(params[:page]).per(50).includes(:user, :state) + end end # GET /tasks/1 or /tasks/1.json From 999b5d7c00c52326e3959e8f4ede8653b73e73bf Mon Sep 17 00:00:00 2001 From: fujiwara-e Date: Tue, 28 Oct 2025 15:29:57 +0900 Subject: [PATCH 2/2] Fix correct sorting and filtering logic for task index view --- app/controllers/tasks_controller.rb | 13 +++++++------ app/views/tasks/index.html.erb | 15 +++++++++++---- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index cafc3abb..ca3d767f 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -15,9 +15,9 @@ def search_check(param) def sort_check(param) if param.present? sort_column = [] - sort_column << "state_priority DESC" << param + sort_column << "state.priority DESC" << param else - "state_priority DESC" + "state.priority DESC" end end @@ -25,17 +25,18 @@ def sort_check(param) def index if params[:q].nil? @q = Task.joins(:state).ransack(params[:q]) - @q.sorts = ["state_priority DESC", "due_at ASC"] + @q.sorts = ["state.priority DESC", "due_at ASC"] else @q = Task.joins(:state).ransack({combinator: 'and', groupings: search_check(params[:q][:content_or_assigner_screen_name_or_description_or_project_name_cont])}) @q.sorts = sort_check(params[:q][:s]) end + tasks_query = @q.result if params[:only_todo] == '1' - @tasks = Task.active.page(params[:page]).per(50).includes(:user, :state) - else - @tasks = @q.result.page(params[:page]).per(50).includes(:user, :state) + tasks_query = tasks_query.merge(Task.active) end + + @tasks = tasks_query.page(params[:page]).per(50).includes(:user, :state) end # GET /tasks/1 or /tasks/1.json diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb index fa431f55..f80d0a93 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -22,6 +22,7 @@ class: "form-control me-2", placeholder: '検索ワードを入力して下さい', style: "flex: 1;" %> + <%= hidden_field_tag :only_todo, params[:only_todo] if params[:only_todo].present? %> <%= f.submit "検索", class: "btn btn-primary" %> <% end %> @@ -30,10 +31,16 @@ <%= form_with url: request.path, method: :get, local: true, html: { class: "d-flex" } do %> - <%= check_box_tag :only_todo, "1", params[:only_todo] == "1", onchange: "this.form.submit()" %> -
- todoのみ -
+ <%= check_box_tag :only_todo, "1", params[:only_todo] == "1", onchange: "this.form.submit()" %> +
+ todoのみ +
+ + <% if params[:q].present? %> + <% params[:q].each do |key, value| %> + <%= hidden_field_tag "q[#{key}]", value %> + <% end %> + <% end %> <% end %> <%= render @tasks %>