This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/lib/topics_filter.rb
Alan Guo Xiang Tan 14fd9cb23f
DEV: Remove experimental support for query string on /filter route
Why is this changed being made?

Prior to this change, we were relying on the client to ship a `q` query
param which represents a topics filtering query string that would be
parsed on the server to generate the corresponding topics scope.
However, we decided to revert this change because we lost the ability to
keep our URL "nice". By nice, we mean no encodings in the query params.

What does this commit do?

With this commit, we no longer ship a query string as the `q` query
params to the server. Instead, we parse the input given by the user on
the client side and converts it to the relevant query params. As an
example:

An input value of `status:closed tag:todo` will automatically set
`?status=closed&tag=todo` in the query params.
2023-03-13 09:15:17 +08:00

32 lines
801 B
Ruby

# frozen_string_literal: true
class TopicsFilter
def initialize(guardian:, scope: Topic, category_id: nil)
@guardian = guardian
@scope = scope
@category = category_id.present? ? Category.find_by(id: category_id) : nil
end
def filter(status: nil)
filter_status(@scope, status) if status
@scope
end
private
def filter_status(scope, status)
case status
when "open"
@scope = @scope.where("NOT topics.closed AND NOT topics.archived")
when "closed"
@scope = @scope.where("topics.closed")
when "archived"
@scope = @scope.where("topics.archived")
when "deleted"
if @guardian.can_see_deleted_topics?(@category)
@scope = @scope.unscope(where: :deleted_at).where("topics.deleted_at IS NOT NULL")
end
end
end
end