2018-04-11 23:17:19 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module RepositoryActions
|
2018-04-13 17:47:33 +08:00
|
|
|
class DuplicateRows
|
2018-04-11 23:17:19 +08:00
|
|
|
attr_reader :number_of_duplicated_items
|
2018-04-13 17:47:33 +08:00
|
|
|
def initialize(user, repository, rows_ids = [])
|
2018-04-11 23:17:19 +08:00
|
|
|
@user = user
|
|
|
|
@repository = repository
|
2020-01-14 23:55:10 +08:00
|
|
|
@rows_to_duplicate = rows_ids
|
2018-04-11 23:17:19 +08:00
|
|
|
@number_of_duplicated_items = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
2018-04-13 17:47:33 +08:00
|
|
|
@rows_to_duplicate.each do |row_id|
|
|
|
|
duplicate_row(row_id)
|
2018-04-11 23:17:19 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-04-13 17:47:33 +08:00
|
|
|
def duplicate_row(id)
|
2020-01-14 23:55:10 +08:00
|
|
|
RepositoryRow.transaction do
|
|
|
|
row = @repository.repository_rows.find_by(id: id)
|
|
|
|
return unless row
|
|
|
|
|
|
|
|
new_row = RepositoryRow.create!(
|
|
|
|
row.attributes.merge(new_row_attributes(row.name, @user.id))
|
|
|
|
)
|
2018-04-11 23:17:19 +08:00
|
|
|
|
|
|
|
row.repository_cells.each do |cell|
|
2020-01-14 23:55:10 +08:00
|
|
|
RepositoryActions::DuplicateCell.new(cell, new_row, @user).call
|
2018-04-11 23:17:19 +08:00
|
|
|
end
|
2019-03-29 18:24:22 +08:00
|
|
|
|
2020-01-14 23:55:10 +08:00
|
|
|
@number_of_duplicated_items += 1
|
|
|
|
|
2019-03-29 18:24:22 +08:00
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: :copy_inventory_item,
|
|
|
|
owner: @user,
|
|
|
|
subject: @repository,
|
|
|
|
team: @repository.team,
|
|
|
|
message_items: { repository_row_new: new_row.id, repository_row_original: row.id })
|
2018-04-11 23:17:19 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-13 21:37:36 +08:00
|
|
|
def new_row_attributes(name, user_id)
|
2018-04-11 23:17:19 +08:00
|
|
|
timestamp = DateTime.now
|
|
|
|
{ id: nil,
|
|
|
|
name: "#{name} (1)",
|
2019-05-13 21:37:36 +08:00
|
|
|
created_by_id: user_id,
|
2018-04-11 23:17:19 +08:00
|
|
|
created_at: timestamp,
|
|
|
|
updated_at: timestamp }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|