From 57fcb79d71a88dc8bd97794ec773ee923a1bb637 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Fri, 30 Sep 2022 19:55:23 +0530 Subject: [PATCH] fix: Article slug auto saves (#5524) - Auto save article slug --- app/models/article.rb | 8 +++++++- .../20220930025317_add_unique_index_to_slug.rb | 6 ++++++ db/schema.rb | 4 ++-- spec/factories/articles.rb | 3 +-- spec/models/article_spec.rb | 14 +++++++++++++- 5 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20220930025317_add_unique_index_to_slug.rb diff --git a/app/models/article.rb b/app/models/article.rb index f7318c497..ac6506d01 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -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 diff --git a/db/migrate/20220930025317_add_unique_index_to_slug.rb b/db/migrate/20220930025317_add_unique_index_to_slug.rb new file mode 100644 index 000000000..e35f6289c --- /dev/null +++ b/db/migrate/20220930025317_add_unique_index_to_slug.rb @@ -0,0 +1,6 @@ +class AddUniqueIndexToSlug < ActiveRecord::Migration[6.1] + def change + remove_index :articles, :slug + add_index :articles, :slug, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index ad5c8241a..0793a6587 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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| diff --git a/spec/factories/articles.rb b/spec/factories/articles.rb index f820ddd6c..30c842621 100644 --- a/spec/factories/articles.rb +++ b/spec/factories/articles.rb @@ -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 } diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index 3fffa63d6..2e0f957dc 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -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)