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

Default filters for anonymous users #1087

Merged
merged 9 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion app/assets/javascripts/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ $(() => {
const filters = await QPixel.filters();

function template(option) {
if (option.id == '') { return 'None'; }
if (option.id == '') { return 'Default'; }

const filter = filters[option.id];
const name = `<span>${option.text}</span>`;
Expand Down
19 changes: 14 additions & 5 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def category_params
params.require(:category).permit(:name, :short_wiki, :tag_set_id, :is_homepage, :min_trust_level, :button_text,
:color_code, :min_view_trust_level, :license_id, :sequence,
:asking_guidance_override, :answering_guidance_override,
:use_for_hot_posts, :use_for_advertisement, :min_title_length, :min_body_length,
:use_for_hot_posts, :use_for_advertisement, :min_title_length, :min_body_length, :default_filter_id,
display_post_types: [], post_type_ids: [], required_tag_ids: [],
topic_tag_ids: [], moderator_tag_ids: [])
end
Expand All @@ -165,13 +165,22 @@ def set_list_posts
filter_qualifiers = helpers.params_to_qualifiers
@active_filter = helpers.active_filter

if filter_qualifiers.blank? && user_signed_in?
default_filter_id = helpers.default_filter(current_user.id, @category.id)
default_filter = Filter.find_by(id: default_filter_id)
if filter_qualifiers.blank? && @active_filter[:name].blank?
if user_signed_in?
default_filter_id = helpers.default_filter(current_user.id, @category.id)
default_filter = Filter.find_by(id: default_filter_id)
default = :user if default_filter.present?
end

if default_filter.nil?
default_filter = @category.default_filter
default = :category if default_filter.present?
end

unless default_filter.nil?
filter_qualifiers = helpers.filter_to_qualifiers default_filter
@active_filter = {
default: true,
default: default,
name: default_filter.name,
min_score: default_filter.min_score,
max_score: default_filter.max_score,
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/search_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def filter_to_qualifiers(filter)

def active_filter
{
default: false,
default: nil,
name: params[:predefined_filter],
min_score: params[:min_score],
max_score: params[:max_score],
Expand Down
1 change: 1 addition & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Category < ApplicationRecord
has_many :posts
belongs_to :tag_set
belongs_to :license
belongs_to :default_filter, class_name: 'Filter', optional: true

serialize :display_post_types, Array

Expand Down
7 changes: 7 additions & 0 deletions app/views/categories/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@
</span>
<%= f.number_field :sequence, class: 'form-element' %>
</div>

<div class="form-group">
<%= f.label :default_filter_id, class: 'form-element' %>
<span class="form-caption">The default filter for this category, used for anonymous users.</span>
<% system_filters = User.find(-1).filters.to_h { |filter| [filter.name, filter.id] } %>
<%= f.select :default_filter_id, options_for_select(system_filters, selected: @category.default_filter_id), { include_blank: "No default" } %>
</div>
</details>

<details>
Expand Down
10 changes: 9 additions & 1 deletion app/views/categories/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,18 @@

<details>
<summary>Filters (<%= @filtered ? @active_filter[:name] || 'Custom' : 'None' %>)</summary>
<% if @active_filter[:default] == true %>
<% if @active_filter[:default] == :user %>
<div class="notice is-info">
You are currently filtering by <strong><%= @active_filter[:name] %></strong> because it is set as your default for this category
</div>
<% elsif @active_filter[:default] == :category and not user_signed_in? %>
<div class="notice is-info">
You are currently filtering by <strong><%= @active_filter[:name] %></strong> because it is the default for this category
</div>
<% elsif @active_filter[:default] == :category and user_signed_in? %>
<div class="notice is-info">
You are currently filtering by <strong><%= @active_filter[:name] %></strong> because it is the default for this category and you do not have a personal default set
</div>
<% end %>
<%= form_tag request.original_url, method: :get do %>
<%= render 'search/filters' %>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20230627035349_add_default_filter_to_category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDefaultFilterToCategory < ActiveRecord::Migration[7.0]
def change
add_reference :categories, :default_filter, foreign_key: { to_table: :filters }, null: true
end
end
11 changes: 7 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_06_13_205236) do
ActiveRecord::Schema[7.0].define(version: 2023_06_27_035349) do
create_table "abilities", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "community_id"
t.string "name"
Expand Down Expand Up @@ -111,7 +111,9 @@
t.boolean "use_for_advertisement", default: true
t.integer "min_title_length", default: 15, null: false
t.integer "min_body_length", default: 30, null: false
t.bigint "default_filter_id"
t.index ["community_id"], name: "index_categories_on_community_id"
t.index ["default_filter_id"], name: "index_categories_on_default_filter_id"
t.index ["license_id"], name: "index_categories_on_license_id"
t.index ["sequence"], name: "index_categories_on_sequence"
t.index ["tag_set_id"], name: "index_categories_on_tag_set_id"
Expand Down Expand Up @@ -424,8 +426,8 @@
t.boolean "is_top_level", default: false, null: false
t.boolean "is_freely_editable", default: false, null: false
t.string "icon_name"
t.boolean "has_reactions"
t.bigint "answer_type_id"
t.boolean "has_reactions"
t.boolean "has_only_specific_reactions"
t.index ["answer_type_id"], name: "index_post_types_on_answer_type_id"
t.index ["name"], name: "index_post_types_on_name"
Expand Down Expand Up @@ -754,15 +756,16 @@
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.boolean "read", default: false
t.index ["author_id"], name: "index_warnings_on_author_id"
MoshiKoi marked this conversation as resolved.
Show resolved Hide resolved
t.index ["community_user_id"], name: "index_warnings_on_community_user_id"
t.index ["author_id"], name: "index_mod_messages_on_author_id"
t.index ["community_user_id"], name: "index_mod_messages_on_community_user_id"
end

add_foreign_key "abilities", "communities"
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "audit_logs", "communities"
add_foreign_key "audit_logs", "users"
add_foreign_key "categories", "filters", column: "default_filter_id"
add_foreign_key "categories", "licenses"
add_foreign_key "categories", "tag_sets"
add_foreign_key "category_filter_defaults", "categories"
Expand Down
3 changes: 3 additions & 0 deletions db/seeds/filters.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
- name: None
user_id: -1

- name: Positive
user_id: -1
min_score: 0.5
Expand Down