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:
@@ -59,12 +59,10 @@ class ApplicationRecord < ActiveRecord::Base
|
|||||||
policy.api_attributes
|
policy.api_attributes
|
||||||
end
|
end
|
||||||
|
|
||||||
def html_data_attributes
|
# XXX deprecated, shouldn't expose this as an instance method.
|
||||||
data_attributes = self.class.columns.select do |column|
|
def html_data_attributes(user: CurrentUser.user)
|
||||||
column.type.in?([:integer, :boolean]) && !column.array?
|
policy = Pundit.policy([user, nil], self) || ApplicationPolicy.new([user, nil], self)
|
||||||
end.map(&:name).map(&:to_sym)
|
policy.html_data_attributes
|
||||||
|
|
||||||
api_attributes & data_attributes
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_hash(options = {})
|
def serializable_hash(options = {})
|
||||||
|
|||||||
@@ -38,14 +38,6 @@ class Ban < ApplicationRecord
|
|||||||
q
|
q
|
||||||
end
|
end
|
||||||
|
|
||||||
module ApiMethods
|
|
||||||
def html_data_attributes
|
|
||||||
super + [:expired?]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
include ApiMethods
|
|
||||||
|
|
||||||
def self.prune!
|
def self.prune!
|
||||||
expired.includes(:user).find_each do |ban|
|
expired.includes(:user).find_each do |ban|
|
||||||
ban.user.unban! if ban.user.ban_expired?
|
ban.user.unban! if ban.user.ban_expired?
|
||||||
|
|||||||
@@ -49,14 +49,7 @@ class ForumPost < ApplicationRecord
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module ApiMethods
|
|
||||||
def html_data_attributes
|
|
||||||
super + [[:topic, :is_deleted?]]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
extend SearchMethods
|
extend SearchMethods
|
||||||
include ApiMethods
|
|
||||||
|
|
||||||
def self.new_reply(params)
|
def self.new_reply(params)
|
||||||
if params[:topic_id]
|
if params[:topic_id]
|
||||||
|
|||||||
@@ -190,10 +190,6 @@ class ForumTopic < ApplicationRecord
|
|||||||
title.gsub(/\A\[APPROVED\]|\[REJECTED\]/, "")
|
title.gsub(/\A\[APPROVED\]|\[REJECTED\]/, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
def html_data_attributes
|
|
||||||
super + [:is_read?]
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.searchable_includes
|
def self.searchable_includes
|
||||||
[:creator, :updater, :forum_posts, :bulk_update_requests, :tag_aliases, :tag_implications]
|
[:creator, :updater, :forum_posts, :bulk_update_requests, :tag_aliases, :tag_implications]
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -50,10 +50,6 @@ class IpAddress < ApplicationRecord
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def html_data_attributes
|
|
||||||
super & attributes.keys.map(&:to_sym)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.searchable_includes
|
def self.searchable_includes
|
||||||
[:user, :model]
|
[:user, :model]
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1504,10 +1504,6 @@ class Post < ApplicationRecord
|
|||||||
save
|
save
|
||||||
end
|
end
|
||||||
|
|
||||||
def html_data_attributes
|
|
||||||
super + [:has_large?, :current_image_size]
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.model_restriction(table)
|
def self.model_restriction(table)
|
||||||
super.where(table[:is_pending].eq(false)).where(table[:is_flagged].eq(false)).where(table[:is_deleted].eq(false))
|
super.where(table[:is_pending].eq(false)).where(table[:is_flagged].eq(false)).where(table[:is_deleted].eq(false))
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -39,14 +39,7 @@ class UserFeedback < ApplicationRecord
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module ApiMethods
|
|
||||||
def html_data_attributes
|
|
||||||
super + [:category]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
extend SearchMethods
|
extend SearchMethods
|
||||||
include ApiMethods
|
|
||||||
|
|
||||||
def user_name=(name)
|
def user_name=(name)
|
||||||
self.user = User.find_by_name(name)
|
self.user = User.find_by_name(name)
|
||||||
|
|||||||
@@ -105,14 +105,7 @@ class WikiPage < ApplicationRecord
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module ApiMethods
|
|
||||||
def html_data_attributes
|
|
||||||
super + [:category_name]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
extend SearchMethods
|
extend SearchMethods
|
||||||
include ApiMethods
|
|
||||||
|
|
||||||
def validate_rename
|
def validate_rename
|
||||||
return unless title_changed?
|
return unless title_changed?
|
||||||
|
|||||||
@@ -70,7 +70,18 @@ class ApplicationPolicy
|
|||||||
permitted_attributes_for_update
|
permitted_attributes_for_update
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# The list of attributes that are permitted to be returned by the API.
|
||||||
def api_attributes
|
def api_attributes
|
||||||
record.class.attribute_types.reject { |name, attr| attr.type.in?([:inet, :tsvector]) }.keys.map(&:to_sym)
|
record.class.attribute_types.reject { |name, attr| attr.type.in?([:inet, :tsvector]) }.keys.map(&:to_sym)
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -15,4 +15,8 @@ class BanPolicy < ApplicationPolicy
|
|||||||
def permitted_attributes_for_update
|
def permitted_attributes_for_update
|
||||||
[:reason, :duration, :expires_at]
|
[:reason, :duration, :expires_at]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def html_data_attributes
|
||||||
|
super + [:expired?]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -42,4 +42,8 @@ class ForumPostPolicy < ApplicationPolicy
|
|||||||
def permitted_attributes_for_update
|
def permitted_attributes_for_update
|
||||||
[:body]
|
[:body]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def html_data_attributes
|
||||||
|
super + [[:topic, :is_deleted?]]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -37,4 +37,8 @@ class ForumTopicPolicy < ApplicationPolicy
|
|||||||
([:is_sticky, :is_locked, :min_level] if moderate?)
|
([:is_sticky, :is_locked, :min_level] if moderate?)
|
||||||
].compact.flatten
|
].compact.flatten
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def html_data_attributes
|
||||||
|
super + [:is_read?]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,4 +2,8 @@ class IpAddressPolicy < ApplicationPolicy
|
|||||||
def index?
|
def index?
|
||||||
user.is_moderator?
|
user.is_moderator?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def html_data_attributes
|
||||||
|
super & attributes.keys.map(&:to_sym)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -95,4 +95,8 @@ class PostPolicy < ApplicationPolicy
|
|||||||
attributes -= [:fav_string] if !user.is_moderator?
|
attributes -= [:fav_string] if !user.is_moderator?
|
||||||
attributes
|
attributes
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def html_data_attributes
|
||||||
|
super + [:has_large?, :current_image_size]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -22,4 +22,8 @@ class UserFeedbackPolicy < ApplicationPolicy
|
|||||||
def permitted_attributes_for_update
|
def permitted_attributes_for_update
|
||||||
[:body, :category, :is_deleted]
|
[:body, :category, :is_deleted]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def html_data_attributes
|
||||||
|
super + [:category]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -18,4 +18,8 @@ class WikiPagePolicy < ApplicationPolicy
|
|||||||
def api_attributes
|
def api_attributes
|
||||||
super + [:category_name]
|
super + [:category_name]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def html_data_attributes
|
||||||
|
super + [:category_name]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user