Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort activities of a series by their defined order in the series #4302

Merged
merged 2 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def index
authorize @series, :show?
policy(@series).overview? ? @series.activities : []
else
policy_scope(Activity)
policy_scope(Activity).order_by_popularity(:DESC)
end

if params[:repository_id]
Expand All @@ -77,7 +77,7 @@ def index
end

unless @activities.empty?
@activities = apply_scopes(@activities.order_by_popularity(:DESC))
@activities = apply_scopes(@activities)
@activities = @activities.paginate(page: parse_pagination_param(params[:page]))
end
@labels = policy_scope(Label.all)
Expand Down
44 changes: 44 additions & 0 deletions test/controllers/activities_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -761,4 +761,48 @@ class ExerciseDescriptionTest < ActionDispatch::IntegrationTest
get description_activity_url(exercise, token: exercise.access_token, format: :json)
end
end

test 'activities of a series should be ordered by order of series membership' do
course = courses(:course1)
exercise = exercises(:python_exercise)
other_exercise = create :exercise
series = create :series, course: course, exercises: [exercise, other_exercise]

get series_activities_url(series, format: :json)

assert_response :success
activities_json = JSON.parse response.body
assert_equal [exercise.id, other_exercise.id], activities_json.pluck('id')

SeriesMembership.find_by(series: series, activity: exercise).update(order: 2)
SeriesMembership.find_by(series: series, activity: other_exercise).update(order: 1)

get series_activities_url(series, format: :json)

assert_response :success
activities_json = JSON.parse response.body
assert_equal [other_exercise.id, exercise.id], activities_json.pluck('id')
end

test 'activities should be ordered by popularity' do
exercise = exercises(:python_exercise)
other_exercise = create :exercise
create :series, exercises: [exercise, other_exercise]
create :series, exercises: [exercise]

get activities_url(format: :json)

assert_response :success
activities_json = JSON.parse response.body
assert_equal [exercise.id, other_exercise.id], activities_json.pluck('id')

create :series, exercises: [other_exercise]
create :series, exercises: [other_exercise]

get activities_url(format: :json)

assert_response :success
activities_json = JSON.parse response.body
assert_equal [other_exercise.id, exercise.id], activities_json.pluck('id')
end
end