Implement changes to ThemeHelper requested by review

- turned theme_attribute_map into frozen constant ATTRIBUTE_MAP
- early return if no theme exists, instead of if-block
- usage of Hash#key? instead Hash[k] to confirm existence of key in hash
- Early skip/next if key is not present in Hash instead of if-block
This commit is contained in:
Andreas Nedbal 2020-05-06 13:36:43 +02:00
parent d9f67e86d9
commit 892b708c6f
1 changed files with 38 additions and 38 deletions

View File

@ -1,6 +1,5 @@
module ThemeHelper module ThemeHelper
def render_theme ATTRIBUTE_MAP = {
theme_attribute_map = {
'primary_color' => 'primary', 'primary_color' => 'primary',
'primary_text' => 'primary-text', 'primary_text' => 'primary-text',
'danger_color' => 'danger', 'danger_color' => 'danger',
@ -22,21 +21,23 @@ module ThemeHelper
'input_color' => 'input-bg', 'input_color' => 'input-bg',
'input_text' => 'input-text', 'input_text' => 'input-text',
'muted_text' => 'muted-text' 'muted_text' => 'muted-text'
} }.freeze
def render_theme
theme = get_active_theme theme = get_active_theme
if theme return unless theme
body = ":root {\n" body = ":root {\n"
theme.attributes.each do |k, v| theme.attributes.each do |k, v|
if theme_attribute_map[k] next unless ATTRIBUTE_MAP.key?(k)
if k.include? "text" if k.include? "text"
hex = get_hex_color_from_theme_value(v) hex = get_hex_color_from_theme_value(v)
body += "\t--#{theme_attribute_map[k]}: #{get_decimal_triplet_from_hex(hex)};\n" body += "\t--#{ATTRIBUTE_MAP[k]}: #{get_decimal_triplet_from_hex(hex)};\n"
else else
body += "\t--#{theme_attribute_map[k]}: ##{get_hex_color_from_theme_value(v)};\n" body += "\t--#{ATTRIBUTE_MAP[k]}: ##{get_hex_color_from_theme_value(v)};\n"
end
end end
end end
@ -44,7 +45,6 @@ module ThemeHelper
content_tag(:style, body) content_tag(:style, body)
end end
end
def get_active_theme def get_active_theme
if @user&.theme if @user&.theme