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 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', 'danger_text' => 'danger-text',
'danger_text' => 'danger-text', 'warning_color' => 'warning',
'warning_color' => 'warning', 'warning_text' => 'warning-text',
'warning_text' => 'warning-text', 'info_color' => 'info',
'info_color' => 'info', 'info_text' => 'info-text',
'info_text' => 'info-text', 'success_color' => 'success',
'success_color' => 'success', 'success_text' => 'success-text',
'success_text' => 'success-text', 'dark_color' => 'dark',
'dark_color' => 'dark', 'dark_text' => 'dark-text',
'dark_text' => 'dark-text', 'light_color' => 'light',
'light_color' => 'light', 'light_text' => 'light-text',
'light_text' => 'light-text', 'raised_background' => 'raised-bg',
'raised_background' => 'raised-bg', 'raised_accent' => 'raised-accent',
'raised_accent' => 'raised-accent', 'background_color' => 'background',
'background_color' => 'background', 'body_text' => 'body-text',
'body_text' => 'body-text', '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|
if theme_attribute_map[k] theme.attributes.each do |k, v|
if k.include? "text" next unless ATTRIBUTE_MAP.key?(k)
hex = get_hex_color_from_theme_value(v)
body += "\t--#{theme_attribute_map[k]}: #{get_decimal_triplet_from_hex(hex)};\n" if k.include? "text"
else hex = get_hex_color_from_theme_value(v)
body += "\t--#{theme_attribute_map[k]}: ##{get_hex_color_from_theme_value(v)};\n" body += "\t--#{ATTRIBUTE_MAP[k]}: #{get_decimal_triplet_from_hex(hex)};\n"
end else
end body += "\t--#{ATTRIBUTE_MAP[k]}: ##{get_hex_color_from_theme_value(v)};\n"
end end
body += "}"
content_tag(:style, body)
end end
body += "}"
content_tag(:style, body)
end end
def get_active_theme def get_active_theme