diff options
-rw-r--r-- | app/helpers/application_helper.rb | 29 | ||||
-rw-r--r-- | app/helpers/gitlab_markdown_helper.rb | 1 | ||||
-rw-r--r-- | app/helpers/preferences_helper.rb | 31 | ||||
-rw-r--r-- | app/views/layouts/application.html.haml | 6 | ||||
-rw-r--r-- | app/views/layouts/errors.html.haml | 2 | ||||
-rw-r--r-- | app/views/profiles/preferences/update.js.erb | 2 | ||||
-rw-r--r-- | spec/helpers/application_helper_spec.rb | 21 | ||||
-rw-r--r-- | spec/helpers/preferences_helper_spec.rb | 53 |
8 files changed, 90 insertions, 55 deletions
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 62794bc5f4c..10d7aa11209 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -2,26 +2,6 @@ require 'digest/md5' require 'uri' module ApplicationHelper - COLOR_SCHEMES = { - 1 => 'white', - 2 => 'dark', - 3 => 'solarized-light', - 4 => 'solarized-dark', - 5 => 'monokai', - } - COLOR_SCHEMES.default = 'white' - - # Helper method to access the COLOR_SCHEMES - # - # The keys are the `color_scheme_ids` - # The values are the `name` of the scheme. - # - # The preview images are `name-scheme-preview.png` - # The stylesheets should use the css class `.name` - def color_schemes - COLOR_SCHEMES.freeze - end - # Check if a particular controller is the current one # # args - One or more controller names to check @@ -138,15 +118,6 @@ module ApplicationHelper Emoji.names.to_s end - def app_theme - theme = Gitlab::Themes.by_id(current_user.try(:theme_id)) - theme.css_class - end - - def user_color_scheme_class - COLOR_SCHEMES[current_user.try(:color_scheme_id)] if defined?(current_user) - end - # Define whenever show last push event # with suggestion to create MR def show_last_push_widget?(event) diff --git a/app/helpers/gitlab_markdown_helper.rb b/app/helpers/gitlab_markdown_helper.rb index 2777944fc9d..9aabe01f60e 100644 --- a/app/helpers/gitlab_markdown_helper.rb +++ b/app/helpers/gitlab_markdown_helper.rb @@ -2,6 +2,7 @@ require 'nokogiri' module GitlabMarkdownHelper include Gitlab::Markdown + include PreferencesHelper # Use this in places where you would normally use link_to(gfm(...), ...). # diff --git a/app/helpers/preferences_helper.rb b/app/helpers/preferences_helper.rb new file mode 100644 index 00000000000..04873b9bd00 --- /dev/null +++ b/app/helpers/preferences_helper.rb @@ -0,0 +1,31 @@ +# Helper methods for per-User preferences +module PreferencesHelper + COLOR_SCHEMES = { + 1 => 'white', + 2 => 'dark', + 3 => 'solarized-light', + 4 => 'solarized-dark', + 5 => 'monokai', + } + COLOR_SCHEMES.default = 'white' + + # Helper method to access the COLOR_SCHEMES + # + # The keys are the `color_scheme_ids` + # The values are the `name` of the scheme. + # + # The preview images are `name-scheme-preview.png` + # The stylesheets should use the css class `.name` + def color_schemes + COLOR_SCHEMES.freeze + end + + def user_application_theme + theme = Gitlab::Themes.by_id(current_user.try(:theme_id)) + theme.css_class + end + + def user_color_scheme_class + COLOR_SCHEMES[current_user.try(:color_scheme_id)] if defined?(current_user) + end +end diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 173033f7eab..678ed3c2c1f 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -1,10 +1,10 @@ !!! 5 %html{ lang: "en"} = render "layouts/head" - %body{class: "#{app_theme}", :'data-page' => body_data_page} - / Ideally this would be inside the head, but turbolinks only evaluates page-specific JS in the body. + %body{class: "#{user_application_theme}", 'data-page' => body_data_page} + -# Ideally this would be inside the head, but turbolinks only evaluates page-specific JS in the body. = yield :scripts_body_top - + - if current_user = render "layouts/header/default", title: header_title - else diff --git a/app/views/layouts/errors.html.haml b/app/views/layouts/errors.html.haml index 2e3a2b16eb7..2af265a2296 100644 --- a/app/views/layouts/errors.html.haml +++ b/app/views/layouts/errors.html.haml @@ -1,7 +1,7 @@ !!! 5 %html{ lang: "en"} = render "layouts/head" - %body{class: "#{app_theme} application"} + %body{class: "#{user_application_theme} application"} = render "layouts/header/empty" .container.navless-container = render "layouts/flash" diff --git a/app/views/profiles/preferences/update.js.erb b/app/views/profiles/preferences/update.js.erb index 830df228557..cd2c5b632e0 100644 --- a/app/views/profiles/preferences/update.js.erb +++ b/app/views/profiles/preferences/update.js.erb @@ -1,3 +1,3 @@ // Remove body class for any previous theme, re-add current one $('body').removeClass('<%= Gitlab::Themes.body_classes %>') -$('body').addClass('<%= app_theme %>') +$('body').addClass('<%= user_application_theme %>') diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 3307ac776fc..47e10197f5c 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -185,27 +185,6 @@ describe ApplicationHelper do end end - describe 'user_color_scheme_class' do - context 'with current_user is nil' do - it 'should return a string' do - allow(self).to receive(:current_user).and_return(nil) - expect(user_color_scheme_class).to be_kind_of(String) - end - end - - context 'with a current_user' do - (1..5).each do |color_scheme_id| - context "with color_scheme_id == #{color_scheme_id}" do - it 'should return a string' do - current_user = double(:color_scheme_id => color_scheme_id) - allow(self).to receive(:current_user).and_return(current_user) - expect(user_color_scheme_class).to be_kind_of(String) - end - end - end - end - end - describe 'simple_sanitize' do let(:a_tag) { '<a href="#">Foo</a>' } diff --git a/spec/helpers/preferences_helper_spec.rb b/spec/helpers/preferences_helper_spec.rb new file mode 100644 index 00000000000..095b016e6ec --- /dev/null +++ b/spec/helpers/preferences_helper_spec.rb @@ -0,0 +1,53 @@ +require 'spec_helper' + +describe PreferencesHelper do + describe 'user_application_theme' do + context 'with a user' do + it "returns user's theme's css_class" do + user = double('user', theme_id: 3) + allow(self).to receive(:current_user).and_return(user) + expect(user_application_theme).to eq 'ui_green' + end + + it 'returns the default when id is invalid' do + user = double('user', theme_id: Gitlab::Themes::THEMES.size + 5) + + allow(Gitlab.config.gitlab).to receive(:default_theme).and_return(2) + allow(self).to receive(:current_user).and_return(user) + + expect(user_application_theme).to eq 'ui_charcoal' + end + end + + context 'without a user' do + before do + allow(self).to receive(:current_user).and_return(nil) + end + + it 'returns the default theme' do + expect(user_application_theme).to eq Gitlab::Themes.default.css_class + end + end + end + + describe 'user_color_scheme_class' do + context 'with current_user is nil' do + it 'should return a string' do + allow(self).to receive(:current_user).and_return(nil) + expect(user_color_scheme_class).to be_kind_of(String) + end + end + + context 'with a current_user' do + (1..5).each do |color_scheme_id| + context "with color_scheme_id == #{color_scheme_id}" do + it 'should return a string' do + current_user = double(:color_scheme_id => color_scheme_id) + allow(self).to receive(:current_user).and_return(current_user) + expect(user_color_scheme_class).to be_kind_of(String) + end + end + end + end + end +end |