Skip to content

Commit

Permalink
Project copying fixes: Talk roles and field guide images (#4258)
Browse files Browse the repository at this point in the history
* Copy field guide images via project copier

* Create Talk admin roles for copied projects

* Hound's right

* Move worker call outside transaction

* Remove duplicate field_guide assoc
  • Loading branch information
zwolf authored Oct 25, 2023
1 parent b7707fb commit d742f89
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/project_copier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ class ProjectCopier
EXCLUDE_ATTRIBUTES = %i[classifications_count launched_row_order beta_row_order].freeze
INCLUDE_ASSOCIATIONS = [
:tutorials,
:field_guides,
:pages,
:tags,
:tagged_resources,
:avatar,
:background,
{ active_workflows: %i[tutorials attached_images] }
{ active_workflows: %i[tutorials attached_images] },
{ field_guides: %i[attached_images] }
].freeze

def initialize(project_id, user_id)
Expand All @@ -22,7 +22,7 @@ def copy
# Should this all be wrapped in a transaction?
# to ensure we rollback an sub resource creations,
# e.g. inband primary lang strings for the associated resources....
Project.transaction(requires_new: true) do
copied_project = Project.transaction(requires_new: true) do
copied_project = copy_project

# save the project and create the project versions for use in translation strings
Expand All @@ -35,9 +35,14 @@ def copy
# to keep the translations system working with these copied resources
setup_associated_primary_language_translations(copied_project)

# return the newly copied project
copied_project
end

# Creates Talk roles via background worker
TalkAdminCreateWorker.perform_async(copied_project.id)

# return the newly copied project
copied_project
end

private
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/project_copier_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe ProjectCopier do
Expand Down Expand Up @@ -39,6 +41,14 @@
expect(copied_project.configuration['source_project_id']).to be(project.id)
end

it 'creates Talk roles for the new project and its owner' do
allow(TalkAdminCreateWorker).to receive(:perform_async)
copied_project
expect(TalkAdminCreateWorker)
.to have_received(:perform_async)
.with(be_kind_of(Integer))
end

context 'when a project has active_worklfows' do
it 'creates a valid workflow copy' do
expect(copied_project.active_workflows.first).to be_valid
Expand Down Expand Up @@ -84,6 +94,12 @@
field_guide = copied_project.field_guides.first
expect(field_guide.translations.first.language).to eq(project.primary_language)
end

it 'copies the field guide attached images' do
fg = create(:field_guide, project: project)
fg.attached_images << create(:medium, type: 'field_guide_attached_image', linked: fg)
expect(copied_project.field_guides.first.attached_images[0]).to be_valid
end
end

context 'when a project has a project page' do
Expand Down

0 comments on commit d742f89

Please sign in to comment.