parent
1819041f5a
commit
57fcb79d71
5 changed files with 29 additions and 6 deletions
|
@ -23,7 +23,7 @@
|
|||
#
|
||||
# index_articles_on_associated_article_id (associated_article_id)
|
||||
# index_articles_on_author_id (author_id)
|
||||
# index_articles_on_slug (slug)
|
||||
# index_articles_on_slug (slug) UNIQUE
|
||||
#
|
||||
class Article < ApplicationRecord
|
||||
include PgSearch::Model
|
||||
|
@ -45,6 +45,8 @@ class Article < ApplicationRecord
|
|||
belongs_to :author, class_name: 'User'
|
||||
|
||||
before_validation :ensure_account_id
|
||||
before_validation :ensure_article_slug
|
||||
|
||||
validates :account_id, presence: true
|
||||
validates :category_id, presence: true
|
||||
validates :author_id, presence: true
|
||||
|
@ -112,4 +114,8 @@ class Article < ApplicationRecord
|
|||
def ensure_account_id
|
||||
self.account_id = portal&.account_id
|
||||
end
|
||||
|
||||
def ensure_article_slug
|
||||
self.slug ||= "#{Time.now.utc.to_i}-#{title.underscore.parameterize(separator: '-')}" if title.present?
|
||||
end
|
||||
end
|
||||
|
|
6
db/migrate/20220930025317_add_unique_index_to_slug.rb
Normal file
6
db/migrate/20220930025317_add_unique_index_to_slug.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
class AddUniqueIndexToSlug < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
remove_index :articles, :slug
|
||||
add_index :articles, :slug, unique: true
|
||||
end
|
||||
end
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2022_09_26_164441) do
|
||||
ActiveRecord::Schema.define(version: 2022_09_30_025317) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "pg_stat_statements"
|
||||
|
@ -134,7 +134,7 @@ ActiveRecord::Schema.define(version: 2022_09_26_164441) do
|
|||
t.string "slug", null: false
|
||||
t.index ["associated_article_id"], name: "index_articles_on_associated_article_id"
|
||||
t.index ["author_id"], name: "index_articles_on_author_id"
|
||||
t.index ["slug"], name: "index_articles_on_slug"
|
||||
t.index ["slug"], name: "index_articles_on_slug", unique: true
|
||||
end
|
||||
|
||||
create_table "attachments", id: :serial, force: :cascade do |t|
|
||||
|
|
|
@ -3,8 +3,7 @@ FactoryBot.define do
|
|||
account_id { 1 }
|
||||
category_id { 1 }
|
||||
author_id { 1 }
|
||||
title { 'MyString' }
|
||||
slug { 'MyString' }
|
||||
title { Faker::Movie.title }
|
||||
content { 'MyText' }
|
||||
description { 'MyDescrption' }
|
||||
status { 1 }
|
||||
|
|
|
@ -119,11 +119,23 @@ RSpec.describe Article, type: :model do
|
|||
records = portal_1.articles.search(params)
|
||||
expect(records.count).to eq(2)
|
||||
end
|
||||
|
||||
it 'auto saves article slug' do
|
||||
article = create(:article, category_id: category_1.id, title: 'the awesome article 1', content: 'This is the content', portal_id: portal_1.id,
|
||||
author_id: user.id)
|
||||
expect(article.slug).to include('the-awesome-article-1')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with pagination' do
|
||||
it 'returns paginated articles' do
|
||||
create_list(:article, 30, category_id: category_2.id, slug: 'title-1', title: 'title 1', portal_id: portal_2.id, author_id: user.id)
|
||||
build_list(:article, 30) do |record, i|
|
||||
record.category_id = category_2.id
|
||||
record.title = "title #{i}"
|
||||
record.portal_id = portal_2.id
|
||||
record.author_id = user.id
|
||||
record.save!
|
||||
end
|
||||
params = { category_slug: 'category_2' }
|
||||
records = portal_2.articles.search(params)
|
||||
expect(records.count).to eq(25)
|
||||
|
|
Loading…
Reference in a new issue