Add CreateStatusColumnService service

This commit is contained in:
Urban Rotnik 2019-10-09 15:18:51 +02:00
parent c00a837105
commit bbb8c46fe2
4 changed files with 141 additions and 7 deletions

View file

@ -6,6 +6,9 @@ class RepositoryColumn < ApplicationRecord
has_many :repository_cells, dependent: :destroy
has_many :repository_rows, through: :repository_cells
has_many :repository_list_items, dependent: :destroy
has_many :repository_status_items, dependent: :destroy, index_errors: true
accepts_nested_attributes_for :repository_status_items
enum data_type: Extends::REPOSITORY_DATA_TYPES

View file

@ -5,13 +5,15 @@ module RepositoryColumns
extend Service
include Canaid::Helpers::PermissionsHelper
attr_reader :errors
attr_reader :errors, :column
def initialize(user, repository, name)
def initialize(user:, repository:, column_name:, team:)
@user = user
@repository = repository
@name = name
@column_name = column_name
@team = team
@errors = {}
@column = nil
end
def call
@ -34,18 +36,28 @@ module RepositoryColumns
end.compact
end
@errors[:user_without_permissions] = :forbidden unless can_create_repository_columns?(@user, @repository)
succeed?
end
def create_base_column(type)
@column = RepositoryColumn.new(
repository_id: @repository_id,
repository: @repository,
created_by: @user,
name: @name,
name: @column_name,
data_type: type
)
end
def log_activity(type)
Activities::CreateActivityService
.call(activity_type: type,
owner: @user,
subject: @repository,
team: @team,
message_items: {
repository_column: @column.id,
repository: @repository.id
})
end
end
end

View file

@ -0,0 +1,33 @@
# frozen_string_literal: true
module RepositoryColumns
class CreateStatusColumnService < RepositoryColumns::CreateColumnService
def initialize(user:, repository:, params:, team:)
super(user: user, repository: repository, team: team, column_name: params[:name])
@params = params
end
def call
return self unless valid?
@column = RepositoryColumn.new(repository_status_items_attributes)
if @column.save
log_activity(:create_column_inventory)
else
errors[:repository_column] = @column.errors.messages
end
self
end
private
def repository_status_items_attributes
@params[:repository_status_items_attributes]&.map do |m|
m.merge!(repository_id: @repository.id, created_by_id: @user.id)
end
@params.merge(repository_id: @repository.id, created_by_id: @user.id, data_type: :RepositoryStatusValue)
end
end
end

View file

@ -0,0 +1,86 @@
# frozen_string_literal: true
require 'rails_helper'
describe RepositoryColumns::CreateStatusColumnService do
let(:user) { create :user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
let(:team) { create :team }
let(:repository) { create :repository, team: team }
let(:params) do
{
name: 'myrepo',
repository_status_items_attributes: [
{ status: 'first', icon: 'some icon' },
{ status: 'second', icon: 'some icon' },
{ status: 'third', icon: 'some icon' }
]
}
end
context 'when creates new status column' do
let(:service_call) do
RepositoryColumns::CreateStatusColumnService.call(repository: repository,
user: user,
team: team,
params: params)
end
it 'adds RepositoryColumn record' do
expect { service_call }.to change { RepositoryColumn.count }.by(1)
end
it 'adds Activity record' do
expect { service_call }.to(change { Activity.count }.by(1))
end
it 'adds RepositoryStatusItem record' do
expect { service_call }.to(change { RepositoryStatusItem.count }.by(3))
end
end
context 'when has invalid record' do
let(:params) do
{
name: '',
repository_status_items_attributes: [
{ status: '', icon: 'some icon' },
{ status: 'second', icon: 'some icon' },
{ status: 'third' }
]
}
end
let(:service_call) do
RepositoryColumns::CreateStatusColumnService.call(repository: repository,
user: user,
team: team,
params: params)
end
it 'returns AR errors' do
expect(service_call.errors).to have_key(:repository_column)
end
it 'returns succeed false' do
expect(service_call.succeed?).to be_falsey
end
end
context 'whem repository is nil' do
let(:service_call) do
RepositoryColumns::CreateStatusColumnService.call(repository: nil,
user: user,
team: team,
params: params)
end
it 'returns invalid_arugments error' do
expect(service_call.errors).to have_key(:invalid_arguments)
end
it 'returns succeed false' do
expect(service_call.succeed?).to be_falsey
end
end
end