add user cant follow himself validation
This commit is contained in:
@@ -1,4 +1,12 @@
|
||||
class UserFollow < ApplicationRecord
|
||||
belongs_to :follower, class_name: 'User'
|
||||
belongs_to :followed, class_name: 'User'
|
||||
|
||||
validate :followed, :user_cant_follow_himself
|
||||
|
||||
private
|
||||
|
||||
def user_cant_follow_himself
|
||||
errors.add(:followed, 'can\'t follow himself') if follower_id == followed_id
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
FactoryBot.define do
|
||||
factory :user do
|
||||
username { Faker::Internet.username.gsub(/[^0-9a-z ]/i, '') }
|
||||
username {
|
||||
Faker::Internet.username[0..14]
|
||||
.gsub(/[^0-9a-z ]/i, '')
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
14
spec/models/user_follow_spec.rb
Normal file
14
spec/models/user_follow_spec.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe UserFollow, type: :model do
|
||||
describe 'validations' do
|
||||
context 'and user try to follow her-self' do
|
||||
it 'raise error' do
|
||||
user = create(:user)
|
||||
user_follow = build(:user_follow, follower_id: user.id, followed_id: user.id)
|
||||
|
||||
expect(user_follow).to_not be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user