scinote-web/app/services/repository_columns/create_column_service.rb

64 lines
1.3 KiB
Ruby
Raw Normal View History

2019-10-09 20:09:27 +08:00
# frozen_string_literal: true
module RepositoryColumns
class CreateColumnService
extend Service
include Canaid::Helpers::PermissionsHelper
2019-10-09 21:18:51 +08:00
attr_reader :errors, :column
2019-10-09 20:09:27 +08:00
2019-10-09 21:18:51 +08:00
def initialize(user:, repository:, column_name:, team:)
2019-10-09 20:09:27 +08:00
@user = user
@repository = repository
2019-10-09 21:18:51 +08:00
@column_name = column_name
@team = team
2019-10-09 20:09:27 +08:00
@errors = {}
2019-10-09 21:18:51 +08:00
@column = nil
2019-10-09 20:09:27 +08:00
end
def call
raise NotImplementedError
end
def succeed?
@errors.none?
end
private
def valid?
unless @user && @repository
@errors[:invalid_arguments] =
{ 'user': @user,
'repository': @repository }
.map do |key, value|
"Can't find #{key.capitalize}" if value.nil?
end.compact
end
succeed?
end
def create_base_column(type)
@column = RepositoryColumn.new(
2019-10-09 21:18:51 +08:00
repository: @repository,
2019-10-09 20:09:27 +08:00
created_by: @user,
2019-10-09 21:18:51 +08:00
name: @column_name,
2019-10-09 20:09:27 +08:00
data_type: type
)
end
2019-10-09 21:18:51 +08:00
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
2019-10-09 20:09:27 +08:00
end
end