Skip to content

Commit

Permalink
feat: Backend changes for article and categories ordering (#6655)
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaswinichile committed Mar 13, 2023
1 parent 2e95d3a commit 8f4d479
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 18 deletions.
6 changes: 3 additions & 3 deletions app/controllers/api/v1/accounts/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def portal

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# content :text
# description :text
# meta :jsonb
# position :integer
# slug :string not null
# status :integer
# title :string
Expand Down
1 change: 1 addition & 0 deletions app/views/api/v1/accounts/articles/_article.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/views/public/api/v1/models/_article.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions app/views/public/api/v1/portals/_category-block.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -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 %>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion app/views/public/api/v1/portals/show.html.erb
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20230313113920_add_position_to_articles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPositionToArticles < ActiveRecord::Migration[6.1]
def change
add_column :articles, :position, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -133,6 +133,7 @@
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
Expand Down
8 changes: 6 additions & 2 deletions spec/controllers/api/v1/accounts/articles_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
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",
Expand All @@ -37,6 +38,7 @@
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
Expand Down Expand Up @@ -110,7 +112,8 @@
article: {
title: 'MyTitle2',
status: 'published',
description: 'test_description'
description: 'test_description',
position: 5
}
}

Expand All @@ -123,6 +126,7 @@
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
Expand Down
17 changes: 11 additions & 6 deletions spec/controllers/api/v1/accounts/categories_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
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) }

Expand All @@ -27,7 +31,7 @@
category: {
name: 'test_category',
description: 'test_description',
position: 1,
position: 5,
locale: 'es',
slug: 'test_category_1',
parent_category_id: category.id,
Expand All @@ -42,7 +46,7 @@
category: {
name: 'test_category_2',
description: 'test_description_2',
position: 1,
position: 6,
locale: 'es',
slug: 'test_category_2',
parent_category_id: category.id,
Expand Down Expand Up @@ -187,6 +191,7 @@
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]
Expand Down

0 comments on commit 8f4d479

Please sign in to comment.