scinote-web/app/models/concerns/cloneable.rb
artoscinote 4664ef1d9b
Implement task cloning in experiments table [SCI-7382] (#4653)
* Implement task cloning in experiments table [SCI-7382]

* Fix provisioning status polling [SCI-7382]

* Remove unused method [SCI-7382]

* Fix linter issues [SCI-7382]

* Fix fetching last clone number [SCI-7382]

* Fixing experiment duplication [SCI-7382]

* Add truncation to cloned name [SCI-7382]

* Add readable scope to batch clone action [SCI-7382]

* Move 'Clone' to translations, simplify JS [SCI-7382]
2022-12-01 15:08:59 +01:00

20 lines
701 B
Ruby

# frozen_string_literal: true
module Cloneable
extend ActiveSupport::Concern
def next_clone_name
raise NotImplementedError, "Cloneable model must implement the '.parent' method!" unless respond_to?(:parent)
clone_label = I18n.t('general.clone_label')
last_clone_number =
parent.public_send(self.class.table_name)
.select("substring(#{self.class.table_name}.name, '(?:^#{clone_label} )(\\d+)')::int AS clone_number")
.where('name ~ ?', "^#{clone_label} \\d+ - #{name}$")
.order(clone_number: :asc)
.last&.clone_number
"#{clone_label} #{(last_clone_number || 0) + 1} - #{name}".truncate(Constants::NAME_MAX_LENGTH)
end
end