From 892b708c6fe5c98335e97960a304b5ee0cbc63cf Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 6 May 2020 13:36:43 +0200 Subject: [PATCH] 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 --- app/helpers/theme_helper.rb | 76 ++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/app/helpers/theme_helper.rb b/app/helpers/theme_helper.rb index fea46af3..13f74a0a 100644 --- a/app/helpers/theme_helper.rb +++ b/app/helpers/theme_helper.rb @@ -1,49 +1,49 @@ module ThemeHelper - def render_theme - theme_attribute_map = { - 'primary_color' => 'primary', - 'primary_text' => 'primary-text', - 'danger_color' => 'danger', - 'danger_text' => 'danger-text', - 'warning_color' => 'warning', - 'warning_text' => 'warning-text', - 'info_color' => 'info', - 'info_text' => 'info-text', - 'success_color' => 'success', - 'success_text' => 'success-text', - 'dark_color' => 'dark', - 'dark_text' => 'dark-text', - 'light_color' => 'light', - 'light_text' => 'light-text', - 'raised_background' => 'raised-bg', - 'raised_accent' => 'raised-accent', - 'background_color' => 'background', - 'body_text' => 'body-text', - 'input_color' => 'input-bg', - 'input_text' => 'input-text', - 'muted_text' => 'muted-text' - } + ATTRIBUTE_MAP = { + 'primary_color' => 'primary', + 'primary_text' => 'primary-text', + 'danger_color' => 'danger', + 'danger_text' => 'danger-text', + 'warning_color' => 'warning', + 'warning_text' => 'warning-text', + 'info_color' => 'info', + 'info_text' => 'info-text', + 'success_color' => 'success', + 'success_text' => 'success-text', + 'dark_color' => 'dark', + 'dark_text' => 'dark-text', + 'light_color' => 'light', + 'light_text' => 'light-text', + 'raised_background' => 'raised-bg', + 'raised_accent' => 'raised-accent', + 'background_color' => 'background', + 'body_text' => 'body-text', + 'input_color' => 'input-bg', + 'input_text' => 'input-text', + 'muted_text' => 'muted-text' + }.freeze + def render_theme theme = get_active_theme - if theme + return unless theme body = ":root {\n" - theme.attributes.each do |k, v| - if theme_attribute_map[k] - if k.include? "text" - hex = get_hex_color_from_theme_value(v) - body += "\t--#{theme_attribute_map[k]}: #{get_decimal_triplet_from_hex(hex)};\n" - else - body += "\t--#{theme_attribute_map[k]}: ##{get_hex_color_from_theme_value(v)};\n" - end - end + + theme.attributes.each do |k, v| + next unless ATTRIBUTE_MAP.key?(k) + + if k.include? "text" + hex = get_hex_color_from_theme_value(v) + body += "\t--#{ATTRIBUTE_MAP[k]}: #{get_decimal_triplet_from_hex(hex)};\n" + else + body += "\t--#{ATTRIBUTE_MAP[k]}: ##{get_hex_color_from_theme_value(v)};\n" end - - body += "}" - - content_tag(:style, body) end + + body += "}" + + content_tag(:style, body) end def get_active_theme