Commit 39426213 authored by Robert Schilling's avatar Robert Schilling

Expose target, filter by state as string

parent fd9cd5ae
...@@ -25,6 +25,7 @@ class TodosFinder ...@@ -25,6 +25,7 @@ class TodosFinder
def execute def execute
items = current_user.todos items = current_user.todos
items = by_action_id(items) items = by_action_id(items)
items = by_action(items)
items = by_author(items) items = by_author(items)
items = by_project(items) items = by_project(items)
items = by_state(items) items = by_state(items)
...@@ -43,6 +44,18 @@ class TodosFinder ...@@ -43,6 +44,18 @@ class TodosFinder
params[:action_id] params[:action_id]
end end
def to_action_id
Todo::ACTION_NAMES.key(action.to_sym)
end
def action?
action.present? && to_action_id
end
def action
params[:action]
end
def author? def author?
params[:author_id].present? params[:author_id].present?
end end
...@@ -96,6 +109,14 @@ class TodosFinder ...@@ -96,6 +109,14 @@ class TodosFinder
params[:type] params[:type]
end end
def by_action(items)
if action?
items = items.where(action: to_action_id)
end
items
end
def by_action_id(items) def by_action_id(items)
if action_id? if action_id?
items = items.where(action: action_id) items = items.where(action: action_id)
......
This diff is collapsed.
...@@ -277,16 +277,16 @@ module API ...@@ -277,16 +277,16 @@ module API
expose :project, using: Entities::BasicProjectDetails expose :project, using: Entities::BasicProjectDetails
expose :author, using: Entities::UserBasic expose :author, using: Entities::UserBasic
expose :action_name expose :action_name
expose :target_id
expose :target_type expose :target_type
expose :target_reference do |todo, options|
todo.target.to_reference expose :target do |todo, options|
Entities.const_get(todo.target_type).represent(todo.target, options)
end end
expose :target_url do |todo, options| expose :target_url do |todo, options|
target_type = todo.target_type.underscore target_type = todo.target_type.underscore
target_url = "namespace_project_#{target_type}_url" target_url = "namespace_project_#{target_type}_url"
target_anchor = "note_#{todo.note_id}" if todo.note.present? target_anchor = "note_#{todo.note_id}" if todo.note_id?
Gitlab::Application.routes.url_helpers.public_send(target_url, Gitlab::Application.routes.url_helpers.public_send(target_url,
todo.project.namespace, todo.project, todo.target, anchor: target_anchor) todo.project.namespace, todo.project, todo.target, anchor: target_anchor)
......
...@@ -18,7 +18,7 @@ module API ...@@ -18,7 +18,7 @@ module API
get do get do
todos = find_todos todos = find_todos
present paginate(todos), with: Entities::Todo present paginate(todos), with: Entities::Todo, current_user: current_user
end end
# Mark a todo as done # Mark a todo as done
...@@ -33,7 +33,7 @@ module API ...@@ -33,7 +33,7 @@ module API
todo = current_user.todos.find(params[:id]) todo = current_user.todos.find(params[:id])
todo.done todo.done
present todo, with: Entities::Todo present todo, with: Entities::Todo, current_user: current_user
end end
# Mark all todos as done # Mark all todos as done
...@@ -45,7 +45,7 @@ module API ...@@ -45,7 +45,7 @@ module API
todos = find_todos todos = find_todos
todos.each(&:done) todos.each(&:done)
present paginate(Kaminari.paginate_array(todos)), with: Entities::Todo present paginate(Kaminari.paginate_array(todos)), with: Entities::Todo, current_user: current_user
end end
end end
end end
......
...@@ -9,7 +9,7 @@ describe API::Todos, api: true do ...@@ -9,7 +9,7 @@ describe API::Todos, api: true do
let(:author_2) { create(:user) } let(:author_2) { create(:user) }
let(:john_doe) { create(:user, username: 'john_doe') } let(:john_doe) { create(:user, username: 'john_doe') }
let(:merge_request) { create(:merge_request, source_project: project_1) } let(:merge_request) { create(:merge_request, source_project: project_1) }
let!(:pending_1) { create(:todo, project: project_1, author: author_1, user: john_doe) } let!(:pending_1) { create(:todo, :mentioned, project: project_1, author: author_1, user: john_doe) }
let!(:pending_2) { create(:todo, project: project_2, author: author_2, user: john_doe) } let!(:pending_2) { create(:todo, project: project_2, author: author_2, user: john_doe) }
let!(:pending_3) { create(:todo, project: project_1, author: author_2, user: john_doe, target: merge_request) } let!(:pending_3) { create(:todo, project: project_1, author: author_2, user: john_doe, target: merge_request) }
let!(:done) { create(:todo, :done, project: project_1, author: author_1, user: john_doe) } let!(:done) { create(:todo, :done, project: project_1, author: author_1, user: john_doe) }
...@@ -38,9 +38,8 @@ describe API::Todos, api: true do ...@@ -38,9 +38,8 @@ describe API::Todos, api: true do
expect(json_response[0]['id']).to eq(pending_3.id) expect(json_response[0]['id']).to eq(pending_3.id)
expect(json_response[0]['project']).to be_a Hash expect(json_response[0]['project']).to be_a Hash
expect(json_response[0]['author']).to be_a Hash expect(json_response[0]['author']).to be_a Hash
expect(json_response[0]['target_id']).to be_present
expect(json_response[0]['target_type']).to be_present expect(json_response[0]['target_type']).to be_present
expect(json_response[0]['target_reference']).to be_present expect(json_response[0]['target']).to be_a Hash
expect(json_response[0]['target_url']).to be_present expect(json_response[0]['target_url']).to be_present
expect(json_response[0]['body']).to be_present expect(json_response[0]['body']).to be_present
expect(json_response[0]['state']).to eq('pending') expect(json_response[0]['state']).to eq('pending')
...@@ -87,6 +86,16 @@ describe API::Todos, api: true do ...@@ -87,6 +86,16 @@ describe API::Todos, api: true do
expect(json_response.length).to eq(1) expect(json_response.length).to eq(1)
end end
end end
context 'and using the action filter' do
it 'filters based on action param' do
get api('/todos', john_doe), { action: 'mentioned' }
expect(response.status).to eq(200)
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
end
end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment