From 00153b9214eafa67020ca43e99d3b8685a33cd8c Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 9 Dec 2021 16:21:52 -0600 Subject: [PATCH] html: add cookie data attributes to tag. * Add `data-cookie-*` attributes to the tag * Add a `post-preview-show-votes` class to thumbnails. Ref: https://github.com/danbooru/danbooru/issues/4930#issuecomment-978064810 --- app/components/post_preview_component.rb | 1 + app/helpers/application_helper.rb | 46 +++++++++++++++++------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/app/components/post_preview_component.rb b/app/components/post_preview_component.rb index ccd63ac1a..ee2294406 100644 --- a/app/components/post_preview_component.rb +++ b/app/components/post_preview_component.rb @@ -80,6 +80,7 @@ class PostPreviewComponent < ApplicationComponent klass << "post-status-deleted" if post.is_deleted? klass << "post-status-has-parent" if post.parent_id klass << "post-status-has-children" if post.has_visible_children? + klass << "post-preview-show-votes" if show_votes klass << "post-preview-fit-#{fit}" klass << "post-preview-#{size}" klass diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f4261c06a..74d5da472 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -253,14 +253,6 @@ module ApplicationHelper end def body_attributes(user, params, current_item = nil) - current_user_data_attributes = data_attributes_for(user, "current-user", current_user_attributes) - - if !current_item.nil? && current_item.respond_to?(:html_data_attributes) && current_item.respond_to?(:model_name) - model_name = current_item.model_name.singular.dasherize - model_attributes = current_item.html_data_attributes - current_item_data_attributes = data_attributes_for(current_item, model_name, model_attributes) - end - controller_param = params[:controller].parameterize.dasherize action_param = params[:action].parameterize.dasherize @@ -273,20 +265,43 @@ module ApplicationHelper action: action_param, layout: controller.class.send(:_layout), "current-user-ip-addr": request.remote_ip, - **current_user_data_attributes, - **current_item_data_attributes.to_h, + **current_user_data_attributes(user), + **cookie_data_attributes, + **current_item_data_attributes(current_item), } } end - def current_user_attributes - %i[ + def current_user_data_attributes(user) + attributes = %i[ id name level level_string theme always_resize_images can_upload_free can_approve_posts disable_categorized_saved_searches disable_mobile_gestures disable_post_tooltips enable_safe_mode hide_deleted_posts show_deleted_children style_usernames default_image_size ] + User::Roles.map { |role| :"is_#{role}?" } + + data_attributes_for(user, "current-user", attributes) + end + + def cookie_data_attributes + attributes = %i[ + news-ticker hide_upgrade_account_notice hide_verify_account_notice + hide_dmail_notice dab show-relationship-previews post_preview_size + post_preview_show_votes + ] + + data_attributes_for(cookies, "cookie", attributes) + end + + def current_item_data_attributes(current_item) + if current_item.present? && current_item.respond_to?(:html_data_attributes) && current_item.respond_to?(:model_name) + model_name = current_item.model_name.singular.dasherize + model_attributes = current_item.html_data_attributes + data_attributes_for(current_item, model_name, model_attributes) + else + {} + end end def data_attributes_for(record, prefix, attributes) @@ -300,13 +315,18 @@ module ApplicationHelper break end end - else + elsif record.respond_to?(attr) name = attr.to_s.dasherize.delete("?") value = record.send(attr) + else + name = attr.to_s.dasherize.delete("?") + value = record[attr] end + if value.nil? value = "null" end + if prefix.blank? [:"#{name}", value] else