Skip to content

Commit

Permalink
Merge pull request #135 from leouofa/article-links
Browse files Browse the repository at this point in the history
added specs for article links
  • Loading branch information
leouofa committed Jun 30, 2024
2 parents 995509a + 7c35d5a commit 329d81b
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 1 deletion.
12 changes: 12 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ class Article < ApplicationRecord

has_neighbors :embedding

has_many :article_links
has_many :linked_articles, through: :article_links, source: :linked_article, source_type: 'Article'

validates :name, :description, :original_text, presence: true
validate :linked_articles_count_within_limit

scope :without_embedding, -> { where(embedding: nil) }

private

def linked_articles_count_within_limit
return unless linked_articles.size > 3

errors.add(:linked_articles, "cannot exceed 3")
end
end
14 changes: 14 additions & 0 deletions app/models/article_link.rb
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
10 changes: 10 additions & 0 deletions db/migrate/20240630010230_create_article_links.rb
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
13 changes: 12 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions spec/factories/articles.rb
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
15 changes: 15 additions & 0 deletions spec/models/article_link_spec.rb
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
39 changes: 39 additions & 0 deletions spec/models/articles_spec.rb
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

0 comments on commit 329d81b

Please sign in to comment.