diff options
author | Stan Hu <stanhu@gmail.com> | 2019-01-13 07:11:26 -0800 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2019-01-13 08:02:02 -0800 |
commit | 2265ce34afe05d2011343cf5674db979e6c250de (patch) | |
tree | 61d364475faf1a6138885283e01e24c41f144e74 | |
parent | 1161c99e5c5a6e717127b83665de00068d810e0e (diff) | |
download | gitlab-ce-2265ce34afe05d2011343cf5674db979e6c250de.tar.gz |
Fix no avatar not showing in user selection box
After upgrading to Ruby 2.5.3, we switched `URI.join` in favor of
`Gitlab::Utils.append_path`. However,
ActionController::Base.helpers.image_path can return a full URL if a CDN
host is present.
Rails provides a way to generate the full URL using the asset path, but
that doesn't appear to work because `request` is nil`.
Revert to the previous behavior to handle CDNs and relative URLs.
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/56268
-rw-r--r-- | changelogs/unreleased/sh-fix-gon-helper-avatar.yml | 5 | ||||
-rw-r--r-- | lib/gitlab/gon_helper.rb | 15 | ||||
-rw-r--r-- | spec/lib/gitlab/gon_helper_spec.rb | 9 |
3 files changed, 25 insertions, 4 deletions
diff --git a/changelogs/unreleased/sh-fix-gon-helper-avatar.yml b/changelogs/unreleased/sh-fix-gon-helper-avatar.yml new file mode 100644 index 00000000000..c83273608ad --- /dev/null +++ b/changelogs/unreleased/sh-fix-gon-helper-avatar.yml @@ -0,0 +1,5 @@ +--- +title: Fix no avatar not showing in user selection box +merge_request: 24346 +author: +type: fixed diff --git a/lib/gitlab/gon_helper.rb b/lib/gitlab/gon_helper.rb index 15137140639..9b1794eec91 100644 --- a/lib/gitlab/gon_helper.rb +++ b/lib/gitlab/gon_helper.rb @@ -8,10 +8,7 @@ module Gitlab def add_gon_variables gon.api_version = 'v4' - gon.default_avatar_url = - Gitlab::Utils.append_path( - Gitlab.config.gitlab.url, - ActionController::Base.helpers.image_path('no_avatar.png')) + gon.default_avatar_url = default_avatar_url gon.max_file_size = Gitlab::CurrentSettings.max_attachment_size gon.asset_host = ActionController::Base.asset_host gon.webpack_public_path = webpack_public_path @@ -50,5 +47,15 @@ module Gitlab # use this method to push multiple feature flags. gon.push({ features: { var_name => enabled } }, true) end + + def default_avatar_url + # We can't use ActionController::Base.helpers.image_url because it + # doesn't return an actual URL because request is nil for some reason. + # + # We also can't use Gitlab::Utils.append_path because the image path + # may be an absolute URL. + URI.join(Gitlab.config.gitlab.url, + ActionController::Base.helpers.image_path('no_avatar.png')).to_s + end end end diff --git a/spec/lib/gitlab/gon_helper_spec.rb b/spec/lib/gitlab/gon_helper_spec.rb index c6f09ca2112..1ff2334bacf 100644 --- a/spec/lib/gitlab/gon_helper_spec.rb +++ b/spec/lib/gitlab/gon_helper_spec.rb @@ -29,4 +29,13 @@ describe Gitlab::GonHelper do helper.push_frontend_feature_flag(:my_feature_flag, 10) end end + + describe '#default_avatar_url' do + it 'returns an absolute URL' do + url = helper.default_avatar_url + + expect(url).to match(/^http/) + expect(url).to match(/no_avatar.*png$/) + end + end end |