From d38e96f53be12bf6b241075177fe338063326763 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Wed, 20 Dec 2023 13:49:05 +0000 Subject: [PATCH] Allow users to have multiple themes --- app/helpers/theme_helper.rb | 6 +++--- app/models/user.rb | 3 ++- app/views/settings/theme/edit.html.haml | 4 ++-- db/migrate/20231220130817_allow_multiple_themes.rb | 14 ++++++++++++++ db/schema.rb | 5 ++++- 5 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20231220130817_allow_multiple_themes.rb diff --git a/app/helpers/theme_helper.rb b/app/helpers/theme_helper.rb index 7c54e05b..8fbfb311 100644 --- a/app/helpers/theme_helper.rb +++ b/app/helpers/theme_helper.rb @@ -52,7 +52,7 @@ module ThemeHelper end def theme_color - theme = active_theme_user&.theme + theme = active_theme_user&.active_theme if theme theme.theme_color else @@ -61,7 +61,7 @@ module ThemeHelper end def mobile_theme_color - theme = active_theme_user&.theme + theme = active_theme_user&.active_theme if theme theme.mobile_theme_color else @@ -72,7 +72,7 @@ module ThemeHelper def active_theme_user user = @user || @answer&.user # rubocop:disable Rails/HelperInstanceVariable - if user&.theme.present? && should_show_foreign_theme? + if user&.active_theme.present? && should_show_foreign_theme? user elsif user_signed_in? current_user diff --git a/app/models/user.rb b/app/models/user.rb index eb401fcd..02fde44c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -41,13 +41,14 @@ class User < ApplicationRecord has_many :list_memberships, class_name: "ListMember", dependent: :destroy_async has_many :mute_rules, dependent: :destroy_async has_many :anonymous_blocks, dependent: :destroy_async + has_many :themes, dependent: :destroy has_many :subscriptions, dependent: :destroy_async has_many :totp_recovery_codes, dependent: :destroy_async has_many :web_push_subscriptions, dependent: :destroy_async has_one :profile, dependent: :destroy - has_one :theme, dependent: :destroy + has_one :active_theme, -> { where(active: true) }, class_name: "Theme" has_many :bans, class_name: "UserBan", dependent: :destroy_async has_many :banned_users, class_name: "UserBan", diff --git a/app/views/settings/theme/edit.html.haml b/app/views/settings/theme/edit.html.haml index 1d5c84b0..db51d9bd 100644 --- a/app/views/settings/theme/edit.html.haml +++ b/app/views/settings/theme/edit.html.haml @@ -4,10 +4,10 @@ %p.lead= t(".lead") = t(".body_html") - - if current_user.theme + - if current_user.active_theme .pull-right = link_to t(".delete"), settings_theme_path, data: { turbo_confirm: t("voc.confirm"), turbo_method: :delete }, tabindex: -1, class: "btn btn-danger" -= bootstrap_form_for(current_user.theme || Theme.new, += bootstrap_form_for(current_user.active_theme || Theme.new, html: { id: "update" }, method: :patch, data: { turbo: false, controller: "theme", action: "theme#submit" }) do |f| diff --git a/db/migrate/20231220130817_allow_multiple_themes.rb b/db/migrate/20231220130817_allow_multiple_themes.rb new file mode 100644 index 00000000..24c46d88 --- /dev/null +++ b/db/migrate/20231220130817_allow_multiple_themes.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class AllowMultipleThemes < ActiveRecord::Migration[7.0] + def up + change_table :themes, bulk: true do |t| + t.boolean :active, default: true, null: false + t.string :name, null: true + end + + change_column :themes, :active, :boolean, default: false, null: false + + add_index :themes, %i[user_id active] + end +end diff --git a/db/schema.rb b/db/schema.rb index e4990d1f..b1c82f98 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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_12_20_100445) do +ActiveRecord::Schema[7.0].define(version: 2023_12_20_130817) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -292,6 +292,9 @@ ActiveRecord::Schema[7.0].define(version: 2023_12_20_100445) do t.integer "input_placeholder", default: 7107965, null: false t.integer "raised_text", default: 0, null: false t.integer "raised_accent_text", default: 0, null: false + t.boolean "active", default: false, null: false + t.string "name" + t.index ["user_id", "active"], name: "index_themes_on_user_id_and_active" t.index ["user_id"], name: "index_themes_on_user_id" end