[deploy] Limit Suggestions for Social Service Posts (#7811)

This commit is contained in:
Molly Struve 2020-05-13 08:38:23 -05:00 committed by GitHub
parent f388fb46e9
commit 225b87390d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View file

@ -2,7 +2,7 @@ class BufferUpdate < ApplicationRecord
resourcify
belongs_to :article
validate :validate_body_text_recent_uniqueness
validate :validate_body_text_recent_uniqueness, :validate_suggestion_limit
validates :status, inclusion: { in: %w[pending sent_direct confirmed dismissed] }
def self.buff!(article_id, text, buffer_profile_id_code, social_service_name = "twitter", tag_id = nil, admin_id = nil)
@ -56,4 +56,10 @@ class BufferUpdate < ApplicationRecord
errors.add(:body_text, "\"#{body_text}\" has already been submitted very recently")
end
end
def validate_suggestion_limit
return unless BufferUpdate.where(article_id: article_id, tag_id: tag_id, social_service_name: social_service_name).count > 2
errors.add(:article_id, "already has multiple suggestions for #{social_service_name}")
end
end

View file

@ -42,4 +42,13 @@ RSpec.describe BufferUpdate, type: :model do
described_class.buff!(create(:article).id, "twitter_buffer_text", "CODE", "twitter", 1)
expect(described_class.all.size).to eq(2)
end
it "does not allow more than 3 suggestions" do
Array.new(3) do |i|
described_class.buff!(article.id, "twitter_buffer_text_#{i}", "CODE", "twitter")
end
invalid_buffer = described_class.buff!(article.id, "twitter_buffer_text_4", "CODE", "twitter")
expect(described_class.all.size).to eq(3)
expect(invalid_buffer.errors.full_messages.first).to include("already has multiple suggestions")
end
end