Refactor text inventory type

This commit is contained in:
Mojca Lorber 2019-10-21 13:01:31 +02:00 committed by Urban Rotnik
parent 37b7d5aa64
commit 305104f639
9 changed files with 337 additions and 17 deletions

View file

@ -144,8 +144,8 @@ var RepositoryColumns = (function() {
}); });
} }
function insertNewListItem(column) { function insertNewListItem(id, column) {
var html = `<li class="list-group-item row" data-id="${column.id}" var html = `<li class="list-group-item row" data-id="${id}"
data-destroy-url="${column.destroy_html_url}" data-destroy-url="${column.destroy_html_url}"
data-edit-url="${column.edit_url}"> data-edit-url="${column.edit_url}">
<div class="col-xs-8"> <div class="col-xs-8">
@ -154,12 +154,12 @@ var RepositoryColumns = (function() {
<div class="col-xs-4"> <div class="col-xs-4">
<span class="controlls pull-right"> <span class="controlls pull-right">
<button class="btn btn-default edit-repo-column" data-action="edit"> <button class="btn btn-default edit-repo-column" data-action="edit">
<span class="fas fa-pencil-alt"></span>&nbsp; <span class="fas fa-pencil-alt"></span>
<%= I18n.t "libraries.repository_columns.index.edit_column" %> ${I18n.t('libraries.repository_columns.index.edit_column')}
</button>&nbsp; </button>&nbsp;
<button class="btn btn-default delete-repo-column" data-action="destroy"> <button class="btn btn-default delete-repo-column" data-action="destroy">
<span class="fas fa-trash-alt"></span>&nbsp; <span class="fas fa-trash-alt"></span>
<%= I18n.t "libraries.repository_columns.index.delete_column" %> ${I18n.t('libraries.repository_columns.index.delete_column')}
</button> </button>
</span> </span>
</div> </div>
@ -176,10 +176,13 @@ var RepositoryColumns = (function() {
$('#new-repo-column-submit').on('click', function() { $('#new-repo-column-submit').on('click', function() {
var url = $('#repository_column_data_type').find(':selected').data('create-url'); var url = $('#repository_column_data_type').find(':selected').data('create-url');
var params = { repository_column: { name: $('#repository_column_name').val() } }; var params = { repository_column: { name: $('#repository_column_name').val() } };
$.post(url, params, (data) => { $.post(url, params, (responseData) => {
insertNewListItem(data); var data = responseData.data;
HelperModule.flashAlertMsg(data.message, 'success'); insertNewListItem(data.id, data.attributes);
HelperModule.flashAlertMsg(data.attributes.message, 'success');
modalHtml.modal('hide'); modalHtml.modal('hide');
}).error((error) => {
$('#new_repository_column').renderFormErrors('repository_column', error.responseJSON.repository_column, true);
}); });
}); });
} }

View file

@ -42,7 +42,7 @@ var renderFormError = function(ev, input, errMsgs, clearErr, errAttributes) {
})).join('<br />'); })).join('<br />');
var $errSpan = "<span class='help-block'" + var $errSpan = "<span class='help-block'" +
errAttributes + '>' + errorText + '</span>'; errAttributes + '>' + errorText + '</span>';
$formGroup.append($errSpan); $(input).after($errSpan);
} }
var $parent; var $parent;

View file

@ -0,0 +1,60 @@
# frozen_string_literal: true
module RepositoryColumns
class TextColumnsController < BaseColumnsController
include InputSanitizeHelper
before_action :load_column, only: %i(update destroy)
before_action :check_create_permissions, only: :create
before_action :check_manage_permissions, only: %i(update destroy)
def create
byebug
service = RepositoryColumns::CreateColumnService
.call(user: current_user, repository: @repository, team: current_team,
column_type: Extends::REPOSITORY_DATA_TYPES[:RepositoryTextValue],
params: repository_column_params)
if service.succeed?
render json: service.column,
serializer: RepositoryColumnSerializer,
status: :created
else
render json: service.errors, status: :unprocessable_entity
end
end
def update
byebug
service = RepositoryColumns::UpdateColumnService
.call(user: current_user,
team: current_team,
column: @repository_column,
params: repository_column_params)
if service.succeed?
render json: service.column, status: :ok
else
render json: service.errors, status: :unprocessable_entity
end
end
def destroy
byebug
service = RepositoryColumns::DeleteColumnService
.call(user: current_user, team: current_team, column: @repository_column)
if service.succeed?
render json: {}, status: :ok
else
render json: service.errors, status: :unprocessable_entity
end
end
private
def repository_column_params
params.require(:repository_column).permit(:name)
end
end
end

