fix: Add slug to articles (#5500)

This commit is contained in:
Tejaswini Chile 2022-09-28 00:57:18 +05:30 committed by GitHub
parent 8e5d8fcdaf
commit c1c57fb2cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 14 deletions

View file

@ -42,7 +42,7 @@ class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController
def article_params def article_params
params.require(:article).permit( params.require(:article).permit(
:title, :content, :description, :position, :category_id, :author_id, :associated_article_id, :status, meta: [:title, :description, :title, :slug, :content, :description, :position, :category_id, :author_id, :associated_article_id, :status, meta: [:title, :description,
{ tags: [] }] { tags: [] }]
) )
end end

View file

@ -6,6 +6,7 @@
# content :text # content :text
# description :text # description :text
# meta :jsonb # meta :jsonb
# slug :string not null
# status :integer # status :integer
# title :string # title :string
# views :integer # views :integer
@ -22,6 +23,7 @@
# #
# index_articles_on_associated_article_id (associated_article_id) # index_articles_on_associated_article_id (associated_article_id)
# index_articles_on_author_id (author_id) # index_articles_on_author_id (author_id)
# index_articles_on_slug (slug)
# #
class Article < ApplicationRecord class Article < ApplicationRecord
include PgSearch::Model include PgSearch::Model

View file

@ -0,0 +1,21 @@
class AddSlugToArticle < ActiveRecord::Migration[6.1]
def up
add_column :articles, :slug, :string
update_past_articles_with_slug
add_index :articles, :slug
change_column_null(:articles, :slug, false)
end
def down
remove_column(:articles, :slug)
end
def update_past_articles_with_slug
Article.all.each_with_index do |article, index|
slug = article.title.underscore.parameterize(separator: '-')
article.update!(slug: "#{slug}-#{index}")
end
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_09_20_014549) do ActiveRecord::Schema.define(version: 2022_09_26_164441) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements" enable_extension "pg_stat_statements"
@ -131,8 +131,10 @@ ActiveRecord::Schema.define(version: 2022_09_20_014549) do
t.bigint "author_id" t.bigint "author_id"
t.bigint "associated_article_id" t.bigint "associated_article_id"
t.jsonb "meta", default: {} t.jsonb "meta", default: {}
t.string "slug", null: false
t.index ["associated_article_id"], name: "index_articles_on_associated_article_id" t.index ["associated_article_id"], name: "index_articles_on_associated_article_id"
t.index ["author_id"], name: "index_articles_on_author_id" t.index ["author_id"], name: "index_articles_on_author_id"
t.index ["slug"], name: "index_articles_on_slug"
end end
create_table "attachments", id: :serial, force: :cascade do |t| create_table "attachments", id: :serial, force: :cascade do |t|

View file

@ -24,6 +24,7 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do
category_id: category.id, category_id: category.id,
description: 'test description', description: 'test description',
title: 'MyTitle', title: 'MyTitle',
slug: 'my-title',
content: 'This is my content.', content: 'This is my content.',
status: :published, status: :published,
author_id: agent.id author_id: agent.id
@ -39,8 +40,9 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do
end end
it 'associate to the root article' do it 'associate to the root article' do
root_article = create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id, associated_article_id: nil) root_article = create(:article, category: category, slug: 'root-article', portal: portal, account_id: account.id, author_id: agent.id,
parent_article = create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id, associated_article_id: nil)
parent_article = create(:article, category: category, slug: 'parent-article', portal: portal, account_id: account.id, author_id: agent.id,
associated_article_id: root_article.id) associated_article_id: root_article.id)
article_params = { article_params = {
@ -48,6 +50,7 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do
category_id: category.id, category_id: category.id,
description: 'test description', description: 'test description',
title: 'MyTitle', title: 'MyTitle',
slug: 'MyTitle',
content: 'This is my content.', content: 'This is my content.',
status: :published, status: :published,
author_id: agent.id, author_id: agent.id,
@ -73,6 +76,7 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do
category_id: category.id, category_id: category.id,
description: 'test description', description: 'test description',
title: 'MyTitle', title: 'MyTitle',
slug: 'MyTitle',
content: 'This is my content.', content: 'This is my content.',
status: :published, status: :published,
author_id: agent.id, author_id: agent.id,
@ -210,9 +214,9 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do
it 'get associated articles' do it 'get associated articles' do
root_article = create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id, associated_article_id: nil) root_article = create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id, associated_article_id: nil)
child_article_1 = create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id, child_article_1 = create(:article, slug: 'child-1', category: category, portal: portal, account_id: account.id, author_id: agent.id,
associated_article_id: root_article.id) associated_article_id: root_article.id)
child_article_2 = create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id, child_article_2 = create(:article, slug: 'child-2', category: category, portal: portal, account_id: account.id, author_id: agent.id,
associated_article_id: root_article.id) associated_article_id: root_article.id)
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{root_article.id}", get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{root_article.id}",

