Skip to content

Commit

Permalink
Added a format filter to Exercises search
Browse files Browse the repository at this point in the history
  • Loading branch information
Dantemss committed Aug 4, 2022
1 parent 6e1c2d7 commit 0448ac8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
13 changes: 13 additions & 0 deletions app/routines/search_exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,19 @@ def exec(params = {}, options = {})
end
end

with.keyword :format do |formats|
formats.each do |format|
sanitized_formats = to_string_array(format).map(&:downcase)
next @items = @items.none if sanitized_formats.empty?

@items = @items.where(
Question.joins(stems: :stylings).where(
'"questions"."exercise_id" = "exercises"."id"'
).where(stems: { stylings: { style: sanitized_formats } }).arel.exists
)
end
end

with.keyword :solutions_are_public do |saps|
saps.each do |sap|
sanitized_saps = to_string_array(sap).map do |str|
Expand Down
29 changes: 28 additions & 1 deletion spec/routines/search_exercises_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
'answers' => [{
'content_html' => "Sed quia non numquam"
}],
'formats' => [ 'multiple-choice', 'free-response' ]
'formats' => [ 'true-false' ]
}],
'solutions_are_public' => false
)
Expand Down Expand Up @@ -217,6 +217,15 @@
expect(outputs.items).to eq [@exercise_1]
end

it "returns an Exercise matching a format" do
result = described_class.call q: "format:multiple-choice"
expect(result.errors).to be_empty

outputs = result.outputs
expect(outputs.total_count).to eq 1
expect(outputs.items).to eq [@exercise_1]
end

it "returns an Exercise matching solutions_are_public" do
result = described_class.call q: "solutions_are_public:true"
expect(result.errors).to be_empty
Expand Down Expand Up @@ -335,6 +344,24 @@
expect(outputs.items).to eq [@exercise_1]
end

it "returns Exercises matching any of a list of formats" do
result = described_class.call q: "format:multiple-choice,true-false"
expect(result.errors).to be_empty

outputs = result.outputs
expect(outputs.total_count).to eq 2
expect(outputs.items).to eq [@exercise_1, @exercise_2]
end

it "returns an Exercise matching all of a list of formats" do
result = described_class.call q: "format:multiple-choice format:free-response"
expect(result.errors).to be_empty

outputs = result.outputs
expect(outputs.total_count).to eq 1
expect(outputs.items).to eq [@exercise_1]
end

it "sorts by multiple fields in different directions" do
result = described_class.call(q: 'content:aDiPiScI', order_by: "number DESC, version ASC")
expect(result.errors).to be_empty
Expand Down

0 comments on commit 0448ac8

Please sign in to comment.