Refactor flag types for more customization

This commit is contained in:
Robin Ward
2017-10-17 13:31:45 -04:00
parent e600fb79b3
commit 838568cbc3
26 changed files with 253 additions and 82 deletions

View File

@@ -15,7 +15,7 @@ class FlaggedTopicSummarySerializer < ActiveModel::Serializer
def flag_counts
object.flag_counts.map do |k, v|
{ flag_type_id: k, count: v }
{ post_action_type_id: k, count: v, name_key: PostActionType.types[k] }
end
end

View File

@@ -2,13 +2,25 @@ require_dependency 'configurable_urls'
class PostActionTypeSerializer < ApplicationSerializer
attributes :name_key, :name, :description, :short_description, :long_form, :is_flag, :icon, :id, :is_custom_flag
attributes(
:id,
:name_key,
:name,
:description,
:short_description,
:long_form,
:is_flag,
:is_custom_flag
)
include ConfigurableUrls
def is_custom_flag
object.id == PostActionType.types[:notify_user] ||
object.id == PostActionType.types[:notify_moderators]
!!PostActionType.custom_types[object.id]
end
def is_flag
!!PostActionType.flag_types[object.id]
end
def name
@@ -27,10 +39,14 @@ class PostActionTypeSerializer < ApplicationSerializer
i18n('short_description', tos_url: tos_path)
end
def name_key
PostActionType.types[object.id]
end
protected
def i18n(field, vars = nil)
key = "post_action_types.#{object.name_key}.#{field}"
key = "post_action_types.#{name_key}.#{field}"
vars ? I18n.t(key, vars) : I18n.t(key)
end

View File

@@ -246,7 +246,7 @@ class PostSerializer < BasicPostSerializer
# The following only applies if you're logged in
if summary[:can_act] && scope.current_user.present?
summary[:can_defer_flags] = true if scope.is_staff? &&
PostActionType.flag_types.values.include?(id) &&
PostActionType.flag_types_without_custom.values.include?(id) &&
active_flags.present? && active_flags.has_key?(id) &&
active_flags[id].count > 0
end

View File

@@ -52,13 +52,14 @@ class SiteSerializer < ApplicationSerializer
def post_action_types
cache_fragment("post_action_types_#{I18n.locale}") do
ActiveModel::ArraySerializer.new(PostActionType.ordered).as_json
types = PostActionType.types.values.map { |id| PostActionType.new(id: id) }
ActiveModel::ArraySerializer.new(types).as_json
end
end
def topic_flag_types
cache_fragment("post_action_flag_types_#{I18n.locale}") do
flags = PostActionType.ordered.where(name_key: ['inappropriate', 'spam', 'notify_moderators'])
flags = PostActionType.ordered.where(id: PostActionType.topic_flag_types.values)
ActiveModel::ArraySerializer.new(flags, each_serializer: TopicFlagTypeSerializer).as_json
end