Skip to content

Commit bd44929

Browse files
committed
Show Gravater and email even if the current user is not editable
Closes #3237
1 parent 1190d51 commit bd44929

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

app/assets/stylesheets/rails_admin/base/theming.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ body.rails_admin {
2626
margin-left: ($avatar-size + 5px);
2727
}
2828
}
29+
30+
> span {
31+
color: $navbar-default-link-color;
32+
line-height: $navbar-height;
33+
padding-left: $navbar-padding-horizontal;
34+
padding-right: $navbar-padding-horizontal;
35+
}
2936
}
3037
}
3138

app/helpers/rails_admin/application_helper.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ def actions(scope = :all, abstract_model = nil, object = nil)
2222
def edit_user_link
2323
return nil unless _current_user.respond_to?(:email)
2424
return nil unless abstract_model = RailsAdmin.config(_current_user.class).abstract_model
25-
return nil unless (edit_action = RailsAdmin::Config::Actions.find(:edit, controller: controller, abstract_model: abstract_model, object: _current_user)).try(:authorized?)
26-
link_to rails_admin.url_for(action: edit_action.action_name, model_name: abstract_model.to_param, id: _current_user.id, controller: 'rails_admin/main') do
27-
html = []
28-
html << image_tag("#{(request.ssl? ? 'https://secure' : 'http://www')}.gravatar.com/avatar/#{Digest::MD5.hexdigest _current_user.email}?s=30", alt: '') if RailsAdmin::Config.show_gravatar && _current_user.email.present?
29-
html << content_tag(:span, _current_user.email)
30-
html.join.html_safe
25+
content = [
26+
RailsAdmin::Config.show_gravatar && _current_user.email.present? && image_tag("#{(request.ssl? ? 'https://secure' : 'http://www')}.gravatar.com/avatar/#{Digest::MD5.hexdigest _current_user.email}?s=30", alt: ''),
27+
content_tag(:span, _current_user.email),
28+
].compact.join.html_safe
29+
if (edit_action = RailsAdmin::Config::Actions.find(:edit, controller: controller, abstract_model: abstract_model, object: _current_user)).try(:authorized?)
30+
link_to content, rails_admin.url_for(action: edit_action.action_name, model_name: abstract_model.to_param, id: _current_user.id, controller: 'rails_admin/main')
31+
else
32+
content_tag :span, content
3133
end
3234
end
3335

spec/helpers/rails_admin/application_helper_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,21 @@
470470
result = helper.edit_user_link
471471
expect(result).not_to include('gravatar')
472472
end
473+
474+
context 'when the user is not authorized to perform edit' do
475+
let(:user) { FactoryBot.create(:user) }
476+
before do
477+
allow_any_instance_of(RailsAdmin::Config::Actions::Edit).to receive(:authorized?).and_return(false)
478+
allow(helper).to receive(:_current_user).and_return(user)
479+
end
480+
481+
it 'show gravatar and email without a link' do
482+
result = helper.edit_user_link
483+
expect(result).to include('gravatar')
484+
expect(result).to include(user.email)
485+
expect(result).not_to match('href')
486+
end
487+
end
473488
end
474489
end
475490

0 commit comments

Comments
 (0)