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,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