Skip to content

Commit 6266ba8

Browse files
committed
Extract ProjectScoping concern & Current.project_scope
To remove duplication and simplify the code. This means there is now only one place where we use the `project_type` param to override the default scope on Project to give access to scratch projects. What's more, it should be easier to add this to other controller actions in the future. Note that the underlying value of `Current.project_scope` will get reset to `nil` after every request [1]. Also note that the default value functionality for these attributes wasn't added until Rails v7.2 [2], so we have to override the reader method to provide the default value if the underlying value is `nil`. [1]: https://api.rubyonrails.org/v7.1.3.4/classes/ActiveSupport/CurrentAttributes.html [2]: https://github.com/rails/rails/blob/v7.2.0/activesupport/CHANGELOG.md
1 parent 971e52f commit 6266ba8

File tree

6 files changed

+31
-9
lines changed

6 files changed

+31
-9
lines changed

app/controllers/api/projects/remixes_controller.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,14 @@ def create
3636
private
3737

3838
def project
39-
@project ||= project_scope.find_by!(identifier: params[:project_id])
39+
@project ||= Current.project_scope.find_by!(identifier: params[:project_id])
4040
end
4141

4242
def load_and_authorize_remix
43-
@project = project_scope.find_by!(remixed_from_id: project.id, user_id: current_user&.id)
43+
@project = Current.project_scope.find_by!(remixed_from_id: project.id, user_id: current_user&.id)
4444
authorize! :show, @project
4545
end
4646

47-
def project_scope
48-
Project.only_scratch(params[:project_type] == Project::Types::SCRATCH)
49-
end
50-
5147
def remix_params
5248
params.require(:project)
5349
.permit(:name,

app/controllers/api/projects_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def verify_lesson_belongs_to_school
6969
def load_project
7070
project_loader = ProjectLoader.new(params[:id], [params[:locale]])
7171
@project = if action_name == 'show'
72-
project_loader.load(include_images: true, only_scratch: params[:project_type] == Project::Types::SCRATCH)
72+
project_loader.load(include_images: true)
7373
else
7474
project_loader.load
7575
end

app/controllers/api_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class ApiController < ActionController::API
44
class ::ParameterError < StandardError; end
55

66
include Identifiable
7+
include ProjectScoping
78

89
rescue_from ActionController::ParameterMissing, with: -> { bad_request }
910
rescue_from ActiveRecord::RecordNotFound, with: -> { not_found }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
module ProjectScoping
4+
extend ActiveSupport::Concern
5+
6+
included do
7+
before_action :set_project_scope
8+
end
9+
10+
private
11+
12+
def set_project_scope
13+
only_scratch = params[:project_type] == Project::Types::SCRATCH
14+
Current.project_scope = Project.only_scratch(only_scratch)
15+
end
16+
end

app/models/current.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
class Current < ActiveSupport::CurrentAttributes
4+
attribute :project_scope
5+
6+
def project_scope
7+
super || Project
8+
end
9+
end

lib/project_loader.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def initialize(identifier, locales)
88
@locales = [*locales, 'en', nil]
99
end
1010

11-
def load(include_images: false, only_scratch: false)
12-
query = Project.only_scratch(only_scratch)
11+
def load(include_images: false)
12+
query = Current.project_scope
1313
query = query.where(identifier:, locale: @locales)
1414
query = query.includes(images_attachments: :blob) if include_images
1515
query.min_by { |project| @locales.find_index(project.locale) }

0 commit comments

Comments
 (0)