-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02e2d3c
commit 9d4a21e
Showing
8 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
class Follow < ApplicationRecord | ||
belongs_to :follower, class_name: "User", inverse_of: :follower_follows | ||
belongs_to :followed, class_name: "User", inverse_of: :followed_follows | ||
|
||
validates :follower, uniqueness: { scope: :followed_id } | ||
|
||
validate :follower_and_followed_cannot_be_the_same | ||
|
||
private | ||
|
||
def follower_and_followed_cannot_be_the_same | ||
return unless follower_id == followed_id | ||
|
||
errors.add(:base, :follower_and_followed_cannot_be_the_same) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateFollows < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :follows, id: :uuid do |t| | ||
t.references :follower, null: false, foreign_key: { to_table: :users }, type: :uuid | ||
t.references :followed, null: false, foreign_key: { to_table: :users }, type: :uuid | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :follows, %i[follower_id followed_id], unique: true | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :follow do | ||
association :follower, factory: :user, strategy: :create | ||
association :followed, factory: :user, strategy: :create | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Follow do | ||
describe "associations" do | ||
it { is_expected.to belong_to(:follower).class_name("User").inverse_of(:follower_follows) } | ||
it { is_expected.to belong_to(:followed).class_name("User").inverse_of(:followed_follows) } | ||
end | ||
|
||
describe "validations" do | ||
it "validates uniqueness of the pair follower_id and followed_id" do | ||
follow = create(:follow) | ||
|
||
expect(follow).to validate_uniqueness_of(:follower).scoped_to(:followed_id) | ||
end | ||
|
||
context "when follower and followed are the same" do | ||
it "adds an error message to :base" do | ||
user = create(:user) | ||
follow = described_class.new(follower: user, followed: user) | ||
follow.valid? | ||
|
||
error_message = I18n.t("activerecord.errors.models.follow.follower_and_followed_cannot_be_the_same") | ||
|
||
expect(follow.errors[:base]).to include(error_message) | ||
end | ||
end | ||
|
||
context "when follower and followed aren't the same" do | ||
it "doesn't add an error message to :base" do | ||
follow = create(:follow) | ||
|
||
error_message = I18n.t("activerecord.errors.models.follow.follower_and_followed_cannot_be_the_same") | ||
|
||
expect(follow.errors[:base]).not_to include(error_message) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters