html: add controller & action attributes to <body> tag (#3844).

This commit is contained in:
evazion
2018-08-26 14:10:47 -05:00
parent b98bc26397
commit 4e1d76da81
3 changed files with 21 additions and 6 deletions

View File

@@ -192,12 +192,27 @@ module ApplicationHelper
attributes = [:id, :name, :level, :level_string, :can_approve_posts?, :can_upload_free?]
attributes += User::Roles.map { |role| :"is_#{role}?" }
controller_param = params[:controller].parameterize.dasherize
action_param = params[:action].parameterize.dasherize
{
lang: "en",
class: "c-#{controller_param} a-#{action_param}",
data: {
controller: controller_param,
action: action_param,
**data_attributes_for(user, "user", attributes)
}
}
end
def data_attributes_for(record, prefix, attributes)
attributes.map do |attr|
name = attr.to_s.dasherize.delete("?")
value = user.send(attr)
value = record.send(attr)
%{data-user-#{name}="#{h(value)}"}
end.join(" ").html_safe
[:"#{prefix}-#{name}", value]
end.to_h
end
protected

View File

@@ -117,7 +117,7 @@ PostModeMenu.change = function() {
return;
}
var $body = $(document.body);
$body.removeClass();
$body.removeClass((i, classNames) => classNames.split(/ /).filter(name => /^mode-/.test(name)).join(" "));
$body.addClass("mode-" + s);
Cookie.put("mode", s, 1);

View File

@@ -86,7 +86,7 @@
</script>
</head>
<body lang="en" <%= body_attributes(CurrentUser.user) %>>
<%= tag.body **body_attributes(CurrentUser.user) do %>
<header id="top">
<%= render "news_updates/listing" %>
@@ -137,5 +137,5 @@
</div>
<%= render "static/footer" %>
</body>
<% end %>
</html>