5
3
mirror of https://github.com/tildeverse/lobsters synced 2024-06-20 23:47:04 +00:00

restrict comment hats to user's own hats

This commit is contained in:
David Wolgemuth 2018-05-23 08:59:31 -05:00 committed by Peter Bhat Harkins
parent d6a889e5f2
commit 07a0a4fd88
3 changed files with 20 additions and 0 deletions

View File

@ -57,6 +57,9 @@ class Comment < ApplicationRecord
self.comment.to_s.strip.match(/\Ame too.?\z/i) &&
errors.add(:base, "Please just upvote the parent post instead.")
self.hat.present? && self.user.wearable_hats.exclude?(self.hat) &&
errors.add(:hat, "not wearable by user")
end
def self.arrange_for_user(user)

View File

@ -6,4 +6,20 @@ describe Comment do
expect(c.short_id).to match(/^\A[a-zA-Z0-9]{1,10}\z/)
end
describe "hat" do
it "can't be worn if user doesn't have that hat" do
comment = Comment.make(hat: Hat.make!)
comment.valid?
expect(comment.errors[:hat]).to eq(['not wearable by user'])
end
it "can be one of the user's hats" do
user = User.make!
hat = Hat.make!(user_id: user.id)
comment = Comment.make!(user_id: user.id, hat: hat)
comment.valid?
expect(comment.errors[:hat]).to be_empty
end
end
end

View File

@ -43,6 +43,7 @@ Comment.blueprint do
user_id { User.make!.id }
story_id { Story.make!.id }
comment { "comment text #{sn}" }
hat { nil }
end
Message.blueprint do