mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-12-26 09:42:46 +08:00
Refactor text inventory type
This commit is contained in:
parent
37b7d5aa64
commit
305104f639
9 changed files with 337 additions and 17 deletions
|
@ -144,8 +144,8 @@ var RepositoryColumns = (function() {
|
|||
});
|
||||
}
|
||||
|
||||
function insertNewListItem(column) {
|
||||
var html = `<li class="list-group-item row" data-id="${column.id}"
|
||||
function insertNewListItem(id, column) {
|
||||
var html = `<li class="list-group-item row" data-id="${id}"
|
||||
data-destroy-url="${column.destroy_html_url}"
|
||||
data-edit-url="${column.edit_url}">
|
||||
<div class="col-xs-8">
|
||||
|
@ -154,12 +154,12 @@ var RepositoryColumns = (function() {
|
|||
<div class="col-xs-4">
|
||||
<span class="controlls pull-right">
|
||||
<button class="btn btn-default edit-repo-column" data-action="edit">
|
||||
<span class="fas fa-pencil-alt"></span>
|
||||
<%= I18n.t "libraries.repository_columns.index.edit_column" %>
|
||||
<span class="fas fa-pencil-alt"></span>
|
||||
${I18n.t('libraries.repository_columns.index.edit_column')}
|
||||
</button>
|
||||
<button class="btn btn-default delete-repo-column" data-action="destroy">
|
||||
<span class="fas fa-trash-alt"></span>
|
||||
<%= I18n.t "libraries.repository_columns.index.delete_column" %>
|
||||
<span class="fas fa-trash-alt"></span>
|
||||
${I18n.t('libraries.repository_columns.index.delete_column')}
|
||||
</button>
|
||||
</span>
|
||||
</div>
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ var renderFormError = function(ev, input, errMsgs, clearErr, errAttributes) {
|
|||
})).join('<br />');
|
||||
var $errSpan = "<span class='help-block'" +
|
||||
errAttributes + '>' + errorText + '</span>';
|
||||
$formGroup.append($errSpan);
|
||||
$(input).after($errSpan);
|
||||
}
|
||||
|
||||
var $parent;
|
||||
|
|
|
@ -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
|
23
app/serializers/repository_column_serializer.rb
Normal file
23
app/serializers/repository_column_serializer.rb
Normal 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
|
|
@ -1 +1 @@
|
|||
<%= 'text-test' %>
|
||||
<%# TO DO: add mandatory checkbox %>
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
Loading…
Reference in a new issue