mirror of
https://github.com/lingble/chatwoot.git
synced 2025-10-29 18:22:53 +00:00
feat: Backend changes for article and categories ordering (#6655)
This commit is contained in:
@@ -55,9 +55,9 @@ class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController
|
||||
|
||||
def article_params
|
||||
params.require(:article).permit(
|
||||
:title, :slug, :content, :description, :position, :category_id, :author_id, :associated_article_id, :status, meta: [:title,
|
||||
:description,
|
||||
{ tags: [] }]
|
||||
:title, :slug, :position, :content, :description, :position, :category_id, :author_id, :associated_article_id, :status, meta: [:title,
|
||||
:description,
|
||||
{ tags: [] }]
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ class Public::Api::V1::Portals::ArticlesController < Public::Api::V1::Portals::B
|
||||
def index
|
||||
@articles = @portal.articles
|
||||
@articles = @articles.search(list_params) if list_params.present?
|
||||
@articles.order(position: :asc)
|
||||
end
|
||||
|
||||
def show; end
|
||||
|
||||
@@ -5,7 +5,7 @@ class Public::Api::V1::Portals::CategoriesController < Public::Api::V1::Portals:
|
||||
layout 'portal'
|
||||
|
||||
def index
|
||||
@categories = @portal.categories
|
||||
@categories = @portal.categories.order(position: :asc)
|
||||
end
|
||||
|
||||
def show; end
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
# content :text
|
||||
# description :text
|
||||
# meta :jsonb
|
||||
# position :integer
|
||||
# slug :string not null
|
||||
# status :integer
|
||||
# title :string
|
||||
|
||||
@@ -3,6 +3,7 @@ json.title article.title
|
||||
json.content article.content
|
||||
json.description article.description
|
||||
json.status article.status
|
||||
json.position article.position
|
||||
json.account_id article.account_id
|
||||
json.updated_at article.updated_at.to_i
|
||||
json.meta article.meta
|
||||
|
||||
@@ -4,6 +4,7 @@ json.title article.title
|
||||
json.content article.content
|
||||
json.description article.description
|
||||
json.status article.status
|
||||
json.position article.position
|
||||
json.account_id article.account_id
|
||||
json.last_updated_at article.updated_at
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
</a>
|
||||
</h3>
|
||||
<span class="text-slate-500">
|
||||
<%= render 'public/api/v1/portals/article_count', article_count: category.articles.published.size %>
|
||||
<%= render 'public/api/v1/portals/article_count', article_count: category.articles.published.order(position: :asc).size %>
|
||||
</span>
|
||||
</div>
|
||||
<div class="py-4 w-full mt-2 flex-grow">
|
||||
@@ -15,7 +15,7 @@
|
||||
<p class="text-sm text-slate-500"><%= I18n.t('public_portal.common.no_articles') %></p>
|
||||
</div>
|
||||
<% else %>
|
||||
<% category.articles.published.take(5).each do |article| %>
|
||||
<% category.articles.published.order(position: :asc).take(5).each do |article| %>
|
||||
<a
|
||||
class="text-slate-800 hover:underline leading-8"
|
||||
href="/hc/<%= portal.slug %>/<%= category.locale %>/<%= category.slug %>/<%= article.id %>"
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
</a>
|
||||
</h3>
|
||||
<span class="text-slate-500">
|
||||
<%= render 'public/api/v1/portals/article_count', article_count: category.articles.published.size %>
|
||||
<%= render 'public/api/v1/portals/article_count', article_count: category.articles.published.order(position: :asc).size %>
|
||||
</span>
|
||||
</div>
|
||||
<div class="py-4 w-full mt-2 flex-grow">
|
||||
@@ -16,7 +16,7 @@
|
||||
<p class="text-sm text-slate-500"><%= I18n.t('public_portal.common.no_articles') %></p>
|
||||
</div>
|
||||
<% else %>
|
||||
<% category.articles.published.take(5).each do |article| %>
|
||||
<% category.articles.published.order(position: :asc).take(5).each do |article| %>
|
||||
<div class="flex justify-between content-center h-8 my-1">
|
||||
<a
|
||||
class="text-slate-800 hover:underline leading-8"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<%= render "public/api/v1/portals/hero", portal: @portal %>
|
||||
<div class="max-w-5xl w-full flex-grow mx-auto py-8 px-4">
|
||||
<div class="grid grid-cols-1 lg:grid-cols-2 gap-x-32 gap-y-0 lg:gap-y-12">
|
||||
<% @portal.categories.where(locale: @locale).joins(:articles).where(articles:{ status: :published }).group('categories.id').each do |category| %>
|
||||
<% @portal.categories.where(locale: @locale).joins(:articles).where(articles:{ status: :published }).order(position: :asc).group('categories.id').each do |category| %>
|
||||
<%= render "public/api/v1/portals/category-block", category: category, portal: @portal %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
5
db/migrate/20230313113920_add_position_to_articles.rb
Normal file
5
db/migrate/20230313113920_add_position_to_articles.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class AddPositionToArticles < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
add_column :articles, :position, :integer
|
||||
end
|
||||
end
|
||||
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2023_03_06_100327) do
|
||||
ActiveRecord::Schema.define(version: 2023_03_13_113920) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "pg_stat_statements"
|
||||
@@ -133,6 +133,7 @@ ActiveRecord::Schema.define(version: 2023_03_06_100327) do
|
||||
t.bigint "associated_article_id"
|
||||
t.jsonb "meta", default: {}
|
||||
t.string "slug", null: false
|
||||
t.integer "position"
|
||||
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", unique: true
|
||||
|
||||
@@ -27,7 +27,8 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do
|
||||
slug: 'my-title',
|
||||
content: 'This is my content.',
|
||||
status: :published,
|
||||
author_id: agent.id
|
||||
author_id: agent.id,
|
||||
position: 3
|
||||
}
|
||||
}
|
||||
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles",
|
||||
@@ -37,6 +38,7 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['payload']['title']).to eql('MyTitle')
|
||||
expect(json_response['payload']['status']).to eql('draft')
|
||||
expect(json_response['payload']['position']).to be(3)
|
||||
end
|
||||
|
||||
it 'associate to the root article' do
|
||||
@@ -110,7 +112,8 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do
|
||||
article: {
|
||||
title: 'MyTitle2',
|
||||
status: 'published',
|
||||
description: 'test_description'
|
||||
description: 'test_description',
|
||||
position: 5
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,6 +126,7 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['payload']['title']).to eql(article_params[:article][:title])
|
||||
expect(json_response['payload']['status']).to eql(article_params[:article][:status])
|
||||
expect(json_response['payload']['position']).to eql(article_params[:article][:position])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,12 +4,16 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||
let(:account) { create(:account) }
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
let!(:portal) { create(:portal, name: 'test_portal', account_id: account.id, config: { allowed_locales: %w[en es] }) }
|
||||
let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id, slug: 'category_slug') }
|
||||
let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id, slug: 'category_slug', position: 1) }
|
||||
let!(:category_to_associate) do
|
||||
create(:category, name: 'associated category', portal: portal, account_id: account.id, slug: 'associated_category_slug')
|
||||
create(:category, name: 'associated category', portal: portal, account_id: account.id, slug: 'associated_category_slug', position: 2)
|
||||
end
|
||||
let!(:related_category_1) do
|
||||
create(:category, name: 'related category 1', portal: portal, account_id: account.id, slug: 'category_slug_1', position: 3)
|
||||
end
|
||||
let!(:related_category_2) do
|
||||
create(:category, name: 'related category 2', portal: portal, account_id: account.id, slug: 'category_slug_2', position: 4)
|
||||
end
|
||||
let!(:related_category_1) { create(:category, name: 'related category 1', portal: portal, account_id: account.id, slug: 'category_slug_1') }
|
||||
let!(:related_category_2) { create(:category, name: 'related category 2', portal: portal, account_id: account.id, slug: 'category_slug_2') }
|
||||
|
||||
before { create(:portal_member, user: agent, portal: portal) }
|
||||
|
||||
@@ -27,7 +31,7 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||
category: {
|
||||
name: 'test_category',
|
||||
description: 'test_description',
|
||||
position: 1,
|
||||
position: 5,
|
||||
locale: 'es',
|
||||
slug: 'test_category_1',
|
||||
parent_category_id: category.id,
|
||||
@@ -42,7 +46,7 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||
category: {
|
||||
name: 'test_category_2',
|
||||
description: 'test_description_2',
|
||||
position: 1,
|
||||
position: 6,
|
||||
locale: 'es',
|
||||
slug: 'test_category_2',
|
||||
parent_category_id: category.id,
|
||||
@@ -187,6 +191,7 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||
expect(json_response['payload']['related_categories'][0]['id']).to eql(related_category_1.id)
|
||||
expect(category.reload.related_category_ids).to eq([related_category_1.id])
|
||||
expect(related_category_1.reload.related_category_ids).to be_empty
|
||||
expect(json_response['payload']['position']).to eql(category.position)
|
||||
end
|
||||
|
||||
# [category_1, category_2] !== [category_2, category_1]
|
||||
|
||||
Reference in New Issue
Block a user