Skip to content

Commit d635cef

Browse files
committed
Extract Project.only_scratch scope
I want to do something similar in the `Api::Projects::RemixesController`. By making this scope available, I'll be able to reduce the amount of duplication.
1 parent 75e622e commit d635cef

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

app/models/project.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ module Types
3535
default_scope -> { where.not(project_type: Types::SCRATCH) }
3636

3737
scope :internal_projects, -> { where(user_id: nil) }
38+
scope :only_scratch, lambda { |only_scratch|
39+
only_scratch ? unscoped.where(project_type: Project::Types::SCRATCH) : self
40+
}
3841

3942
has_paper_trail(
4043
if: ->(p) { p&.school_id },

lib/project_loader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize(identifier, locales)
99
end
1010

1111
def load(include_images: false, only_scratch: false)
12-
query = only_scratch ? Project.unscoped.where(project_type: Project::Types::SCRATCH) : Project
12+
query = Project.only_scratch(only_scratch)
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) }

spec/models/project_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,53 @@
342342
expect(described_class.all).not_to include(scratch_project)
343343
end
344344
end
345+
346+
describe 'only_scratch scope' do
347+
let!(:python_project) { create(:project, project_type: Project::Types::PYTHON) }
348+
let!(:html_project) { create(:project, project_type: Project::Types::HTML) }
349+
let!(:project_with_unknown_type) { create(:project, project_type: 'unknown') }
350+
let!(:scratch_project) { create(:project, project_type: Project::Types::SCRATCH) }
351+
352+
let(:projects_in_scope) { described_class.only_scratch(only_scratch) }
353+
354+
context 'when only_scratch is false' do
355+
let(:only_scratch) { false }
356+
357+
it 'includes python projects' do
358+
expect(projects_in_scope).to include(python_project)
359+
end
360+
361+
it 'includes html projects' do
362+
expect(projects_in_scope).to include(html_project)
363+
end
364+
365+
it 'includes projects with unknown type' do
366+
expect(projects_in_scope).to include(project_with_unknown_type)
367+
end
368+
369+
it 'does not include scratch projects' do
370+
expect(projects_in_scope).not_to include(scratch_project)
371+
end
372+
end
373+
374+
context 'when only_scratch is true' do
375+
let(:only_scratch) { true }
376+
377+
it 'does not include python projects' do
378+
expect(projects_in_scope).not_to include(python_project)
379+
end
380+
381+
it 'does not include html projects' do
382+
expect(projects_in_scope).not_to include(html_project)
383+
end
384+
385+
it 'does not include projects with unknown type' do
386+
expect(projects_in_scope).not_to include(project_with_unknown_type)
387+
end
388+
389+
it 'includes scratch projects' do
390+
expect(projects_in_scope).to include(scratch_project)
391+
end
392+
end
393+
end
345394
end

0 commit comments

Comments
 (0)