Simplify theme helper

This commit is contained in:
Karina Kwiatek 2023-02-01 22:43:46 +01:00
parent 99403988b7
commit c3a0455acb
3 changed files with 30 additions and 89 deletions

View file

@ -28,15 +28,6 @@ module ThemeHelper
"muted_text" => "muted-text",
}.freeze
def render_theme
theme = get_active_theme
return unless theme
css = get_theme_css(theme)
content_tag(:style, css)
end
def get_theme_css(theme)
body = ":root {\n"
@ -61,7 +52,7 @@ module ThemeHelper
end
def theme_color
theme = get_active_theme
theme = active_theme_user&.theme
if theme
theme.theme_color
else
@ -70,7 +61,7 @@ module ThemeHelper
end
def mobile_theme_color
theme = get_active_theme
theme = active_theme_user&.theme
if theme
theme.mobile_theme_color
else
@ -78,32 +69,18 @@ module ThemeHelper
end
end
def get_active_theme
if @user&.theme
if user_signed_in?
if current_user&.show_foreign_themes?
@user.theme
else
current_user&.theme
end
else
@user.theme
end
elsif @answer&.user&.theme
if user_signed_in?
if current_user&.show_foreign_themes?
@answer.user.theme
else
current_user&.theme
end
else
@answer.user.theme
end
elsif current_user&.theme
current_user.theme
def active_theme_user
user = @user ||= @answer&.user
if user&.theme.present? && should_show_foreign_theme?
user
elsif user_signed_in?
current_user
end
end
def should_show_foreign_theme? = current_user&.show_foreign_themes || !user_signed_in?
def get_hex_color_from_theme_value(value)
"0000000#{value.to_s(16)}"[-6, 6]
end

View file

@ -5,10 +5,10 @@
%meta{ 'http-equiv': 'X-UA-Compatible', content: 'IE=edge' }
%meta{ name: 'viewport', content: 'width=device-width, initial-scale=1, user-scalable=no, viewport-fit=cover' }
- if user_signed_in?
%meta{ name: 'theme-color', content: "var(--primary)", media: '(min-width: 993px)' }
%meta{ name: 'theme-color', content: "var(--background)", media: '(max-width: 992px)' }
%meta{ name: "theme-color", content: "var(--primary)", media: "(min-width: 993px)" }
%meta{ name: "theme-color", content: "var(--background)", media: "(max-width: 992px)" }
- else
%meta{ name: 'theme-color', content: "var(--primary)" }
%meta{ name: "theme-color", content: "var(--primary)" }
- if @user&.privacy_noindex? || @answer&.user&.privacy_noindex? || @question&.user&.privacy_noindex?
%meta{ name: 'robots', content: 'noindex' }
%link{ rel: 'manifest', href: '/manifest.json', crossorigin: 'use-credentials' }
@ -19,10 +19,7 @@
%link{ rel: 'icon', href: '/images/favicon/favicon-32.png', sizes: '32x32' }
%title= yield(:title)
= stylesheet_link_tag "application", data: { 'turbo-track': "reload" }, media: "all"
- if current_user&.show_foreign_themes?
%link{ rel: 'stylesheet', href: user_path(username: @user&.screen_name || @answer&.user&.screen_name || current_user&.screen_name, format: "css"), data: { turbo_track: "reload" } }
- elsif user_signed_in?
%link{ rel: 'stylesheet', href: user_path(username: current_user&.screen_name, format: "css"), data: { turbo_track: "reload" } }
%link{ rel: "stylesheet", href: user_path(username: active_theme_user.screen_name, format: "css"), data: { turbo_track: "reload" } }
= javascript_include_tag 'application', data: { 'turbo-track': 'reload' }, defer: true
= csrf_meta_tags
= yield(:og)

View file

@ -3,33 +3,6 @@
require "rails_helper"
describe ThemeHelper, type: :helper do
describe "#render_theme" do
context "when target page doesn't have a theme" do
it "returns no theme" do
expect(helper.render_theme).to be_nil
end
end
context "when target page has a theme" do
before(:each) do
@user = FactoryBot.create(:user)
@user.theme = Theme.new
@user.save!
end
it "returns a theme" do
expect(helper.render_theme).to include("<style>:root {")
end
it "contains correct theme background colors" do
expect(helper.render_theme).to include("--primary: #5e35b1;")
end
it "properly converts color values for *-text theme attributes" do
expect(helper.render_theme).to include("--primary-text: 255, 255, 255;")
end
end
end
describe "#get_hex_color_from_theme_value" do
it "returns the proper hex value from the decimal value for white" do
@ -51,11 +24,11 @@ describe ThemeHelper, type: :helper do
end
end
describe "#get_active_theme" do
describe "#active_theme_user" do
context "when user is a guest" do
context "when target page doesn't have a theme" do
it "returns no theme" do
expect(helper.get_active_theme).to be_nil
it "returns no user" do
expect(helper.active_theme_user).to be_nil
end
end
@ -67,7 +40,7 @@ describe ThemeHelper, type: :helper do
end
it "returns a theme" do
expect(helper.get_active_theme).to be_a(Theme)
expect(helper.active_theme_user).to be_a(User)
end
end
@ -78,8 +51,8 @@ describe ThemeHelper, type: :helper do
@answer.user.save!
end
it "returns a theme" do
expect(helper.get_active_theme).to be_a(Theme)
it "returns a user" do
expect(helper.active_theme_user).to be_a(User)
end
end
end
@ -90,13 +63,7 @@ describe ThemeHelper, type: :helper do
before(:each) { sign_in(user) }
context "when user has no theme" do
context "when target page has no theme" do
it "returns no theme" do
expect(helper.get_active_theme).to be_nil
end
end
context "when target page has a theme" do
context "when target page has a corresponding user" do
let(:theme) { Theme.new }
before(:each) do
@ -106,11 +73,11 @@ describe ThemeHelper, type: :helper do
end
it "returns a theme" do
expect(helper.get_active_theme).to be(theme)
expect(helper.active_theme_user).to be(@user)
end
end
context "when target answer's user has a theme" do
context "when target page has contains an answer" do
let(:theme) { Theme.new }
before(:each) do
@ -120,7 +87,7 @@ describe ThemeHelper, type: :helper do
end
it "returns a theme" do
expect(helper.get_active_theme).to be(theme)
expect(helper.active_theme_user).to be(@answer.user)
end
end
end
@ -136,7 +103,7 @@ describe ThemeHelper, type: :helper do
context "when target page has no theme" do
it "returns the theme of the current user" do
expect(helper.get_active_theme).to eq(theme)
expect(helper.active_theme_user).to eq(user)
end
end
@ -150,7 +117,7 @@ describe ThemeHelper, type: :helper do
end
it "returns the theme of the current page" do
expect(helper.get_active_theme).to eq(user_theme)
expect(helper.active_theme_user).to eq(@user)
end
context "when user doesn't allow foreign themes" do
@ -160,7 +127,7 @@ describe ThemeHelper, type: :helper do
end
it "should return the users theme" do
expect(helper.get_active_theme).to eq(theme)
expect(helper.active_theme_user).to eq(user)
end
end
end
@ -175,7 +142,7 @@ describe ThemeHelper, type: :helper do
end
it "returns the theme of the current page" do
expect(helper.get_active_theme).to eq(answer_theme)
expect(helper.active_theme_user).to eq(@answer.user)
end
context "when user doesn't allow foreign themes" do
@ -185,7 +152,7 @@ describe ThemeHelper, type: :helper do
end
it "should return the users theme" do
expect(helper.get_active_theme).to eq(theme)
expect(helper.active_theme_user).to eq(user)
end
end
end