-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from leouofa/article-links
added specs for article links
- Loading branch information
Showing
7 changed files
with
115 additions
and
1 deletion.
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
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 @@ | ||
class ArticleLink < ApplicationRecord | ||
belongs_to :article | ||
belongs_to :linked_article, polymorphic: true | ||
|
||
validate :article_cannot_link_to_itself | ||
|
||
private | ||
|
||
def article_cannot_link_to_itself | ||
return unless article_id == linked_article_id | ||
|
||
errors.add(:linked_article, "can't be the same as the article") | ||
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,10 @@ | ||
class CreateArticleLinks < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :article_links do |t| | ||
t.references :article, null: false, foreign_key: true | ||
t.references :linked_article, polymorphic: true, null: false | ||
|
||
t.timestamps | ||
end | ||
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,13 @@ | ||
FactoryBot.define do | ||
factory :article do | ||
association :pillar_column | ||
sequence(:name) { |n| "Sample Article #{n}" } | ||
description { "This is a sample article." } | ||
original_text { "Some original text." } | ||
end | ||
|
||
factory :article_link do | ||
association :article | ||
association :linked_article, factory: :article | ||
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,15 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe ArticleLink, type: :model do | ||
it "is valid with valid attributes" do | ||
article_link = create(:article_link) | ||
expect(article_link).to be_valid | ||
end | ||
|
||
it "is invalid if an article links to itself" do | ||
article = create(:article) | ||
article_link = build(:article_link, article: article, linked_article: article) | ||
expect(article_link).not_to be_valid | ||
expect(article_link.errors[:linked_article]).to include("can't be the same as the article") | ||
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,39 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Article, type: :model do | ||
it "is valid with valid attributes" do | ||
article = create(:article) | ||
expect(article).to be_valid | ||
end | ||
|
||
it "is invalid without a name" do | ||
article = build(:article, name: nil) | ||
expect(article).not_to be_valid | ||
end | ||
|
||
it "is invalid without a description" do | ||
article = build(:article, description: nil) | ||
expect(article).not_to be_valid | ||
end | ||
|
||
it "is invalid without original text" do | ||
article = build(:article, original_text: nil) | ||
expect(article).not_to be_valid | ||
end | ||
|
||
it "cannot have more than 3 linked articles" do | ||
article = create(:article) | ||
3.times { article.linked_articles << create(:article) } | ||
expect(article).to be_valid | ||
|
||
article.linked_articles << create(:article) | ||
expect(article).not_to be_valid | ||
end | ||
|
||
it "cannot link to itself" do | ||
article = create(:article) | ||
article_link = build(:article_link, article: article, linked_article: article) | ||
expect(article_link).not_to be_valid | ||
expect(article_link.errors[:linked_article]).to include("can't be the same as the article") | ||
end | ||
end |