Simplify methods to get API-public records

Just like we did to tags in commit 2e863fdc5.
This commit is contained in:
Javi Martín
2022-04-05 22:48:42 +02:00
parent 0a3c86b92e
commit 5977f2ec3d
3 changed files with 3 additions and 13 deletions

View File

@@ -37,12 +37,7 @@ class Comment < ApplicationRecord
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) } scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
scope :public_for_api, -> do scope :public_for_api, -> do
not_valuations not_valuations
.where(%{(comments.commentable_type = 'Debate' and comments.commentable_id in (?)) or .where(commentable: [Debate.public_for_api, Proposal.public_for_api, Poll.public_for_api])
(comments.commentable_type = 'Proposal' and comments.commentable_id in (?)) or
(comments.commentable_type = 'Poll' and comments.commentable_id in (?))},
Debate.public_for_api.pluck(:id),
Proposal.public_for_api.pluck(:id),
Poll.public_for_api.pluck(:id))
end end
scope :sort_by_most_voted, -> { order(confidence_score: :desc, created_at: :desc) } scope :sort_by_most_voted, -> { order(confidence_score: :desc, created_at: :desc) }

View File

@@ -10,7 +10,7 @@ class ProposalNotification < ApplicationRecord
validates :proposal, presence: true validates :proposal, presence: true
validate :minimum_interval validate :minimum_interval
scope :public_for_api, -> { where(proposal_id: Proposal.public_for_api.pluck(:id)) } scope :public_for_api, -> { where(proposal: Proposal.public_for_api) }
scope :sort_by_created_at, -> { reorder(created_at: :desc) } scope :sort_by_created_at, -> { reorder(created_at: :desc) }
scope :sort_by_moderated, -> { reorder(moderated: :desc) } scope :sort_by_moderated, -> { reorder(moderated: :desc) }

View File

@@ -5,12 +5,7 @@ ActsAsVotable::Vote.class_eval do
belongs_to :budget_investment, foreign_key: "votable_id", class_name: "Budget::Investment" belongs_to :budget_investment, foreign_key: "votable_id", class_name: "Budget::Investment"
scope :public_for_api, -> do scope :public_for_api, -> do
where(%{(votes.votable_type = 'Debate' and votes.votable_id in (?)) or where(votable: [Debate.public_for_api, Proposal.public_for_api, Comment.public_for_api])
(votes.votable_type = 'Proposal' and votes.votable_id in (?)) or
(votes.votable_type = 'Comment' and votes.votable_id in (?))},
Debate.public_for_api.pluck(:id),
Proposal.public_for_api.pluck(:id),
Comment.public_for_api.pluck(:id))
end end
def value def value