models: move html_data_attributes to policies.

Move html_data_attributes definitions from models to policies. Which
attributes are permitted as data-* attributes is a view level concern
and should be defined on the policy level, not the model level. Models
should be agnostic about how they're used in views.
This commit is contained in:
evazion
2020-08-17 17:22:41 -05:00
parent 4b18361aaf
commit 937653e519
16 changed files with 43 additions and 47 deletions

View File

@@ -70,7 +70,18 @@ class ApplicationPolicy
permitted_attributes_for_update
end
# The list of attributes that are permitted to be returned by the API.
def api_attributes
record.class.attribute_types.reject { |name, attr| attr.type.in?([:inet, :tsvector]) }.keys.map(&:to_sym)
end
# The list of attributes that are permitted to be used as data-* attributes
# in tables and in the <body> tag on show pages.
def html_data_attributes
data_attributes = record.class.columns.select do |column|
column.type.in?([:integer, :boolean]) && !column.array?
end.map(&:name).map(&:to_sym)
api_attributes & data_attributes
end
end

View File

@@ -15,4 +15,8 @@ class BanPolicy < ApplicationPolicy
def permitted_attributes_for_update
[:reason, :duration, :expires_at]
end
def html_data_attributes
super + [:expired?]
end
end

View File

@@ -42,4 +42,8 @@ class ForumPostPolicy < ApplicationPolicy
def permitted_attributes_for_update
[:body]
end
def html_data_attributes
super + [[:topic, :is_deleted?]]
end
end

View File

@@ -37,4 +37,8 @@ class ForumTopicPolicy < ApplicationPolicy
([:is_sticky, :is_locked, :min_level] if moderate?)
].compact.flatten
end
def html_data_attributes
super + [:is_read?]
end
end

View File

@@ -2,4 +2,8 @@ class IpAddressPolicy < ApplicationPolicy
def index?
user.is_moderator?
end
def html_data_attributes
super & attributes.keys.map(&:to_sym)
end
end

View File

@@ -95,4 +95,8 @@ class PostPolicy < ApplicationPolicy
attributes -= [:fav_string] if !user.is_moderator?
attributes
end
def html_data_attributes
super + [:has_large?, :current_image_size]
end
end

View File

@@ -22,4 +22,8 @@ class UserFeedbackPolicy < ApplicationPolicy
def permitted_attributes_for_update
[:body, :category, :is_deleted]
end
def html_data_attributes
super + [:category]
end
end

View File

@@ -18,4 +18,8 @@ class WikiPagePolicy < ApplicationPolicy
def api_attributes
super + [:category_name]
end
def html_data_attributes
super + [:category_name]
end
end