Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/controllers/api/scratch/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ def move_pending_scratch_upload_to_remix(pending_upload, remix_project)
end

def load_project
@project = Project.find_by!(identifier: params.expect(:id), project_type: Project::Types::CODE_EDITOR_SCRATCH)
@project = ProjectLoader.new(params[:id], project_locale).load
raise ActiveRecord::RecordNotFound unless @project&.scratch_project?
end
Comment thread
zetter-rpf marked this conversation as resolved.

def project_locale
request.headers['X-Project-Locale']
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion config/initializers/bullet.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# frozen_string_literal: true

Bullet.add_safelist type: :unused_eager_loading, class_name: 'Project', association: :images_attachments if Rails.env.development? || Rails.env.test?
if Rails.env.development? || Rails.env.test?
Bullet.add_safelist type: :unused_eager_loading, class_name: 'Project', association: :images_attachments
Bullet.add_safelist type: :n_plus_one_query, class_name: 'Project', association: :scratch_component
end
13 changes: 13 additions & 0 deletions spec/features/scratch/showing_a_scratch_project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@
expect(response.parsed_body.fetch('targets').pluck('name')).to eq(%w[Stage Sprite1 Sprite2])
end

it 'prefers the project matching the project locale header' do
identifier = 'locale-specific-project'
english_project = create(:project, project_type: Project::Types::CODE_EDITOR_SCRATCH, identifier:, locale: 'en', user_id: nil)
create(:scratch_component, project: english_project, content: { targets: [{ name: 'English Stage', isStage: true }] })
french_project = create(:project, project_type: Project::Types::CODE_EDITOR_SCRATCH, identifier:, locale: 'fr-FR', user_id: nil)
create(:scratch_component, project: french_project, content: { targets: [{ name: 'French Stage', isStage: true }] })

get "/api/scratch/projects/#{identifier}", headers: { 'X-Project-Locale' => 'fr-FR' }

expect(response).to have_http_status(:ok)
expect(response.parsed_body.fetch('targets').pluck('name')).to eq(['French Stage'])
end

it 'returns a 404 if project does not exist' do
authenticated_in_hydra_as(teacher)
get '/api/scratch/projects/non_existent_project', headers: headers
Expand Down
Loading