diff --git a/app/assets/javascripts/repository_columns/index.js b/app/assets/javascripts/repository_columns/index.js index e3fb4d7c3..f74ce9773 100644 --- a/app/assets/javascripts/repository_columns/index.js +++ b/app/assets/javascripts/repository_columns/index.js @@ -144,8 +144,8 @@ var RepositoryColumns = (function() { }); } - function insertNewListItem(column) { - var html = `
  • @@ -154,12 +154,12 @@ var RepositoryColumns = (function() {
     
    @@ -176,10 +176,13 @@ var RepositoryColumns = (function() { $('#new-repo-column-submit').on('click', function() { var url = $('#repository_column_data_type').find(':selected').data('create-url'); var params = { repository_column: { name: $('#repository_column_name').val() } }; - $.post(url, params, (data) => { - insertNewListItem(data); - HelperModule.flashAlertMsg(data.message, 'success'); + $.post(url, params, (responseData) => { + var data = responseData.data; + insertNewListItem(data.id, data.attributes); + HelperModule.flashAlertMsg(data.attributes.message, 'success'); modalHtml.modal('hide'); + }).error((error) => { + $('#new_repository_column').renderFormErrors('repository_column', error.responseJSON.repository_column, true); }); }); } diff --git a/app/assets/javascripts/sitewide/form_errors.js b/app/assets/javascripts/sitewide/form_errors.js index 373cfd0b9..e62972481 100644 --- a/app/assets/javascripts/sitewide/form_errors.js +++ b/app/assets/javascripts/sitewide/form_errors.js @@ -42,7 +42,7 @@ var renderFormError = function(ev, input, errMsgs, clearErr, errAttributes) { })).join('
    '); var $errSpan = "' + errorText + ''; - $formGroup.append($errSpan); + $(input).after($errSpan); } var $parent; diff --git a/app/controllers/repository_columns/text_columns_controller.rb b/app/controllers/repository_columns/text_columns_controller.rb new file mode 100644 index 000000000..2783d0574 --- /dev/null +++ b/app/controllers/repository_columns/text_columns_controller.rb @@ -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 diff --git a/app/serializers/repository_column_serializer.rb b/app/serializers/repository_column_serializer.rb new file mode 100644 index 000000000..6e280191c --- /dev/null +++ b/app/serializers/repository_column_serializer.rb @@ -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 diff --git a/app/views/repository_columns/manage_column_partials/_text.html.erb b/app/views/repository_columns/manage_column_partials/_text.html.erb index 13ac90476..994c17ca5 100644 --- a/app/views/repository_columns/manage_column_partials/_text.html.erb +++ b/app/views/repository_columns/manage_column_partials/_text.html.erb @@ -1 +1 @@ -<%= 'text-test' %> +<%# TO DO: add mandatory checkbox %> diff --git a/spec/controllers/repository_columns/asset_columns_controller_spec.rb b/spec/controllers/repository_columns/asset_columns_controller_spec.rb index 5602ec560..5f82bfe16 100644 --- a/spec/controllers/repository_columns/asset_columns_controller_spec.rb +++ b/spec/controllers/repository_columns/asset_columns_controller_spec.rb @@ -25,7 +25,7 @@ RSpec.describe RepositoryColumns::AssetColumnsController, type: :controller do before do service = double('success_service') 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) end @@ -101,7 +101,7 @@ RSpec.describe RepositoryColumns::AssetColumnsController, type: :controller do before do service = double('success_service') 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) end diff --git a/spec/controllers/repository_columns/list_columns_controller_spec.rb b/spec/controllers/repository_columns/list_columns_controller_spec.rb index 8dbcf78e0..ca14b3eb9 100644 --- a/spec/controllers/repository_columns/list_columns_controller_spec.rb +++ b/spec/controllers/repository_columns/list_columns_controller_spec.rb @@ -25,7 +25,7 @@ RSpec.describe RepositoryColumns::ListColumnsController, type: :controller do before do service = double('success_service') 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) end @@ -101,7 +101,7 @@ RSpec.describe RepositoryColumns::ListColumnsController, type: :controller do before do service = double('success_service') 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) end diff --git a/spec/controllers/repository_columns/status_columns_controller_spec.rb b/spec/controllers/repository_columns/status_columns_controller_spec.rb index 71f026102..a9250642b 100644 --- a/spec/controllers/repository_columns/status_columns_controller_spec.rb +++ b/spec/controllers/repository_columns/status_columns_controller_spec.rb @@ -25,7 +25,7 @@ RSpec.describe RepositoryColumns::StatusColumnsController, type: :controller do before do service = double('success_service') 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) end @@ -101,7 +101,7 @@ RSpec.describe RepositoryColumns::StatusColumnsController, type: :controller do before do service = double('success_service') 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) end diff --git a/spec/controllers/repository_columns/text_columns_controller_spec.rb b/spec/controllers/repository_columns/text_columns_controller_spec.rb new file mode 100644 index 000000000..b20b3dd32 --- /dev/null +++ b/spec/controllers/repository_columns/text_columns_controller_spec.rb @@ -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