diff options
author | Paco Guzman <pacoguzmanp@gmail.com> | 2016-06-03 09:06:44 +0200 |
---|---|---|
committer | Paco Guzman <pacoguzmanp@gmail.com> | 2016-06-03 11:04:50 +0200 |
commit | a2e2570773da88fd5c3fe2ece798ce47c982072f (patch) | |
tree | 168be0112a856d6964f23efbf044fbcfa4297ff0 | |
parent | 847cde18fa6d955ec962f72e3395045fc124f17c (diff) | |
download | gitlab-ce-18034-cache-todo-counter-with-todo-service.tar.gz |
TodoService from TodosController18034-cache-todo-counter-with-todo-service
-rw-r--r-- | app/controllers/dashboard/todos_controller.rb | 6 | ||||
-rw-r--r-- | app/services/todo_service.rb | 6 | ||||
-rw-r--r-- | spec/services/todo_service_spec.rb | 21 |
3 files changed, 28 insertions, 5 deletions
diff --git a/app/controllers/dashboard/todos_controller.rb b/app/controllers/dashboard/todos_controller.rb index d94059ff136..7842fb9ce63 100644 --- a/app/controllers/dashboard/todos_controller.rb +++ b/app/controllers/dashboard/todos_controller.rb @@ -6,8 +6,7 @@ class Dashboard::TodosController < Dashboard::ApplicationController end def destroy - todo.done - current_user.update_cache_todos_count + TodoService.new.mark_todos_as_done([todo], current_user) todo_notice = 'Todo was successfully marked as done.' @@ -21,8 +20,7 @@ class Dashboard::TodosController < Dashboard::ApplicationController end def destroy_all - @todos.each(&:done) - current_user.update_cache_todos_count + TodoService.new.mark_todos_as_done(@todos, current_user) respond_to do |format| format.html { redirect_to dashboard_todos_path, notice: 'All todos were marked as done.' } diff --git a/app/services/todo_service.rb b/app/services/todo_service.rb index 7ed5a90f709..68371dcdfa3 100644 --- a/app/services/todo_service.rb +++ b/app/services/todo_service.rb @@ -1,6 +1,6 @@ # TodoService class # -# Used for creating todos after certain user actions +# Used for creating/updating todos after certain user actions # # Ex. # TodoService.new.new_issue(issue, current_user) @@ -133,6 +133,10 @@ class TodoService end end + def mark_todos_as_done(todos, current_user) + todos.each(&:done).tap { current_user.update_cache_todos_count } + end + private def create_todos(users, attributes) diff --git a/spec/services/todo_service_spec.rb b/spec/services/todo_service_spec.rb index c608e56fc3f..befae2a30b2 100644 --- a/spec/services/todo_service_spec.rb +++ b/spec/services/todo_service_spec.rb @@ -159,6 +159,27 @@ describe TodoService, services: true do end end + describe '#mark_todos_as_done' do + it 'marks related todos for the user as done' do + first_todo = create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author) + second_todo = create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author) + + service.mark_todos_as_done([first_todo, second_todo], john_doe) + + expect(first_todo.reload).to be_done + expect(second_todo.reload).to be_done + end + + describe 'caching' do + it 'repopulates the cache' do + todo = create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author) + expect_repopulate_todos_count_cache_for(john_doe) + + service.mark_todos_as_done([todo], john_doe) + end + end + end + describe '#new_note' do let!(:first_todo) { create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author) } let!(:second_todo) { create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author) } |