diff --git a/app/controllers/api/scratch/projects_controller.rb b/app/controllers/api/scratch/projects_controller.rb index 54d549752..5bb84f26e 100644 --- a/app/controllers/api/scratch/projects_controller.rb +++ b/app/controllers/api/scratch/projects_controller.rb @@ -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 + + def project_locale + request.headers['X-Project-Locale'] end end end diff --git a/config/initializers/bullet.rb b/config/initializers/bullet.rb index b40b53940..3eb731b7f 100644 --- a/config/initializers/bullet.rb +++ b/config/initializers/bullet.rb @@ -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 diff --git a/spec/features/scratch/showing_a_scratch_project_spec.rb b/spec/features/scratch/showing_a_scratch_project_spec.rb index 29524cf46..9d7db76c7 100644 --- a/spec/features/scratch/showing_a_scratch_project_spec.rb +++ b/spec/features/scratch/showing_a_scratch_project_spec.rb @@ -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