Refactor code getting a user's comments

This way it's more readable and easier to change.
This commit is contained in:
Javi Martín
2021-09-04 22:56:54 +02:00
parent 3cd4f3827e
commit af53b2159c

View File

@@ -43,7 +43,11 @@ class Users::PublicActivityComponent < ApplicationComponent
end end
def comments def comments
only_active_commentables.includes(:commentable) Comment.not_valuations
.not_as_admin_or_moderator
.where(user_id: user.id)
.where.not(commentable_type: disabled_commentables)
.includes(:commentable)
end end
def budget_investments def budget_investments
@@ -66,18 +70,10 @@ class Users::PublicActivityComponent < ApplicationComponent
params[:page] params[:page]
end end
def only_active_commentables def disabled_commentables
disabled_commentables = [] [
disabled_commentables << "Debate" unless Setting["process.debates"] ("Debate" unless feature?(:debates)),
disabled_commentables << "Budget::Investment" unless Setting["process.budgets"] ("Budget::Investment" unless feature?(:budgets))
if disabled_commentables.present? ].compact
all_user_comments.where.not(commentable_type: disabled_commentables)
else
all_user_comments
end
end
def all_user_comments
Comment.not_valuations.not_as_admin_or_moderator.where(user_id: user.id)
end end
end end