Commit 2d633969 authored by Felipe Artur's avatar Felipe Artur

Move controllers to allow reusing

parent ad36ddfb
...@@ -57,7 +57,8 @@ $(() => { ...@@ -57,7 +57,8 @@ $(() => {
data: { data: {
state: Store.state, state: Store.state,
loading: true, loading: true,
endpoint: $boardApp.dataset.endpoint, boardsEndpoint: $boardApp.dataset.boardsEndpoint,
listsEndpoint: $boardApp.dataset.listsEndpoint,
boardId: $boardApp.dataset.boardId, boardId: $boardApp.dataset.boardId,
disabled: $boardApp.dataset.disabled === 'true', disabled: $boardApp.dataset.disabled === 'true',
issueLinkBase: $boardApp.dataset.issueLinkBase, issueLinkBase: $boardApp.dataset.issueLinkBase,
...@@ -83,8 +84,8 @@ $(() => { ...@@ -83,8 +84,8 @@ $(() => {
Store.updateFiltersUrl(true); Store.updateFiltersUrl(true);
} }
gl.boardService = new BoardService(this.endpoint, this.bulkUpdatePath, this.boardId); gl.boardService = new BoardService(this.boardsEndpoint, this.listsEndpoint, this.bulkUpdatePath, this.boardId);
Store.rootPath = this.endpoint; Store.rootPath = this.boardsEndpoint;
this.filterManager = new FilteredSearchBoards(Store.filter, true, [(this.milestoneTitle ? 'milestone' : null)]); this.filterManager = new FilteredSearchBoards(Store.filter, true, [(this.milestoneTitle ? 'milestone' : null)]);
this.filterManager.setup(); this.filterManager.setup();
......
...@@ -3,17 +3,19 @@ ...@@ -3,17 +3,19 @@
import Vue from 'vue'; import Vue from 'vue';
class BoardService { class BoardService {
constructor (root, bulkUpdatePath, boardId) { constructor (root, listsEndpoint, bulkUpdatePath, boardId) {
alert(listsEndpoint)
alert(root)
this.boards = Vue.resource(`${root}{/id}.json`, {}, { this.boards = Vue.resource(`${root}{/id}.json`, {}, {
issues: { issues: {
method: 'GET', method: 'GET',
url: `${root}/${boardId}/issues.json` url: `${root}/${boardId}/issues.json`
} }
}); });
this.lists = Vue.resource(`${root}/${boardId}/lists{/id}`, {}, { this.lists = Vue.resource(`${listsEndpoint}{/id}`, {}, {
generate: { generate: {
method: 'POST', method: 'POST',
url: `${root}/${boardId}/lists/generate.json` url: `${listsEndpoint}/generate.json`
} }
}); });
this.issue = Vue.resource(`${root}/${boardId}/issues{/id}`, {}); this.issue = Vue.resource(`${root}/${boardId}/issues{/id}`, {});
......
module Boards
class ApplicationController < ::ApplicationController
respond_to :json
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
private
def record_not_found(exception)
render json: { error: exception.message }, status: :not_found
end
end
end
module Boards
class ListsController < Boards::ApplicationController
# before_action :authorize_admin_list!, only: [:create, :update, :destroy, :generate]
# before_action :authorize_read_list!, only: [:index]
def index
lists = ::Boards::Lists::ListService.new(project, current_user).execute(board)
render json: serialize_as_json(lists)
end
def create
list = ::Boards::Lists::CreateService.new(project, current_user, list_params).execute(board)
if list.valid?
render json: serialize_as_json(list)
else
render json: list.errors, status: :unprocessable_entity
end
end
def update
list = board.lists.movable.find(params[:id])
service = ::Boards::Lists::MoveService.new(project, current_user, move_params)
if service.execute(list)
head :ok
else
head :unprocessable_entity
end
end
def destroy
list = board.lists.destroyable.find(params[:id])
service = ::Boards::Lists::DestroyService.new(project, current_user)
if service.execute(list)
head :ok
else
head :unprocessable_entity
end
end
def generate
service = ::Boards::Lists::GenerateService.new(project, current_user)
if service.execute(board)
render json: serialize_as_json(board.lists.movable)
else
head :unprocessable_entity
end
end
private
def authorize_admin_list!
return render_403 unless can?(current_user, :admin_list, project)
end
def authorize_read_list!
return render_403 unless can?(current_user, :read_list, project)
end
def board
@board ||= project.boards.find(params[:board_id])
end
def list_params
params.require(:list).permit(:label_id)
end
def move_params
params.require(:list).permit(:position)
end
def serialize_as_json(resource)
resource.as_json(
only: [:id, :list_type, :position],
methods: [:title],
label: true
)
end
end
end
module Projects
module Boards
class ApplicationController < Projects::ApplicationController
respond_to :json
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
private
def record_not_found(exception)
render json: { error: exception.message }, status: :not_found
end
end
end
end
module Projects
module Boards
class ListsController < Boards::ApplicationController
before_action :authorize_admin_list!, only: [:create, :update, :destroy, :generate]
before_action :authorize_read_list!, only: [:index]
def index
lists = ::Boards::Lists::ListService.new(project, current_user).execute(board)
render json: serialize_as_json(lists)
end
def create
list = ::Boards::Lists::CreateService.new(project, current_user, list_params).execute(board)
if list.valid?
render json: serialize_as_json(list)
else
render json: list.errors, status: :unprocessable_entity
end
end
def update
list = board.lists.movable.find(params[:id])
service = ::Boards::Lists::MoveService.new(project, current_user, move_params)
if service.execute(list)
head :ok
else
head :unprocessable_entity
end
end
def destroy
list = board.lists.destroyable.find(params[:id])
service = ::Boards::Lists::DestroyService.new(project, current_user)
if service.execute(list)
head :ok
else
head :unprocessable_entity
end
end
def generate
service = ::Boards::Lists::GenerateService.new(project, current_user)
if service.execute(board)
render json: serialize_as_json(board.lists.movable)
else
head :unprocessable_entity
end
end
private
def authorize_admin_list!
return render_403 unless can?(current_user, :admin_list, project)
end
def authorize_read_list!
return render_403 unless can?(current_user, :read_list, project)
end
def board
@board ||= project.boards.find(params[:board_id])
end
def list_params
params.require(:list).permit(:label_id)
end
def move_params
params.require(:list).permit(:position)
end
def serialize_as_json(resource)
resource.as_json(
only: [:id, :list_type, :position],
methods: [:title],
label: true
)
end
end
end
end
...@@ -32,6 +32,7 @@ class Projects::BoardsController < Projects::ApplicationController ...@@ -32,6 +32,7 @@ class Projects::BoardsController < Projects::ApplicationController
def assign_endpoint_vars def assign_endpoint_vars
@boards_endpoint = project_boards_path(project) @boards_endpoint = project_boards_path(project)
@issues_path = project_issues_path(project) @issues_path = project_issues_path(project)
@bulk_issues_path = bulk_update_project_issues_path(@project) @bulk_issues_path = bulk_update_project_issues_path(@project)
end end
......
...@@ -5,7 +5,8 @@ module BoardsHelper ...@@ -5,7 +5,8 @@ module BoardsHelper
board = @board || @boards.first board = @board || @boards.first
{ {
endpoint: @boards_endpoint, boards_endpoint: @boards_endpoint,
lists_endpoint: board_lists_path(board),
board_id: board.id, board_id: board.id,
board_milestone_title: board&.milestone&.title, board_milestone_title: board&.milestone&.title,
disabled: "#{!can?(current_user, :admin_list, @project)}", # Create this permission for groups( if needed ) disabled: "#{!can?(current_user, :admin_list, @project)}", # Create this permission for groups( if needed )
......
...@@ -96,5 +96,15 @@ Rails.application.routes.draw do ...@@ -96,5 +96,15 @@ Rails.application.routes.draw do
draw :test if Rails.env.test? draw :test if Rails.env.test?
resources :boards do
resources :lists, module: :boards, only: [:index, :create, :update, :destroy] do
collection do
post :generate
end
resources :issues, only: [:index, :create, :update]
end
end
get '*unmatched_route', to: 'application#route_not_found' get '*unmatched_route', to: 'application#route_not_found'
end end
...@@ -382,14 +382,6 @@ constraints(ProjectUrlConstrainer.new) do ...@@ -382,14 +382,6 @@ constraints(ProjectUrlConstrainer.new) do
resources :boards, only: [:index, :show, :create, :update, :destroy] do resources :boards, only: [:index, :show, :create, :update, :destroy] do
scope module: :boards do scope module: :boards do
resources :issues, only: [:index, :update] resources :issues, only: [:index, :update]
resources :lists, only: [:index, :create, :update, :destroy] do
collection do
post :generate
end
resources :issues, only: [:index, :create]
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