Skip to content
Open
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
14 changes: 14 additions & 0 deletions app/assets/stylesheets/components/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@
background-color: var(--color-bg-hover);
}

.settings-page .email-table tbody tr.saved-search-hidden td:not(:last-child) {
opacity: 0.55;
}

.settings-page .email-table tbody tr.saved-search-hidden td:first-child {
font-style: italic;
}

.settings-page .email-table td:last-child {
display: flex;
gap: var(--spacing-2);
Expand Down Expand Up @@ -732,6 +740,12 @@
color: var(--color-text-secondary);
}

.settings-page .badge-default {
background: var(--color-primary-100);
color: var(--color-primary-700);
margin-left: var(--spacing-2);
}

.sent-header {
margin-bottom: var(--spacing-6);
}
Expand Down
14 changes: 14 additions & 0 deletions app/controllers/settings/saved_search_preferences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,19 @@ def create

redirect_back fallback_location: settings_saved_searches_path, notice: hidden ? "Search hidden" : "Search shown"
end

def set_default
saved_search = SavedSearch.visible_to(current_user).find(params[:saved_search_id])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either this has to restrict selection to non-team saved searches, or the redirect has to handle adding the team id

default = ActiveModel::Type::Boolean.new.cast(params[:default])

pref = SavedSearchPreference.find_or_initialize_by(
saved_search: saved_search,
user: current_user
)
pref.default = default
pref.save!

redirect_back fallback_location: settings_saved_searches_path, notice: default ? "Default search set" : "Default search cleared"
end
end
end
5 changes: 5 additions & 0 deletions app/controllers/settings/saved_searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Settings
class SavedSearchesController < Settings::BaseController
before_action :set_saved_search, only: [ :edit, :update, :destroy ]
before_action :set_default_saved_search_id, only: [ :index ]

def index
@saved_searches = current_user.saved_searches.order(:position, :name)
Expand Down Expand Up @@ -66,6 +67,10 @@ def set_saved_search
@saved_search = current_user.saved_searches.find(params[:id])
end

def set_default_saved_search_id
@default_saved_search_id = current_user.default_saved_search_preference&.saved_search_id
end

def saved_search_params
params.require(:saved_search).permit(:name, :query)
end
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class TopicsController < ApplicationController
ROW_STATES_LIMIT = 50

def index
if user_signed_in? && request.format.html? && params[:cursor].blank? && params[:commit].blank?
default_search = current_user.default_saved_search

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the default view showing everything very difficult to access, we should have an easy way (button/link) that brings users back to the.

We can also get a ui-issue that since the default search is a saved search, it will show up as a badge at the top, where you can click on the "X" button that redirect to the index, which will redirect back again to the same saved search.

Maybe one solution could be that we make the root "/" URL dependent on this (maybe even make / explicitly redirect to /topics or the saved search), and that way clicking on the hackorum logo always would bring up the user's preferred home page, while clicking on topics will always bring up the entire index?

return redirect_to search_topics_path(saved_search_id: default_search.id) if default_search
end

@search_query = nil
base_query = Topic.includes(*TOPIC_LIST_PRELOADS)
base_query = apply_default_ignore_filter(base_query) if user_signed_in?
Expand Down
8 changes: 8 additions & 0 deletions app/models/saved_search_preference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ class SavedSearchPreference < ApplicationRecord
belongs_to :user

validates :saved_search_id, uniqueness: { scope: :user_id }

before_save :clear_other_defaults, if: -> { default? && will_save_change_to_default? }

private

def clear_other_defaults
self.class.where(user_id: user_id, default: true).where.not(id: id).update_all(default: false)
end
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class User < ApplicationRecord
has_many :starred_topics, through: :topic_stars, source: :topic
has_many :saved_searches
has_many :saved_search_preferences
has_one :default_saved_search_preference, -> { where(default: true) }, class_name: "SavedSearchPreference"
has_one :default_saved_search, through: :default_saved_search_preference, source: :saved_search
has_many :outgoing_drafts, dependent: :destroy
has_many :user_features, dependent: :destroy