View file

@ -0,0 +1,23 @@
# frozen_string_literal: true
class RepositoryColumnSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
attributes :id, :name, :message, :edit_url, :update_url, :destroy_html_url
def message
I18n.t('libraries.repository_columns.create.success_flash', name: object.name)
end
def edit_url
edit_repository_repository_column_path(object.repository, object)
end
def update_url
repository_repository_columns_text_column_path(object.repository, object)
end
def destroy_html_url
repository_columns_destroy_html_path(object.repository, object)
end
end

View file

@ -1 +1 @@
<%= 'text-test' %> <%# TO DO: add mandatory checkbox %>

View file

@ -25,7 +25,7 @@ RSpec.describe RepositoryColumns::AssetColumnsController, type: :controller do
before do before do
service = double('success_service') service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true) allow(service).to(receive(:succeed?)).and_return(true)
allow(service).to(receive(:column)).and_return(:repository_column) allow(service).to(receive(:column)).and_return(repository_column)
allow_any_instance_of(RepositoryColumns::CreateColumnService).to(receive(:call)).and_return(service) allow_any_instance_of(RepositoryColumns::CreateColumnService).to(receive(:call)).and_return(service)
end end
@ -101,7 +101,7 @@ RSpec.describe RepositoryColumns::AssetColumnsController, type: :controller do
before do before do
service = double('success_service') service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true) allow(service).to(receive(:succeed?)).and_return(true)
allow(service).to(receive(:column)).and_return(:repository_column) allow(service).to(receive(:column)).and_return(repository_column)
allow_any_instance_of(RepositoryColumns::UpdateColumnService).to(receive(:call)).and_return(service) allow_any_instance_of(RepositoryColumns::UpdateColumnService).to(receive(:call)).and_return(service)
end end

View file

@ -25,7 +25,7 @@ RSpec.describe RepositoryColumns::ListColumnsController, type: :controller do
before do before do
service = double('success_service') service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true) allow(service).to(receive(:succeed?)).and_return(true)
allow(service).to(receive(:column)).and_return(:repository_column) allow(service).to(receive(:column)).and_return(repository_column)
allow_any_instance_of(RepositoryColumns::CreateColumnService).to(receive(:call)).and_return(service) allow_any_instance_of(RepositoryColumns::CreateColumnService).to(receive(:call)).and_return(service)
end end
@ -101,7 +101,7 @@ RSpec.describe RepositoryColumns::ListColumnsController, type: :controller do
before do before do
service = double('success_service') service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true) allow(service).to(receive(:succeed?)).and_return(true)
allow(service).to(receive(:column)).and_return(:repository_column) allow(service).to(receive(:column)).and_return(repository_column)
allow_any_instance_of(RepositoryColumns::UpdateColumnService).to(receive(:call)).and_return(service) allow_any_instance_of(RepositoryColumns::UpdateColumnService).to(receive(:call)).and_return(service)
end end

View file

@ -25,7 +25,7 @@ RSpec.describe RepositoryColumns::StatusColumnsController, type: :controller do
before do before do
service = double('success_service') service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true) allow(service).to(receive(:succeed?)).and_return(true)
allow(service).to(receive(:column)).and_return(:repository_column) allow(service).to(receive(:column)).and_return(repository_column)
allow_any_instance_of(RepositoryColumns::CreateColumnService).to(receive(:call)).and_return(service) allow_any_instance_of(RepositoryColumns::CreateColumnService).to(receive(:call)).and_return(service)
end end
@ -101,7 +101,7 @@ RSpec.describe RepositoryColumns::StatusColumnsController, type: :controller do
before do before do
service = double('success_service') service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true) allow(service).to(receive(:succeed?)).and_return(true)
allow(service).to(receive(:column)).and_return(:repository_column) allow(service).to(receive(:column)).and_return(repository_column)
allow_any_instance_of(RepositoryColumns::UpdateColumnService).to(receive(:call)).and_return(service) allow_any_instance_of(RepositoryColumns::UpdateColumnService).to(receive(:call)).and_return(service)
end end

