Commit 336750c916498e99505f593067d4eeadccb3c261
1 parent
0447c731
Exists in
master
and in
4 other branches
user_color_scheme_class with current_user
This refactors the `user_color_scheme_class` to use a hash with a default. This prevents problems with workers that don't have access to the current_user. Fixes issue #2758
Showing
2 changed files
with
30 additions
and
11 deletions
Show diff stats
app/helpers/application_helper.rb
@@ -2,6 +2,13 @@ require 'digest/md5' | @@ -2,6 +2,13 @@ require 'digest/md5' | ||
2 | require 'uri' | 2 | require 'uri' |
3 | 3 | ||
4 | module ApplicationHelper | 4 | module ApplicationHelper |
5 | + COLOR_SCHEMES = { | ||
6 | + 1 => 'white', | ||
7 | + 2 => 'black', | ||
8 | + 3 => 'solarized-dark', | ||
9 | + 4 => 'monokai', | ||
10 | + } | ||
11 | + COLOR_SCHEMES.default = 'white' | ||
5 | 12 | ||
6 | # Check if a particular controller is the current one | 13 | # Check if a particular controller is the current one |
7 | # | 14 | # |
@@ -124,17 +131,7 @@ module ApplicationHelper | @@ -124,17 +131,7 @@ module ApplicationHelper | ||
124 | end | 131 | end |
125 | 132 | ||
126 | def user_color_scheme_class | 133 | def user_color_scheme_class |
127 | - # in case we dont have current_user (ex. in mailer) | ||
128 | - return 1 unless defined?(current_user) | ||
129 | - | ||
130 | - case current_user.color_scheme_id | ||
131 | - when 1 then 'white' | ||
132 | - when 2 then 'black' | ||
133 | - when 3 then 'solarized-dark' | ||
134 | - when 4 then 'monokai' | ||
135 | - else | ||
136 | - 'white' | ||
137 | - end | 134 | + COLOR_SCHEMES[current_user.try(:color_scheme_id)] |
138 | end | 135 | end |
139 | 136 | ||
140 | # Define whenever show last push event | 137 | # Define whenever show last push event |
spec/helpers/application_helper_spec.rb
@@ -83,4 +83,26 @@ describe ApplicationHelper do | @@ -83,4 +83,26 @@ describe ApplicationHelper do | ||
83 | end | 83 | end |
84 | 84 | ||
85 | end | 85 | end |
86 | + | ||
87 | + describe "user_color_scheme_class" do | ||
88 | + context "with current_user is nil" do | ||
89 | + it "should return a string" do | ||
90 | + stub!(:current_user).and_return(nil) | ||
91 | + user_color_scheme_class.should be_kind_of(String) | ||
92 | + end | ||
93 | + end | ||
94 | + | ||
95 | + context "with a current_user" do | ||
96 | + (1..5).each do |color_scheme_id| | ||
97 | + context "with color_scheme_id == #{color_scheme_id}" do | ||
98 | + it "should return a string" do | ||
99 | + current_user = double(:color_scheme_id => color_scheme_id) | ||
100 | + stub!(:current_user).and_return(current_user) | ||
101 | + user_color_scheme_class.should be_kind_of(String) | ||
102 | + end | ||
103 | + end | ||
104 | + end | ||
105 | + end | ||
106 | + end | ||
107 | + | ||
86 | end | 108 | end |