View file

@ -4,6 +4,7 @@ FactoryBot.define do
category_id { 1 } category_id { 1 }
author_id { 1 } author_id { 1 }
title { 'MyString' } title { 'MyString' }
slug { 'MyString' }
content { 'MyText' } content { 'MyText' }
description { 'MyDescrption' } description { 'MyDescrption' }
status { 1 } status { 1 }

View file

@ -25,13 +25,15 @@ RSpec.describe Article, type: :model do
let!(:category_3) { create(:category, slug: 'category_3', locale: 'es', portal_id: portal_2.id) } let!(:category_3) { create(:category, slug: 'category_3', locale: 'es', portal_id: portal_2.id) }
before do before do
create(:article, category_id: category_1.id, content: 'This is the content', description: 'this is the description', title: 'this is title', create(:article, category_id: category_1.id, content: 'This is the content', description: 'this is the description',
slug: 'this-is-title', title: 'this is title',
portal_id: portal_1.id, author_id: user.id) portal_id: portal_1.id, author_id: user.id)
create(:article, category_id: category_1.id, title: 'title 1', content: 'This is the content', portal_id: portal_1.id, author_id: user.id) create(:article, category_id: category_1.id, slug: 'title-1', title: 'title 1', content: 'This is the content', portal_id: portal_1.id,
create(:article, category_id: category_2.id, title: 'title 2', portal_id: portal_2.id, author_id: user.id) author_id: user.id)
create(:article, category_id: category_2.id, title: 'title 3', portal_id: portal_1.id, author_id: user.id) create(:article, category_id: category_2.id, slug: 'title-2', title: 'title 2', portal_id: portal_2.id, author_id: user.id)
create(:article, category_id: category_3.id, title: 'title 6', portal_id: portal_2.id, author_id: user.id, status: :published) create(:article, category_id: category_2.id, slug: 'title-3', title: 'title 3', portal_id: portal_1.id, author_id: user.id)
create(:article, category_id: category_2.id, title: 'title 7', portal_id: portal_1.id, author_id: user.id, status: :published) create(:article, category_id: category_3.id, slug: 'title-6', title: 'title 6', portal_id: portal_2.id, author_id: user.id, status: :published)
create(:article, category_id: category_2.id, slug: 'title-7', title: 'title 7', portal_id: portal_1.id, author_id: user.id, status: :published)
end end
context 'when no parameters passed' do context 'when no parameters passed' do
@ -121,7 +123,7 @@ RSpec.describe Article, type: :model do
context 'with pagination' do context 'with pagination' do
it 'returns paginated articles' do it 'returns paginated articles' do
create_list(:article, 30, category_id: category_2.id, title: 'title 1', portal_id: portal_2.id, author_id: user.id) create_list(:article, 30, category_id: category_2.id, slug: 'title-1', title: 'title 1', portal_id: portal_2.id, author_id: user.id)
params = { category_slug: 'category_2' } params = { category_slug: 'category_2' }
records = portal_2.articles.search(params) records = portal_2.articles.search(params)
expect(records.count).to eq(25) expect(records.count).to eq(25)