mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-01-01 13:13:22 +08:00
Implement 'copy' method on Repository object
This commit is contained in:
parent
ef1ecdd7c7
commit
b94d2c5c03
1 changed files with 27 additions and 0 deletions
|
@ -13,4 +13,31 @@ class Repository < ActiveRecord::Base
|
||||||
length: { maximum: Constants::NAME_MAX_LENGTH }
|
length: { maximum: Constants::NAME_MAX_LENGTH }
|
||||||
validates :team, presence: true
|
validates :team, presence: true
|
||||||
validates :created_by, presence: true
|
validates :created_by, presence: true
|
||||||
|
|
||||||
|
def copy(created_by, name)
|
||||||
|
new_repo = nil
|
||||||
|
|
||||||
|
begin
|
||||||
|
Repository.transaction do
|
||||||
|
# Clone the repository object
|
||||||
|
new_repo = dup
|
||||||
|
new_repo.created_by = created_by
|
||||||
|
new_repo.name = name
|
||||||
|
new_repo.save!
|
||||||
|
|
||||||
|
# Clone columns (only if new_repo was saved)
|
||||||
|
repository_columns.find_each do |col|
|
||||||
|
new_col = col.dup
|
||||||
|
new_col.repository = new_repo
|
||||||
|
new_col.created_by = created_by
|
||||||
|
new_col.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
rescue ActiveRecord::RecordInvalid
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
# If everything is okay, return new_repo
|
||||||
|
new_repo
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue