Skip to content

Commit ce20014

Browse files
committed
Use ProjectLoader in Api::PublicProjectsController
This is closely based on how `Api::ProjectsController` works although it's not obvious that its behaviour is tested. However, there is a spec for `ProjectLoader` itself, so I've just added examples to make sure it's wired up to the controller correctly. One thing I've added is to raise `ActiveRecord::RecordNotFound` if `ProjectLoader#load` returns `nil` which seems possible. This will result in a 404 Not Found response which is what I was already asserting in `spec/features/public_project/updating_a_public_project_spec.rb`. As far as I can tell, `Api::ProjectsController` actions which make use of `ProjectLoader#load` (e.g. the `update` action) do not check for the project being `nil` and so will probably respond with a 500 Internal Server Error when the code (e.g. in `Project::Update`) calls a method on the `nil` project. This doesn't seem great!
1 parent 5086e1e commit ce20014

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

app/controllers/api/public_projects_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ def update
3232
private
3333

3434
def load_project
35-
@project = Project.find_by!(identifier: params[:id])
35+
loader = ProjectLoader.new(params[:id], [params[:locale]])
36+
@project = loader.load
37+
raise ActiveRecord::RecordNotFound if @project.blank?
3638
end
3739

3840
def create_params

spec/features/public_project/updating_a_public_project_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
RSpec.describe 'Updating a public project', type: :request do
66
let(:creator) { build(:user) }
7-
let(:project) { create(:project) }
7+
let(:project) { create(:project, locale: 'en') }
88
let(:headers) { { Authorization: UserProfileMock::TOKEN } }
99
let(:params) { { project: { name: 'New name' } } }
1010

spec/requests/public_projects/update_spec.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
require 'rails_helper'
44

55
RSpec.describe 'Update public project requests' do
6-
let(:project) { create(:project) }
6+
let(:locale) { 'fr' }
7+
let(:project_loader) { instance_double(ProjectLoader) }
8+
let(:project) { create(:project, locale: 'en') }
79
let(:creator) { build(:user) }
810
let(:params) { { project: { name: 'New name' } } }
911

@@ -14,11 +16,26 @@
1416
before do
1517
authenticated_in_hydra_as(creator)
1618

19+
allow(ProjectLoader).to receive(:new).and_return(project_loader)
20+
allow(project_loader).to receive(:load).and_return(project)
21+
1722
response = OperationResponse.new
1823
response[:project] = project
1924
allow(PublicProject::Update).to receive(:call).and_return(response)
2025
end
2126

27+
it 'builds ProjectLoader with identifier & locale' do
28+
put("/api/public_projects/#{project.identifier}?locale=#{locale}", headers:, params:)
29+
30+
expect(ProjectLoader).to have_received(:new).with(project.identifier, [locale])
31+
end
32+
33+
it 'uses ProjectLoader#load to find the project based on identifier & locale' do
34+
put("/api/public_projects/#{project.identifier}?locale=#{locale}", headers:, params:)
35+
36+
expect(project_loader).to have_received(:load)
37+
end
38+
2239
it 'returns success' do
2340
put("/api/public_projects/#{project.identifier}", headers:, params:)
2441

0 commit comments

Comments
 (0)