gitlab-org--gitlab-foss/app/controllers/dashboard/todos_controller.rb

101 lines
2.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-02-20 08:59:59 -05:00
class Dashboard::TodosController < Dashboard::ApplicationController
include ActionView::Helpers::NumberHelper
include PaginatedCollection
include Analytics::UniqueVisitsHelper
before_action :authorize_read_project!, only: :index
before_action :authorize_read_group!, only: :index
before_action :find_todos, only: [:index, :destroy_all]
2016-02-20 14:53:25 -05:00
2016-02-20 08:59:59 -05:00
def index
2016-07-26 17:21:20 -04:00
@sort = params[:sort]
2016-12-20 13:52:09 -05:00
@todos = @todos.page(params[:page])
@todos = @todos.with_entity_associations
return if redirect_out_of_range(@todos, todos_page_count(@todos))
2016-02-20 08:59:59 -05:00
end
def destroy
todo = current_user.todos.find(params[:id])
TodoService.new.resolve_todo(todo, current_user, resolved_by_action: :mark_done)
2016-02-20 08:59:59 -05:00
respond_to do |format|
format.html do
redirect_to dashboard_todos_path,
status: :found,
notice: _('To-do item successfully marked as done.')
end
format.js { head :ok }
format.json { render json: todos_counts }
2016-02-20 08:59:59 -05:00
end
end
2016-02-20 14:53:25 -05:00
def destroy_all
updated_ids = TodoService.new.resolve_todos(@todos, current_user, resolved_by_action: :mark_all_done)
2016-02-20 14:53:25 -05:00
respond_to do |format|
format.html { redirect_to dashboard_todos_path, status: :found, notice: _('Everything on your to-do list is marked as done.') }
format.js { head :ok }
format.json { render json: todos_counts.merge(updated_ids: updated_ids) }
2016-02-20 14:53:25 -05:00
end
end
def restore
TodoService.new.restore_todo(current_user.todos.find(params[:id]), current_user)
render json: todos_counts
end
def bulk_restore
TodoService.new.restore_todos(current_user.todos.for_ids(params[:ids]), current_user)
render json: todos_counts
end
2016-02-20 08:59:59 -05:00
private
def authorize_read_project!
project_id = params[:project_id]
if project_id.present?
project = Project.find(project_id)
render_404 unless can?(current_user, :read_project, project)
end
end
def authorize_read_group!
group_id = params[:group_id]
if group_id.present?
group = Group.find(group_id)
render_404 unless can?(current_user, :read_group, group)
end
end
2016-02-20 14:53:25 -05:00
def find_todos
@todos ||= TodosFinder.new(current_user, todo_params).execute
2016-02-20 14:53:25 -05:00
end
def todos_counts
{
count: current_user.todos_pending_count,
done_count: current_user.todos_done_count
}
end
def todos_page_count(todos)
if todo_params.except(:sort, :page).empty?
(current_user.todos_pending_count.to_f / todos.limit_value).ceil
else
todos.total_pages
end
end
def todo_params
params.permit(:action_id, :author_id, :project_id, :type, :sort, :state, :group_id)
end
2016-02-20 08:59:59 -05:00
end