From 8f4d4798c2f89149767790b6e8c16dc23b9ee485 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Mon, 13 Mar 2023 17:39:07 +0530 Subject: [PATCH] feat: Backend changes for article and categories ordering (#6655) --- .../api/v1/accounts/articles_controller.rb | 6 +++--- .../api/v1/portals/articles_controller.rb | 1 + .../api/v1/portals/categories_controller.rb | 2 +- app/models/article.rb | 1 + .../v1/accounts/articles/_article.json.jbuilder | 1 + .../public/api/v1/models/_article.json.jbuilder | 1 + .../api/v1/portals/_category-block.html.erb | 4 ++-- .../portals/categories/_category-block.html.erb | 4 ++-- app/views/public/api/v1/portals/show.html.erb | 2 +- .../20230313113920_add_position_to_articles.rb | 5 +++++ db/schema.rb | 3 ++- .../api/v1/accounts/articles_controller_spec.rb | 8 ++++++-- .../v1/accounts/categories_controller_spec.rb | 17 +++++++++++------ 13 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 db/migrate/20230313113920_add_position_to_articles.rb diff --git a/app/controllers/api/v1/accounts/articles_controller.rb b/app/controllers/api/v1/accounts/articles_controller.rb index b6e07be5f..f1689d918 100644 --- a/app/controllers/api/v1/accounts/articles_controller.rb +++ b/app/controllers/api/v1/accounts/articles_controller.rb @@ -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 diff --git a/app/controllers/public/api/v1/portals/articles_controller.rb b/app/controllers/public/api/v1/portals/articles_controller.rb index 5fccc9ad1..af5410fc3 100644 --- a/app/controllers/public/api/v1/portals/articles_controller.rb +++ b/app/controllers/public/api/v1/portals/articles_controller.rb @@ -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 diff --git a/app/controllers/public/api/v1/portals/categories_controller.rb b/app/controllers/public/api/v1/portals/categories_controller.rb index b1096fbeb..7326f383b 100644 --- a/app/controllers/public/api/v1/portals/categories_controller.rb +++ b/app/controllers/public/api/v1/portals/categories_controller.rb @@ -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 diff --git a/app/models/article.rb b/app/models/article.rb index 842bf2a6f..faed38e29 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -6,6 +6,7 @@ # content :text # description :text # meta :jsonb +# position :integer # slug :string not null # status :integer # title :string diff --git a/app/views/api/v1/accounts/articles/_article.json.jbuilder b/app/views/api/v1/accounts/articles/_article.json.jbuilder index 19b759161..996b58590 100644 --- a/app/views/api/v1/accounts/articles/_article.json.jbuilder +++ b/app/views/api/v1/accounts/articles/_article.json.jbuilder @@ -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 diff --git a/app/views/public/api/v1/models/_article.json.jbuilder b/app/views/public/api/v1/models/_article.json.jbuilder index cacf48dff..0fcdf2ed3 100644 --- a/app/views/public/api/v1/models/_article.json.jbuilder +++ b/app/views/public/api/v1/models/_article.json.jbuilder @@ -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 diff --git a/app/views/public/api/v1/portals/_category-block.html.erb b/app/views/public/api/v1/portals/_category-block.html.erb index a3424d432..c309b85e4 100644 --- a/app/views/public/api/v1/portals/_category-block.html.erb +++ b/app/views/public/api/v1/portals/_category-block.html.erb @@ -6,7 +6,7 @@ - <%= 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 %>
@@ -15,7 +15,7 @@

<%= I18n.t('public_portal.common.no_articles') %>

<% else %> - <% category.articles.published.take(5).each do |article| %> + <% category.articles.published.order(position: :asc).take(5).each do |article| %> - <%= 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 %>
@@ -16,7 +16,7 @@

<%= I18n.t('public_portal.common.no_articles') %>

<% else %> - <% category.articles.published.take(5).each do |article| %> + <% category.articles.published.order(position: :asc).take(5).each do |article| %>
- <% @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 %>
diff --git a/db/migrate/20230313113920_add_position_to_articles.rb b/db/migrate/20230313113920_add_position_to_articles.rb new file mode 100644 index 000000000..d7e195923 --- /dev/null +++ b/db/migrate/20230313113920_add_position_to_articles.rb @@ -0,0 +1,5 @@ +class AddPositionToArticles < ActiveRecord::Migration[6.1] + def change + add_column :articles, :position, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 991f5afb4..eb13c753d 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: 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 diff --git a/spec/controllers/api/v1/accounts/articles_controller_spec.rb b/spec/controllers/api/v1/accounts/articles_controller_spec.rb index 747a0c88d..6e4a7733a 100644 --- a/spec/controllers/api/v1/accounts/articles_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/articles_controller_spec.rb @@ -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 diff --git a/spec/controllers/api/v1/accounts/categories_controller_spec.rb b/spec/controllers/api/v1/accounts/categories_controller_spec.rb index 03898a3a7..783e89d93 100644 --- a/spec/controllers/api/v1/accounts/categories_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/categories_controller_spec.rb @@ -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]