diff --git a/app/controllers/my_module_repository_snapshots_controller.rb b/app/controllers/my_module_repository_snapshots_controller.rb index b1332235a..2ec3538c6 100644 --- a/app/controllers/my_module_repository_snapshots_controller.rb +++ b/app/controllers/my_module_repository_snapshots_controller.rb @@ -31,15 +31,7 @@ class MyModuleRepositorySnapshotsController < ApplicationController end def create - repository_snapshot = @repository.dup.becomes(RepositorySnapshot) - repository_snapshot.assign_attributes(type: RepositorySnapshot.name, - original_repository: @repository, - my_module: @my_module, - created_by: current_user) - repository_snapshot.provisioning! - repository_snapshot.reload - - RepositorySnapshotProvisioningJob.perform_later(repository_snapshot) + repository_snapshot = @repository.provision_snapshot(@my_module, current_user) render json: { html: render_to_string(partial: 'my_modules/repositories/full_view_version', diff --git a/app/models/repository.rb b/app/models/repository.rb index 1c0b95989..2f2525115 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -191,4 +191,17 @@ class Repository < RepositoryBase importer = RepositoryImportParser::Importer.new(sheet, mappings, user, self) importer.run end + + def provision_snapshot(my_module, created_by = nil) + created_by ||= self.created_by + repository_snapshot = dup.becomes(RepositorySnapshot) + repository_snapshot.assign_attributes(type: RepositorySnapshot.name, + original_repository: self, + my_module: my_module, + created_by: created_by) + repository_snapshot.provisioning! + repository_snapshot.reload + RepositorySnapshotProvisioningJob.perform_later(repository_snapshot) + repository_snapshot + end end diff --git a/app/models/repository_snapshot.rb b/app/models/repository_snapshot.rb index 6bf3477ef..b0bacbc62 100644 --- a/app/models/repository_snapshot.rb +++ b/app/models/repository_snapshot.rb @@ -11,7 +11,7 @@ class RepositorySnapshot < RepositoryBase validates :name, presence: true, length: { maximum: Constants::NAME_MAX_LENGTH } validates :status, presence: true - validate :only_one_selected_for_my_module + validate :only_one_selected_for_my_module, if: ->(obj) { obj.changed.include? :selected } def default_columns_count Constants::REPOSITORY_SNAPSHOT_TABLE_DEFAULT_STATE['length']