html: add cookie data attributes to <body> tag.

* Add `data-cookie-*` attributes to the <body> tag
* Add a `post-preview-show-votes` class to thumbnails.

Ref: https://github.com/danbooru/danbooru/issues/4930#issuecomment-978064810
This commit is contained in:
evazion
2021-12-09 16:21:52 -06:00
parent 7976d12cd0
commit 00153b9214
2 changed files with 34 additions and 13 deletions

View File

@@ -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

View File

@@ -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