Commit 2c69ced3343d865bab613c6f76e5c6066b4f29b3
Exists in
master
and in
4 other branches
Merge pull request #4177 from docwhat/fix-user_color_scheme_class
user_color_scheme_class with no current_user
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 | 2 | require 'uri' |
| 3 | 3 | |
| 4 | 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 | 13 | # Check if a particular controller is the current one |
| 7 | 14 | # |
| ... | ... | @@ -124,17 +131,7 @@ module ApplicationHelper |
| 124 | 131 | end |
| 125 | 132 | |
| 126 | 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 | 135 | end |
| 139 | 136 | |
| 140 | 137 | # Define whenever show last push event | ... | ... |
spec/helpers/application_helper_spec.rb
| ... | ... | @@ -83,4 +83,26 @@ describe ApplicationHelper do |
| 83 | 83 | end |
| 84 | 84 | |
| 85 | 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 | 108 | end | ... | ... |