mirror of
https://github.com/Retrospring/retrospring.git
synced 2025-03-31 23:12:13 +02:00
Simplify theme helper
This commit is contained in:
parent
99403988b7
commit
c3a0455acb
3 changed files with 30 additions and 89 deletions
|
@ -28,15 +28,6 @@ module ThemeHelper
|
||||||
"muted_text" => "muted-text",
|
"muted_text" => "muted-text",
|
||||||
}.freeze
|
}.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)
|
def get_theme_css(theme)
|
||||||
body = ":root {\n"
|
body = ":root {\n"
|
||||||
|
|
||||||
|
@ -61,7 +52,7 @@ module ThemeHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def theme_color
|
def theme_color
|
||||||
theme = get_active_theme
|
theme = active_theme_user&.theme
|
||||||
if theme
|
if theme
|
||||||
theme.theme_color
|
theme.theme_color
|
||||||
else
|
else
|
||||||
|
@ -70,7 +61,7 @@ module ThemeHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def mobile_theme_color
|
def mobile_theme_color
|
||||||
theme = get_active_theme
|
theme = active_theme_user&.theme
|
||||||
if theme
|
if theme
|
||||||
theme.mobile_theme_color
|
theme.mobile_theme_color
|
||||||
else
|
else
|
||||||
|
@ -78,32 +69,18 @@ module ThemeHelper
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_active_theme
|
def active_theme_user
|
||||||
if @user&.theme
|
user = @user ||= @answer&.user
|
||||||
if user_signed_in?
|
|
||||||
if current_user&.show_foreign_themes?
|
if user&.theme.present? && should_show_foreign_theme?
|
||||||
@user.theme
|
user
|
||||||
else
|
elsif user_signed_in?
|
||||||
current_user&.theme
|
current_user
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def should_show_foreign_theme? = current_user&.show_foreign_themes || !user_signed_in?
|
||||||
|
|
||||||
def get_hex_color_from_theme_value(value)
|
def get_hex_color_from_theme_value(value)
|
||||||
"0000000#{value.to_s(16)}"[-6, 6]
|
"0000000#{value.to_s(16)}"[-6, 6]
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,10 +5,10 @@
|
||||||
%meta{ 'http-equiv': 'X-UA-Compatible', content: 'IE=edge' }
|
%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' }
|
%meta{ name: 'viewport', content: 'width=device-width, initial-scale=1, user-scalable=no, viewport-fit=cover' }
|
||||||
- if user_signed_in?
|
- if user_signed_in?
|
||||||
%meta{ name: 'theme-color', content: "var(--primary)", media: '(min-width: 993px)' }
|
%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(--background)", media: "(max-width: 992px)" }
|
||||||
- else
|
- 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?
|
- if @user&.privacy_noindex? || @answer&.user&.privacy_noindex? || @question&.user&.privacy_noindex?
|
||||||
%meta{ name: 'robots', content: 'noindex' }
|
%meta{ name: 'robots', content: 'noindex' }
|
||||||
%link{ rel: 'manifest', href: '/manifest.json', crossorigin: 'use-credentials' }
|
%link{ rel: 'manifest', href: '/manifest.json', crossorigin: 'use-credentials' }
|
||||||
|
@ -19,10 +19,7 @@
|
||||||
%link{ rel: 'icon', href: '/images/favicon/favicon-32.png', sizes: '32x32' }
|
%link{ rel: 'icon', href: '/images/favicon/favicon-32.png', sizes: '32x32' }
|
||||||
%title= yield(:title)
|
%title= yield(:title)
|
||||||
= stylesheet_link_tag "application", data: { 'turbo-track': "reload" }, media: "all"
|
= stylesheet_link_tag "application", data: { 'turbo-track': "reload" }, media: "all"
|
||||||
- if current_user&.show_foreign_themes?
|
%link{ rel: "stylesheet", href: user_path(username: active_theme_user.screen_name, format: "css"), data: { turbo_track: "reload" } }
|
||||||
%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" } }
|
|
||||||
= javascript_include_tag 'application', data: { 'turbo-track': 'reload' }, defer: true
|
= javascript_include_tag 'application', data: { 'turbo-track': 'reload' }, defer: true
|
||||||
= csrf_meta_tags
|
= csrf_meta_tags
|
||||||
= yield(:og)
|
= yield(:og)
|
||||||
|
|
|
@ -3,33 +3,6 @@
|
||||||
require "rails_helper"
|
require "rails_helper"
|
||||||
|
|
||||||
describe ThemeHelper, type: :helper do
|
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
|
describe "#get_hex_color_from_theme_value" do
|
||||||
it "returns the proper hex value from the decimal value for white" 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
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#get_active_theme" do
|
describe "#active_theme_user" do
|
||||||
context "when user is a guest" do
|
context "when user is a guest" do
|
||||||
context "when target page doesn't have a theme" do
|
context "when target page doesn't have a theme" do
|
||||||
it "returns no theme" do
|
it "returns no user" do
|
||||||
expect(helper.get_active_theme).to be_nil
|
expect(helper.active_theme_user).to be_nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -67,7 +40,7 @@ describe ThemeHelper, type: :helper do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns a theme" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -78,8 +51,8 @@ describe ThemeHelper, type: :helper do
|
||||||
@answer.user.save!
|
@answer.user.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns a theme" do
|
it "returns a user" do
|
||||||
expect(helper.get_active_theme).to be_a(Theme)
|
expect(helper.active_theme_user).to be_a(User)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -90,13 +63,7 @@ describe ThemeHelper, type: :helper do
|
||||||
before(:each) { sign_in(user) }
|
before(:each) { sign_in(user) }
|
||||||
|
|
||||||
context "when user has no theme" do
|
context "when user has no theme" do
|
||||||
context "when target page has no theme" do
|
context "when target page has a corresponding user" do
|
||||||
it "returns no theme" do
|
|
||||||
expect(helper.get_active_theme).to be_nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when target page has a theme" do
|
|
||||||
let(:theme) { Theme.new }
|
let(:theme) { Theme.new }
|
||||||
|
|
||||||
before(:each) do
|
before(:each) do
|
||||||
|
@ -106,11 +73,11 @@ describe ThemeHelper, type: :helper do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns a theme" do
|
it "returns a theme" do
|
||||||
expect(helper.get_active_theme).to be(theme)
|
expect(helper.active_theme_user).to be(@user)
|
||||||
end
|
end
|
||||||
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 }
|
let(:theme) { Theme.new }
|
||||||
|
|
||||||
before(:each) do
|
before(:each) do
|
||||||
|
@ -120,7 +87,7 @@ describe ThemeHelper, type: :helper do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns a theme" do
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -136,7 +103,7 @@ describe ThemeHelper, type: :helper do
|
||||||
|
|
||||||
context "when target page has no theme" do
|
context "when target page has no theme" do
|
||||||
it "returns the theme of the current user" 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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -150,7 +117,7 @@ describe ThemeHelper, type: :helper do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns the theme of the current page" do
|
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
|
end
|
||||||
|
|
||||||
context "when user doesn't allow foreign themes" do
|
context "when user doesn't allow foreign themes" do
|
||||||
|
@ -160,7 +127,7 @@ describe ThemeHelper, type: :helper do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return the users theme" do
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -175,7 +142,7 @@ describe ThemeHelper, type: :helper do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns the theme of the current page" do
|
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
|
end
|
||||||
|
|
||||||
context "when user doesn't allow foreign themes" do
|
context "when user doesn't allow foreign themes" do
|
||||||
|
@ -185,7 +152,7 @@ describe ThemeHelper, type: :helper do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return the users theme" do
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue