BigW Consortium Gitlab

themes.rb 1.93 KB
Newer Older
1 2 3 4
module Gitlab
  # Module containing GitLab's application theme definitions and helper methods
  # for accessing them.
  module Themes
5 6
    extend self

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    # Theme ID used when no `default_theme` configuration setting is provided.
    APPLICATION_DEFAULT = 2

    # Struct class representing a single Theme
    Theme = Struct.new(:id, :name, :css_class)

    # All available Themes
    THEMES = [
      Theme.new(1, 'Graphite', 'ui_graphite'),
      Theme.new(2, 'Charcoal', 'ui_charcoal'),
      Theme.new(3, 'Green',    'ui_green'),
      Theme.new(4, 'Gray',     'ui_gray'),
      Theme.new(5, 'Violet',   'ui_violet'),
      Theme.new(6, 'Blue',     'ui_blue')
    ].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
27
    def body_classes
28 29 30 31 32 33 34 35 36 37
      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
38
    def by_id(id)
39 40 41
      THEMES.detect { |t| t.id == id } || default
    end

42
    # Returns the number of defined Themes
43
    def count
44 45 46
      THEMES.size
    end

47 48 49
    # Get the default Theme
    #
    # Returns a Theme
50
    def default
51 52 53 54 55 56
      by_id(default_id)
    end

    # Iterate through each Theme
    #
    # Yields the Theme object
57
    def each(&block)
58 59 60
      THEMES.each(&block)
    end

61 62 63 64 65
    # Get the Theme for the specified user, or the default
    #
    # user - User record
    #
    # Returns a Theme
66
    def for_user(user)
67 68 69 70 71 72 73
      if user
        by_id(user.theme_id)
      else
        default
      end
    end

74 75
    private

76
    def default_id
77 78 79 80 81 82 83 84 85 86 87
      id = Gitlab.config.gitlab.default_theme.to_i

      # Prevent an invalid configuration setting from causing an infinite loop
      if id < THEMES.first.id || id > THEMES.last.id
        APPLICATION_DEFAULT
      else
        id
      end
    end
  end
end