scinote-web/app/services/repository_actions/archive_rows_base_service.rb

63 lines
1.4 KiB
Ruby
Raw Normal View History

2020-06-10 00:50:36 +08:00
# frozen_string_literal: true
module RepositoryActions
class ArchiveRowsBaseService
extend Service
attr_reader :errors
2020-06-10 00:50:36 +08:00
2020-06-19 15:04:42 +08:00
def initialize(user:, team:, repository:, repository_rows:, log_activities: true)
2020-06-10 00:50:36 +08:00
@user = user
@team = team
@repository = repository
@repository_rows = scoped_repository_rows(repository_rows)
2020-06-19 15:04:42 +08:00
@log_activities = log_activities
2020-06-10 00:50:36 +08:00
@errors = {}
end
def call
raise NotImplementedError
end
def succeed?
@errors.none?
end
def error_message
@errors.values.join(', ')
end
2020-06-10 00:50:36 +08:00
private
def scoped_repository_rows(_ids)
raise NotImplementedError
end
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
@errors[:repository_rows] = I18n.t('repositories.archive_records.invalid_rows_flash') if @repository_rows.blank?
2020-06-10 00:50:36 +08:00
succeed?
end
def log_activity(type, row)
Activities::CreateActivityService
.call(activity_type: type,
owner: @user,
subject: row,
2020-06-10 00:50:36 +08:00
team: @team,
message_items: {
repository_row: row.id
})
end
end
end