Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: edit_user_link rendering #3475

Merged
merged 2 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions app/helpers/rails_admin/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,24 @@ def actions(scope = :all, abstract_model = nil, object = nil)
end

def edit_user_link
return nil unless _current_user.respond_to?(:email)
return nil unless abstract_model = RailsAdmin.config(_current_user.class).abstract_model

content = [
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: ''),
content_tag(:span, _current_user.email),
].compact.join.html_safe
if (edit_action = RailsAdmin::Config::Actions.find(:edit, controller: controller, abstract_model: abstract_model, object: _current_user)).try(:authorized?)
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'), class: 'nav-link'
return nil unless _current_user.try(:email).present?
return nil unless (abstract_model = RailsAdmin.config(_current_user.class).abstract_model)

edit_action = action(:edit, abstract_model, _current_user)
authorized = edit_action.try(:authorized?)
content = edit_user_link_label

if authorized
edit_url = rails_admin.url_for(
action_name: edit_action.action_name,
model_name: abstract_model.to_param,
controller: 'rails_admin/main',
id: _current_user.id,
)

link_to content, edit_url, class: 'nav-link'
else
content_tag :span, content
content_tag :span, content, class: 'nav-link'
end
end

Expand Down Expand Up @@ -206,5 +213,20 @@ def handle_asset_dependency_error
end
raise e
end

private

def edit_user_link_label
[
RailsAdmin::Config.show_gravatar &&
image_tag(gravatar_url(_current_user.email), alt: ''),

content_tag(:span, _current_user.email),
].filter(&:present?).join.html_safe
end

def gravatar_url(email)
"https://secure.gravatar.com/avatar/#{Digest::MD5.hexdigest email}?s=30"
end
end
end
12 changes: 12 additions & 0 deletions spec/helpers/rails_admin/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,18 @@
expect(result).to include(user.email)
expect(result).not_to match('href')
end

it 'without gravatar and email without a link' do
RailsAdmin.config do |config|
config.show_gravatar = false
end

result = helper.edit_user_link
expect(result).not_to include('gravatar')
expect(result).not_to match('href')
expect(result).to include(user.email)
expect(result).to match("<span class=\"nav-link\"><span>#{user.email}</span></span>")
end
end
end
end
Expand Down