Skip to content

Commit

Permalink
Simplify theme helper
Browse files Browse the repository at this point in the history
  • Loading branch information
raccube committed Feb 1, 2023
1 parent 7caa767 commit fb473ff
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 90 deletions.
45 changes: 11 additions & 34 deletions app/helpers/theme_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -61,7 +52,7 @@ def get_color_for_key(key, color)
end

def theme_color
theme = get_active_theme
theme = active_theme_user&.theme
if theme
theme.theme_color
else
Expand All @@ -70,40 +61,26 @@ def theme_color
end

def mobile_theme_color
theme = get_active_theme
theme = active_theme_user&.theme
if theme
theme.mobile_theme_color
else
"#f0edf4"
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
Expand Down
11 changes: 4 additions & 7 deletions app/views/layouts/base.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand All @@ -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' }
- 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)
Expand Down
65 changes: 16 additions & 49 deletions spec/helpers/theme_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,7 @@

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 ThemeHelper, type: :helper do

describe "#get_hex_color_from_theme_value" do
it "returns the proper hex value from the decimal value for white" do
Expand All @@ -51,11 +24,11 @@
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

Expand All @@ -67,7 +40,7 @@
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

Expand All @@ -78,8 +51,8 @@
@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
Expand All @@ -90,13 +63,7 @@
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
Expand All @@ -106,11 +73,11 @@
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
Expand All @@ -120,7 +87,7 @@
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
Expand All @@ -136,7 +103,7 @@

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

Expand All @@ -150,7 +117,7 @@
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
Expand All @@ -160,7 +127,7 @@
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
Expand All @@ -175,7 +142,7 @@
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
Expand All @@ -185,7 +152,7 @@
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
Expand Down

0 comments on commit fb473ff

Please sign in to comment.