Expand Down
45 changes: 34 additions & 11 deletions app/views/settings/saved_searches/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
th Actions
tbody
- @saved_searches.each do |ss|
- is_default = @default_saved_search_id == ss.id
tr
td = ss.name
td
= ss.name
- if is_default
span.badge.badge-default Default
td
code = ss.query
td
Expand All @@ -28,6 +32,7 @@
i.fa-solid.fa-ellipsis-vertical
.row-actions-menu
= link_to "Edit", edit_settings_saved_search_path(ss), class: "row-actions-item"
= button_to is_default ? "Remove default" : "Set as default", set_default_settings_saved_search_preferences_path(saved_search_id: ss.id, default: !is_default), method: :post, class: "row-actions-item"
= button_to "Delete", settings_saved_search_path(ss), method: :delete, class: "row-actions-item row-actions-item-danger", data: { turbo_confirm: "Delete this saved search?" }

- if @global_searches.any?
Expand All @@ -39,16 +44,25 @@
tr
th Name
th Query
th Visible
th Actions
tbody
- @global_searches.each do |ss|
tr
td = ss.name
- hidden = @hidden_ids.include?(ss.id)
- is_default = @default_saved_search_id == ss.id
tr class=("saved-search-hidden" if hidden)
td
= ss.name
- if is_default
span.badge.badge-default Default
td
code = ss.query
td
- hidden = @hidden_ids.include?(ss.id)
= button_to hidden ? "Show" : "Hide", settings_saved_search_preferences_path(saved_search_id: ss.id, hidden: !hidden), method: :post, class: "button-secondary button-small"
details.row-actions data-controller="nav-dropdown"
summary.row-actions-toggle aria-label="Actions"
i.fa-solid.fa-ellipsis-vertical
.row-actions-menu
= button_to hidden ? "Show" : "Hide", settings_saved_search_preferences_path(saved_search_id: ss.id, hidden: !hidden), method: :post, class: "row-actions-item"
= button_to is_default ? "Remove default" : "Set as default", set_default_settings_saved_search_preferences_path(saved_search_id: ss.id, default: !is_default), method: :post, class: "row-actions-item"

- if @system_searches.any?
h2 Default Searches
Expand All @@ -59,13 +73,22 @@
tr
th Name
th Query
th Visible
th Actions
tbody
- @system_searches.each do |ss|
tr
td = ss.name
- hidden = @hidden_ids.include?(ss.id)
- is_default = @default_saved_search_id == ss.id
tr class=("saved-search-hidden" if hidden)
td
= ss.name
- if is_default
span.badge.badge-default Default
td
code = ss.query
td
- hidden = @hidden_ids.include?(ss.id)
= button_to hidden ? "Show" : "Hide", settings_saved_search_preferences_path(saved_search_id: ss.id, hidden: !hidden), method: :post, class: "button-secondary button-small"
details.row-actions data-controller="nav-dropdown"
summary.row-actions-toggle aria-label="Actions"
i.fa-solid.fa-ellipsis-vertical
.row-actions-menu
= button_to hidden ? "Show" : "Hide", settings_saved_search_preferences_path(saved_search_id: ss.id, hidden: !hidden), method: :post, class: "row-actions-item"
= button_to is_default ? "Remove default" : "Set as default", set_default_settings_saved_search_preferences_path(saved_search_id: ss.id, default: !is_default), method: :post, class: "row-actions-item"
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@
resource :import, only: [ :show, :create ]
resource :deletion, only: [ :show, :create ]
resources :saved_searches
resources :saved_search_preferences, only: [ :create ]
resources :saved_search_preferences, only: [ :create ] do
post :set_default, on: :collection
end

resources :teams, only: [ :index, :show, :create, :update, :destroy ] do
resources :team_members, only: [ :create, :update, :destroy ]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddDefaultToSavedSearchPreferences < ActiveRecord::Migration[8.0]
def change
add_column :saved_search_preferences, :default, :boolean, default: false, null: false

add_index :saved_search_preferences, :user_id,
unique: true,
where: '"default" = true',
name: "idx_one_default_saved_search_per_user"
end
end
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading