mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Improve add_to_serializer include_* options (#21073)
- Move the old '`define_include_method`' arg to a `respect_plugin_enabled` kwarg - Introduce an `include_condition` kwarg which can be passed a lambda with inclusion logic. Lambda will be run via `instance_exec` in the context of the serializer instance This is backwards compatible - old-style invocations will trigger a deprecation message Update chat and poll plugins to new pattern
This commit is contained in:
@@ -195,7 +195,7 @@ after_initialize do
|
||||
end
|
||||
end
|
||||
|
||||
add_to_serializer(:post, :preloaded_polls, false) do
|
||||
add_to_class(PostSerializer, :preloaded_polls) do
|
||||
@preloaded_polls ||=
|
||||
if @topic_view.present?
|
||||
@topic_view.polls[object.id]
|
||||
@@ -204,15 +204,18 @@ after_initialize do
|
||||
end
|
||||
end
|
||||
|
||||
add_to_serializer(:post, :include_preloaded_polls?) { false }
|
||||
|
||||
add_to_serializer(:post, :polls, false) do
|
||||
add_to_serializer(:post, :polls, include_condition: -> { preloaded_polls.present? }) do
|
||||
preloaded_polls.map { |p| PollSerializer.new(p, root: false, scope: self.scope) }
|
||||
end
|
||||
|
||||
add_to_serializer(:post, :include_polls?) { SiteSetting.poll_enabled && preloaded_polls.present? }
|
||||
|
||||
add_to_serializer(:post, :polls_votes, false) do
|
||||
add_to_serializer(
|
||||
:post,
|
||||
:polls_votes,
|
||||
include_condition: -> do
|
||||
scope.user&.id.present? && preloaded_polls.present? &&
|
||||
preloaded_polls.any? { |p| p.has_voted?(scope.user) }
|
||||
end,
|
||||
) do
|
||||
preloaded_polls
|
||||
.map do |poll|
|
||||
user_poll_votes =
|
||||
@@ -227,11 +230,6 @@ after_initialize do
|
||||
.to_h
|
||||
end
|
||||
|
||||
add_to_serializer(:post, :include_polls_votes?) do
|
||||
SiteSetting.poll_enabled && scope.user&.id.present? && preloaded_polls.present? &&
|
||||
preloaded_polls.any? { |p| p.has_voted?(scope.user) }
|
||||
end
|
||||
|
||||
register_search_advanced_filter(/in:polls/) do |posts, match|
|
||||
if SiteSetting.poll_enabled
|
||||
posts.joins(:polls)
|
||||
|
||||
Reference in New Issue
Block a user