From 5e03c3d84e64b05694c0b958647701a25bfe328f Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Sun, 5 Jan 2020 19:41:45 +0000 Subject: [PATCH] Adjust naming of user and model attributes - Remove unnecessary attributes that cause additional SQL queries - Remove unneeded check for nil on current_item --- app/helpers/application_helper.rb | 4 ++-- app/javascript/src/javascripts/current_user.js | 2 +- app/models/artist.rb | 2 +- app/models/wiki_page.rb | 2 +- app/views/layouts/default.html.erb | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ded81b7ba..d67633499 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -205,9 +205,9 @@ module ApplicationHelper def body_attributes(user = CurrentUser.user, current_item = nil) user_attributes = %i[id name level level_string theme] + User::BOOLEAN_ATTRIBUTES.map(&:to_sym) user_attributes += User::Roles.map { |role| :"is_#{role}?" } - mapped_user_attributes = data_attributes_for(user, "user", user_attributes) + mapped_user_attributes = data_attributes_for(user, "current-user", user_attributes) model_attributes = (!current_item.nil? ? (!current_item.id.nil? ? [:id] : [] ) + current_item.html_data_attributes : []) - mapped_model_attributes = (!current_item.nil? ? data_attributes_for(current_item, "show", model_attributes) : {} ) + mapped_model_attributes = (!current_item.nil? ? data_attributes_for(current_item, current_item.model_name.singular.dasherize, model_attributes) : {} ) all_mapped_attributes = mapped_user_attributes.merge(mapped_model_attributes) controller_param = params[:controller].parameterize.dasherize action_param = params[:action].parameterize.dasherize diff --git a/app/javascript/src/javascripts/current_user.js b/app/javascript/src/javascripts/current_user.js index 286253e03..e38ae191f 100644 --- a/app/javascript/src/javascripts/current_user.js +++ b/app/javascript/src/javascripts/current_user.js @@ -1,7 +1,7 @@ let CurrentUser = {}; CurrentUser.data = function(key) { - return $("body").data(`user-${key}`); + return $("body").data(`current-user-${key}`); }; CurrentUser.update = function(settings) { diff --git a/app/models/artist.rb b/app/models/artist.rb index 45fbf8312..a10390064 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -519,7 +519,7 @@ class Artist < ApplicationRecord module ApiMethods def html_data_attributes - [:creator_id, [:wiki_page, :id]] + [:creator_id] end end diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 31f55919a..15b401548 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -117,7 +117,7 @@ class WikiPage < ApplicationRecord module ApiMethods def html_data_attributes - [:category_name, [:artist, :id]] + [:category_name] end end diff --git a/app/views/layouts/default.html.erb b/app/views/layouts/default.html.erb index 741c8d50c..0894f7f24 100644 --- a/app/views/layouts/default.html.erb +++ b/app/views/layouts/default.html.erb @@ -75,7 +75,7 @@ <%= yield :html_header %> <%= raw Danbooru.config.custom_html_header_content %> -<%= tag.body **body_attributes(CurrentUser.user, (defined?(@current_item) ? @current_item : nil)) do %> +<%= tag.body **body_attributes(CurrentUser.user, @current_item) do %> <%= render "news_updates/listing" %>