View file

@ -0,0 +1,234 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe RepositoryColumns::TextColumnsController, type: :controller do
login_user
let(:user) { subject.current_user }
let(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
let(:repository) { create :repository, created_by: user, team: team }
let(:repository_column) { create(:repository_column, :text_type, repository: repository) }
describe 'POST repository_text_columns, #create' do
let(:action) { post :create, params: params }
let(:params) do
{
repository_id: repository.id,
repository_column: {
name: 'name'
}
}
end
before do
service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true)
allow(service).to(receive(:column)).and_return(repository_column)
allow_any_instance_of(RepositoryColumns::CreateColumnService).to(receive(:call)).and_return(service)
end
context 'when column is created' do
it 'respons with status 201' do
action
expect(response).to(have_http_status(201))
end
end
context 'when repository is not found' do
let(:params) do
{
repository_id: -1,
repository_column: {
name: 'name'
}
}
end
it 'respons with status 404' do
action
expect(response).to(have_http_status(404))
end
end
context 'when user does not have permissions' do
before do
user_team.role = :guest
user_team.save
end
it 'respons with status 403' do
action
expect(response).to(have_http_status(403))
end
end
context 'when column cannot be created' do
before do
service = double('failure_service')
allow(service).to(receive(:succeed?)).and_return(false)
allow(service).to(receive(:errors)).and_return({})
allow_any_instance_of(RepositoryColumns::CreateColumnService).to(receive(:call)).and_return(service)
end
it 'respons with status 422' do
action
expect(response).to(have_http_status(422))
end
end
end
describe 'PUT repository_text_column, #update' do
let(:action) { patch :update, params: params }
let(:params) do
{
repository_id: repository.id,
id: repository_column.id,
repository_column: {
name: 'name'
}
}
end
before do
service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true)
allow(service).to(receive(:column)).and_return(repository_column)
allow_any_instance_of(RepositoryColumns::UpdateColumnService).to(receive(:call)).and_return(service)
end
context 'when column is updated' do
it 'respons with status 200' do
action
expect(response).to(have_http_status(200))
end
end
context 'when column is not found' do
let(:params) do
{
repository_id: repository.id,
id: -1,
repository_column: {
name: 'name'
}
}
end
it 'respons with status 404' do
action
expect(response).to(have_http_status(404))
end
end
context 'when user does not have permissions' do
before do
user_team.role = :guest
user_team.save
end
it 'respons with status 403' do
action
expect(response).to(have_http_status(403))
end
end
context 'when column cannot be updated' do
before do
service = double('failure_service')
allow(service).to(receive(:succeed?)).and_return(false)
allow(service).to(receive(:errors)).and_return({})
allow_any_instance_of(RepositoryColumns::UpdateColumnService).to(receive(:call)).and_return(service)
end
it 'respons with status 422' do
action
expect(response).to(have_http_status(422))
end
end
end
describe 'DELETE repository_text_column, #delete' do
let(:action) { delete :destroy, params: params }
let(:params) do
{
repository_id: repository.id,
id: repository_column.id
}
end
before do
service = double('success_service')
allow(service).to(receive(:succeed?)).and_return(true)
allow_any_instance_of(RepositoryColumns::DeleteColumnService).to(receive(:call)).and_return(service)
end
context 'when column deleted' do
it 'respons with status 200' do
action
expect(response).to(have_http_status(200))
end
end
context 'when column is not found' do
let(:params) do
{
repository_id: repository.id,
id: -1
}
end
it 'respons with status 404' do
action
expect(response).to(have_http_status(404))
end
end
context 'when user does not have permissions' do
before do
user_team.role = :guest
user_team.save
end
it 'respons with status 403' do
action
expect(response).to(have_http_status(403))
end
end
context 'when column cannot be deleted fails' do
before do
service = double('failure_service')
allow(service).to(receive(:succeed?)).and_return(false)
allow(service).to(receive(:errors)).and_return({})
allow_any_instance_of(RepositoryColumns::DeleteColumnService).to(receive(:call)).and_return(service)
end
it 'respons with status 422' do
action
expect(response).to(have_http_status(422))
end
end
end
end