gitlab-org--gitlab-foss/lib/gitlab/themes.rb

85 lines
1.8 KiB
Ruby
Raw Normal View History

2017-08-17 11:37:36 -04:00
module Gitlab
# Module containing GitLab's application theme definitions and helper methods
# for accessing them.
module Themes
extend self
# Theme ID used when no `default_theme` configuration setting is provided.
2017-09-07 09:43:12 -04:00
APPLICATION_DEFAULT = 1
2017-08-17 11:37:36 -04:00
# Struct class representing a single Theme
Theme = Struct.new(:id, :name, :css_class)
# All available Themes
THEMES = [
2017-08-31 16:08:30 -04:00
Theme.new(1, 'Indigo', 'ui_indigo'),
2017-09-05 19:57:31 -04:00
Theme.new(2, 'Dark', 'ui_dark'),
2017-09-06 11:09:54 -04:00
Theme.new(3, 'Light', 'ui_light'),
Theme.new(4, 'Blue', 'ui_blue'),
2017-09-06 14:37:40 -04:00
Theme.new(5, 'Green', 'ui_green')
2017-08-17 11:37:36 -04:00
].freeze
# Convenience method to get a space-separated String of all the theme
# classes that might be applied to the `body` element
#
# Returns a String
def body_classes
THEMES.collect(&:css_class).uniq.join(' ')
end
# Get a Theme by its ID
#
# If the ID is invalid, returns the default Theme.
#
# id - Integer ID
#
# Returns a Theme
def by_id(id)
THEMES.detect { |t| t.id == id } || default
end
# Returns the number of defined Themes
def count
THEMES.size
end
# Get the default Theme
#
# Returns a Theme
def default
by_id(default_id)
end
# Iterate through each Theme
#
# Yields the Theme object
def each(&block)
THEMES.each(&block)
end
# Get the Theme for the specified user, or the default
#
# user - User record
#
# Returns a Theme
def for_user(user)
if user
by_id(user.theme_id)
else
default
end
end
private
def default_id
2017-09-07 10:10:39 -04:00
@default_id ||= begin
id = Gitlab.config.gitlab.default_theme.to_i
theme_ids = THEMES.map(&:id)
2017-08-17 11:37:36 -04:00
2017-09-07 10:10:39 -04:00
theme_ids.include?(id) ? id : APPLICATION_DEFAULT
2017-08-17 11:37:36 -04:00
end
end
end
end