diff --git a/Gemfile b/Gemfile index b8e511b1b..9029ccbcb 100644 --- a/Gemfile +++ b/Gemfile @@ -128,6 +128,9 @@ gem 'html2text' # to calculate working hours gem 'working_hours' +# full text search for articles +gem 'pg_search' + group :production, :staging do # we dont want request timing out in development while using byebug gem 'rack-timeout' diff --git a/Gemfile.lock b/Gemfile.lock index de9443f29..cff30308b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -388,6 +388,9 @@ GEM parser (3.1.1.0) ast (~> 2.4.1) pg (1.3.2) + pg_search (2.3.6) + activerecord (>= 5.2) + activesupport (>= 5.2) procore-sift (0.16.0) rails (> 4.2.0) pry (0.14.1) @@ -694,6 +697,7 @@ DEPENDENCIES mock_redis newrelic_rpm pg + pg_search procore-sift pry-rails puma @@ -742,4 +746,4 @@ RUBY VERSION ruby 3.0.4p208 BUNDLED WITH - 2.3.14 + 2.3.15 diff --git a/app/controllers/api/v1/accounts/articles_controller.rb b/app/controllers/api/v1/accounts/articles_controller.rb new file mode 100644 index 000000000..232eecd32 --- /dev/null +++ b/app/controllers/api/v1/accounts/articles_controller.rb @@ -0,0 +1,48 @@ +class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController + before_action :portal + before_action :fetch_article, except: [:index, :create] + + def index + @articles = @portal.articles + @articles.search(list_params) if params[:payload].present? + end + + def create + @article = @portal.articles.create!(article_params) + end + + def edit; end + + def show; end + + def update + @article.update!(article_params) + end + + def destroy + @article.destroy! + head :ok + end + + private + + def fetch_article + @article = @portal.articles.find(params[:id]) + end + + def portal + @portal ||= Current.account.portals.find_by(slug: params[:portal_id]) + end + + def article_params + params.require(:article).permit( + :title, :content, :description, :position, :category_id, :author_id + ) + end + + def list_params + params.require(:payload).permit( + :category_slug, :locale, :query + ) + end +end diff --git a/app/controllers/api/v1/accounts/categories_controller.rb b/app/controllers/api/v1/accounts/categories_controller.rb index 246eeb2a2..a77f1fb2a 100644 --- a/app/controllers/api/v1/accounts/categories_controller.rb +++ b/app/controllers/api/v1/accounts/categories_controller.rb @@ -3,7 +3,7 @@ class Api::V1::Accounts::CategoriesController < Api::V1::Accounts::BaseControlle before_action :fetch_category, except: [:index, :create] def index - @categories = @portal.categories + @categories = @portal.categories.search(params) end def create @@ -31,7 +31,7 @@ class Api::V1::Accounts::CategoriesController < Api::V1::Accounts::BaseControlle def category_params params.require(:category).permit( - :name, :description, :position + :name, :description, :position, :slug, :locale ) end end diff --git a/app/models/article.rb b/app/models/article.rb index 750e3a4fd..72bf3b2c4 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -2,25 +2,34 @@ # # Table name: articles # -# id :bigint not null, primary key -# content :text -# description :text -# status :integer -# title :string -# views :integer -# created_at :datetime not null -# updated_at :datetime not null -# account_id :integer not null -# author_id :integer -# category_id :integer -# folder_id :integer -# portal_id :integer not null +# id :bigint not null, primary key +# content :text +# description :text +# status :integer +# title :string +# views :integer +# created_at :datetime not null +# updated_at :datetime not null +# account_id :integer not null +# author_id :bigint +# category_id :integer +# folder_id :integer +# portal_id :integer not null +# +# Indexes +# +# index_articles_on_author_id (author_id) +# +# Foreign Keys +# +# fk_rails_... (author_id => users.id) # class Article < ApplicationRecord + include PgSearch::Model + belongs_to :account belongs_to :category belongs_to :portal - belongs_to :folder belongs_to :author, class_name: 'User' before_validation :ensure_account_id @@ -32,6 +41,36 @@ class Article < ApplicationRecord enum status: { draft: 0, published: 1 } + scope :search_by_category_slug, ->(category_slug) { where(categories: { slug: category_slug }) if category_slug.present? } + scope :search_by_category_locale, ->(locale) { where(categories: { locale: locale }) if locale.present? } + + # TODO: if text search slows down https://www.postgresql.org/docs/current/textsearch-features.html#TEXTSEARCH-UPDATE-TRIGGERS + pg_search_scope( + :text_search, + against: %i[ + title + description + content + ], + using: { + tsearch: { + prefix: true + } + } + ) + + def self.search(params) + records = joins( + :category + ).search_by_category_slug(params[:category_slug]).search_by_category_locale(params[:locale]) + records.text_search(params[:query]) if params[:query].present? + records.page(current_page(params)) + end + + def self.current_page(params) + params[:page] || 1 + end + private def ensure_account_id diff --git a/app/models/category.rb b/app/models/category.rb index 2fb5815f2..8c5989c0f 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -2,20 +2,22 @@ # # Table name: categories # -# id :bigint not null, primary key -# description :text -# locale :string default("en") -# name :string -# position :integer -# created_at :datetime not null -# updated_at :datetime not null -# account_id :integer not null -# portal_id :integer not null +# id :bigint not null, primary key +# description :text +# locale :string default("en") +# name :string +# position :integer +# slug :string not null +# created_at :datetime not null +# updated_at :datetime not null +# account_id :integer not null +# portal_id :integer not null # # Indexes # -# index_categories_on_locale (locale) -# index_categories_on_locale_and_account_id (locale,account_id) +# index_categories_on_locale (locale) +# index_categories_on_locale_and_account_id (locale,account_id) +# index_categories_on_slug_and_locale_and_portal_id (slug,locale,portal_id) UNIQUE # class Category < ApplicationRecord belongs_to :account @@ -25,7 +27,20 @@ class Category < ApplicationRecord before_validation :ensure_account_id validates :account_id, presence: true + validates :slug, presence: true validates :name, presence: true + validates :locale, uniqueness: { scope: %i[slug portal_id], + message: 'should be unique in the category and portal' } + + scope :search_by_locale, ->(locale) { where(locale: locale) if locale.present? } + + def self.search(params) + search_by_locale(params[:locale]).page(current_page(params)).order(position: :asc) + end + + def self.current_page(params) + params[:page] || 1 + end private diff --git a/app/models/user.rb b/app/models/user.rb index 534ebea21..2125f88d2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -92,6 +92,7 @@ class User < ApplicationRecord has_many :portals, through: :portals_members has_many :team_members, dependent: :destroy_async has_many :teams, through: :team_members + has_many :articles, foreign_key: 'author_id', dependent: :nullify before_validation :set_password_and_uid, on: :create diff --git a/app/views/api/v1/accounts/articles/_article.json.jbuilder b/app/views/api/v1/accounts/articles/_article.json.jbuilder new file mode 100644 index 000000000..85ff7c1a0 --- /dev/null +++ b/app/views/api/v1/accounts/articles/_article.json.jbuilder @@ -0,0 +1,20 @@ +json.id article.id +json.category_id article.category_id +json.title article.title +json.content article.content +json.description article.description +json.status article.status +json.account_id article.account_id + +if article.portal.present? + json.portal do + json.partial! 'api/v1/accounts/portals/portal.json.jbuilder', portal: article.portal + end +end +json.views article.views + +if article.author.present? + json.author do + json.partial! 'api/v1/models/agent.json.jbuilder', resource: article.author + end +end diff --git a/app/views/api/v1/accounts/articles/create.json.jbuilder b/app/views/api/v1/accounts/articles/create.json.jbuilder new file mode 100644 index 000000000..fdc6247c2 --- /dev/null +++ b/app/views/api/v1/accounts/articles/create.json.jbuilder @@ -0,0 +1,3 @@ +json.payload do + json.partial! 'article', article: @article +end diff --git a/app/views/api/v1/accounts/articles/edit.json.jbuilder b/app/views/api/v1/accounts/articles/edit.json.jbuilder new file mode 100644 index 000000000..fdc6247c2 --- /dev/null +++ b/app/views/api/v1/accounts/articles/edit.json.jbuilder @@ -0,0 +1,3 @@ +json.payload do + json.partial! 'article', article: @article +end diff --git a/app/views/api/v1/accounts/articles/index.json.jbuilder b/app/views/api/v1/accounts/articles/index.json.jbuilder new file mode 100644 index 000000000..3e3364007 --- /dev/null +++ b/app/views/api/v1/accounts/articles/index.json.jbuilder @@ -0,0 +1,3 @@ +json.payload do + json.array! @articles, partial: 'article', as: :article +end diff --git a/app/views/api/v1/accounts/articles/show.json.jbuilder b/app/views/api/v1/accounts/articles/show.json.jbuilder new file mode 100644 index 000000000..fdc6247c2 --- /dev/null +++ b/app/views/api/v1/accounts/articles/show.json.jbuilder @@ -0,0 +1,3 @@ +json.payload do + json.partial! 'article', article: @article +end diff --git a/app/views/api/v1/accounts/articles/update.json.jbuilder b/app/views/api/v1/accounts/articles/update.json.jbuilder new file mode 100644 index 000000000..fdc6247c2 --- /dev/null +++ b/app/views/api/v1/accounts/articles/update.json.jbuilder @@ -0,0 +1,3 @@ +json.payload do + json.partial! 'article', article: @article +end diff --git a/app/views/api/v1/accounts/categories/_category.json.jbuilder b/app/views/api/v1/accounts/categories/_category.json.jbuilder index 358420231..4b4915da9 100644 --- a/app/views/api/v1/accounts/categories/_category.json.jbuilder +++ b/app/views/api/v1/accounts/categories/_category.json.jbuilder @@ -1,5 +1,7 @@ json.id category.id json.name category.name +json.slug category.slug +json.locale category.locale json.description category.description json.position category.position json.account_id category.account_id diff --git a/config/routes.rb b/config/routes.rb index a09311b3e..5d1718841 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -162,8 +162,8 @@ Rails.application.routes.draw do resources :categories do resources :folders end + resources :articles end - resources :articles end end # end of account scoped api routes diff --git a/db/migrate/20220527080906_add_reference_for_author_id.rb b/db/migrate/20220527080906_add_reference_for_author_id.rb new file mode 100644 index 000000000..cf7fd22fc --- /dev/null +++ b/db/migrate/20220527080906_add_reference_for_author_id.rb @@ -0,0 +1,6 @@ +class AddReferenceForAuthorId < ActiveRecord::Migration[6.1] + def change + remove_column :articles, :author_id, :integer + add_reference :articles, :author, foreign_key: { to_table: :users } + end +end diff --git a/db/migrate/20220527120826_add_index_on_category_slug_and_locale.rb b/db/migrate/20220527120826_add_index_on_category_slug_and_locale.rb new file mode 100644 index 000000000..be17caac7 --- /dev/null +++ b/db/migrate/20220527120826_add_index_on_category_slug_and_locale.rb @@ -0,0 +1,7 @@ +class AddIndexOnCategorySlugAndLocale < ActiveRecord::Migration[6.1] + def change + add_column :categories, :slug, :string, null: false, default: '' + add_index :categories, [:slug, :locale, :portal_id], unique: true + change_column_default :categories, :slug, from: '', to: nil + end +end diff --git a/db/schema.rb b/db/schema.rb index 9599d193c..6f1ffe408 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -116,7 +116,6 @@ ActiveRecord::Schema.define(version: 2022_06_10_091206) do t.integer "portal_id", null: false t.integer "category_id" t.integer "folder_id" - t.integer "author_id" t.string "title" t.text "description" t.text "content" @@ -124,6 +123,8 @@ ActiveRecord::Schema.define(version: 2022_06_10_091206) do t.integer "views" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.bigint "author_id" + t.index ["author_id"], name: "index_articles_on_author_id" end create_table "attachments", id: :serial, force: :cascade do |t| @@ -193,8 +194,10 @@ ActiveRecord::Schema.define(version: 2022_06_10_091206) do t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.string "locale", default: "en" + t.string "slug", null: false t.index ["locale", "account_id"], name: "index_categories_on_locale_and_account_id" t.index ["locale"], name: "index_categories_on_locale" + t.index ["slug", "locale", "portal_id"], name: "index_categories_on_slug_and_locale_and_portal_id", unique: true end create_table "channel_api", force: :cascade do |t| @@ -817,6 +820,7 @@ ActiveRecord::Schema.define(version: 2022_06_10_091206) do add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "agent_bots", "accounts", on_delete: :cascade + add_foreign_key "articles", "users", column: "author_id" add_foreign_key "campaigns", "accounts", on_delete: :cascade add_foreign_key "campaigns", "inboxes", on_delete: :cascade add_foreign_key "contact_inboxes", "contacts", on_delete: :cascade diff --git a/spec/controllers/api/v1/accounts/articles_controller_spec.rb b/spec/controllers/api/v1/accounts/articles_controller_spec.rb new file mode 100644 index 000000000..69c1ee4ca --- /dev/null +++ b/spec/controllers/api/v1/accounts/articles_controller_spec.rb @@ -0,0 +1,137 @@ +require 'rails_helper' + +RSpec.describe 'Api::V1::Accounts::Articles', 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) } + let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id, locale: 'en', slug: 'category_slug') } + let!(:article) { create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id) } + + describe 'POST /api/v1/accounts/{account.id}/portals/{portal.slug}/articles' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles", params: {} + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + it 'creates article' do + article_params = { + article: { + category_id: category.id, + description: 'test description', + title: 'MyTitle', + content: 'This is my content.', + status: :published, + author_id: agent.id + } + } + post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles", + params: article_params, + headers: agent.create_new_auth_token + expect(response).to have_http_status(:success) + json_response = JSON.parse(response.body) + expect(json_response['payload']['title']).to eql('MyTitle') + end + end + end + + describe 'PUT /api/v1/accounts/{account.id}/portals/{portal.slug}/articles/{article.id}' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{article.id}", params: {} + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + it 'updates category' do + article_params = { + article: { + title: 'MyTitle2', + description: 'test_description' + } + } + + expect(article.title).not_to eql(article_params[:article][:title]) + + put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{article.id}", + params: article_params, + headers: agent.create_new_auth_token + expect(response).to have_http_status(:success) + json_response = JSON.parse(response.body) + expect(json_response['payload']['title']).to eql(article_params[:article][:title]) + end + end + end + + describe 'DELETE /api/v1/accounts/{account.id}/portals/{portal.slug}/articles/{article.id}' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + delete "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{article.id}", params: {} + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + it 'deletes category' do + delete "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{article.id}", + headers: agent.create_new_auth_token + expect(response).to have_http_status(:success) + deleted_article = Article.find_by(id: article.id) + expect(deleted_article).to be nil + end + end + end + + describe 'GET /api/v1/accounts/{account.id}/portals/{portal.slug}/articles' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles" + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + it 'get all articles' do + article2 = create(:article, account_id: account.id, portal: portal, category: category, author_id: agent.id) + expect(article2.id).not_to be nil + + get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles", + headers: agent.create_new_auth_token, + params: { payload: {} } + expect(response).to have_http_status(:success) + json_response = JSON.parse(response.body) + expect(json_response['payload'].count).to be 2 + end + + it 'get all articles with searched params' do + article2 = create(:article, account_id: account.id, portal: portal, category: category, author_id: agent.id) + expect(article2.id).not_to be nil + + get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles", + headers: agent.create_new_auth_token, + params: { payload: { category_slug: category.slug } } + expect(response).to have_http_status(:success) + json_response = JSON.parse(response.body) + expect(json_response['payload'].count).to be 2 + end + end + + describe 'GET /api/v1/accounts/{account.id}/portals/{portal.slug}/articles/{article.id}' do + it 'get article' do + article2 = create(:article, account_id: account.id, portal: portal, category: category, author_id: agent.id) + expect(article2.id).not_to be nil + + get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{article2.id}", + headers: agent.create_new_auth_token + expect(response).to have_http_status(:success) + json_response = JSON.parse(response.body) + + expect(json_response['payload']['title']).to eq(article2.title) + expect(json_response['payload']['id']).to eq(article2.id) + end + 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 a1b767166..053fe9d5e 100644 --- a/spec/controllers/api/v1/accounts/categories_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/categories_controller_spec.rb @@ -4,7 +4,7 @@ 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) } - let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id) } + let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id, slug: 'category_slug') } describe 'POST /api/v1/accounts/{account.id}/portals/{portal.slug}/categories' do context 'when it is an unauthenticated user' do @@ -15,14 +15,17 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do end context 'when it is an authenticated user' do - it 'creates category' do - category_params = { - category: { - name: 'test_category', - description: 'test_description', - position: 1 - } + category_params = { + category: { + name: 'test_category', + description: 'test_description', + position: 1, + locale: 'es', + slug: 'test_category_1' } + } + + it 'creates category' do post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories", params: category_params, headers: agent.create_new_auth_token @@ -30,6 +33,37 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do json_response = JSON.parse(response.body) expect(json_response['payload']['name']).to eql('test_category') end + + it 'will throw an error on locale, category_id uniqueness' do + post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories", + params: category_params, + headers: agent.create_new_auth_token + + post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories", + params: category_params, + headers: agent.create_new_auth_token + expect(response).to have_http_status(:unprocessable_entity) + json_response = JSON.parse(response.body) + expect(json_response['message']).to eql('Locale should be unique in the category and portal') + end + + it 'will throw an error slug presence' do + category_params = { + category: { + name: 'test_category', + description: 'test_description', + position: 1, + locale: 'es' + } + } + + post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories", + params: category_params, + headers: agent.create_new_auth_token + expect(response).to have_http_status(:unprocessable_entity) + json_response = JSON.parse(response.body) + expect(json_response['message']).to eql("Slug can't be blank") + end end end @@ -92,7 +126,7 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do context 'when it is an authenticated user' do it 'get all portals' do - category2 = create(:category, name: 'test_category_2', portal: portal) + category2 = create(:category, name: 'test_category_2', portal: portal, locale: 'es', slug: 'category_slug_2') expect(category2.id).not_to be nil get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories", diff --git a/spec/factories/articles.rb b/spec/factories/articles.rb index 3f0c94c47..9ba5d9ac4 100644 --- a/spec/factories/articles.rb +++ b/spec/factories/articles.rb @@ -2,13 +2,11 @@ FactoryBot.define do factory :article, class: 'Article' do account_id { 1 } category_id { 1 } - folder_id { 1 } author_id { 1 } title { 'MyString' } content { 'MyText' } + description { 'MyDescrption' } status { 1 } views { 1 } - seo_title { 'MyString' } - seo { '' } end end diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index ee1e7f618..1945cca62 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -14,4 +14,105 @@ RSpec.describe Article, type: :model do it { is_expected.to belong_to(:category) } it { is_expected.to belong_to(:author) } end + + describe 'search' do + let!(:account) { create(:account) } + let(:user) { create(:user, account_ids: [account.id], role: :agent) } + let!(:portal_1) { create(:portal, account_id: account.id) } + let!(:portal_2) { create(:portal, account_id: account.id) } + let!(:category_1) { create(:category, slug: 'category_1', locale: 'en', portal_id: portal_1.id) } + let!(:category_2) { create(:category, slug: 'category_2', locale: 'es', portal_id: portal_1.id) } + let!(:category_3) { create(:category, slug: 'category_3', locale: 'es', portal_id: portal_2.id) } + + before do + create(:article, category_id: category_1.id, content: 'This is the content', description: 'this is the description', title: 'this is title', + portal_id: portal_1.id, author_id: user.id) + create(:article, category_id: category_1.id, title: 'title 1', portal_id: portal_1.id, author_id: user.id) + create(:article, category_id: category_2.id, title: 'title 2', portal_id: portal_2.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_3.id, title: 'title 6', portal_id: portal_2.id, author_id: user.id) + create(:article, category_id: category_2.id, title: 'title 7', portal_id: portal_1.id, author_id: user.id) + end + + context 'when no parameters passed' do + it 'returns all the articles in portal' do + records = portal_1.articles.search({}) + expect(records.count).to eq(portal_1.articles.count) + + records = portal_2.articles.search({}) + expect(records.count).to eq(portal_2.articles.count) + end + end + + context 'when params passed' do + it 'returns all the articles with all the params filters' do + params = { query: 'title', locale: 'es', category_slug: 'category_3' } + records = portal_2.articles.search(params) + expect(records.count).to eq(1) + + params = { query: 'this', locale: 'en', category_slug: 'category_1' } + records = portal_1.articles.search(params) + expect(records.count).to eq(2) + end + end + + context 'when some params missing' do + it 'returns data with category slug' do + params = { category_slug: 'category_2' } + records = portal_1.articles.search(params) + expect(records.count).to eq(2) + end + + it 'returns data with locale' do + params = { locale: 'es' } + records = portal_2.articles.search(params) + expect(records.count).to eq(2) + + params = { locale: 'en' } + records = portal_1.articles.search(params) + expect(records.count).to eq(2) + end + + it 'returns data with text_search query' do + params = { query: 'title' } + records = portal_2.articles.search(params) + expect(records.count).to eq(2) + + params = { query: 'title' } + records = portal_1.articles.search(params) + expect(records.count).to eq(4) + + params = { query: 'the content' } + records = portal_2.articles.search(params) + expect(records.count).to eq(2) + end + + it 'returns data with text_search query and locale' do + params = { query: 'the title', locale: 'es' } + records = portal_2.articles.search(params) + expect(records.count).to eq(2) + end + + it 'returns records with locale and category_slug' do + params = { category_slug: 'category_2', locale: 'es' } + records = portal_1.articles.search(params) + expect(records.count).to eq(2) + end + + it 'return records with category_slug and text_search query' do + params = { category_slug: 'category_2', query: 'the title' } + records = portal_1.articles.search(params) + expect(records.count).to eq(2) + end + end + + context 'with pagination' 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) + params = { category_slug: 'category_2' } + records = portal_2.articles.search(params) + expect(records.count).to eq(25) + end + end + end end diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 55bad9a14..37b17284a 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -12,4 +12,39 @@ RSpec.describe Category, type: :model do it { is_expected.to have_many(:folders) } it { is_expected.to have_many(:articles) } end + + describe 'search' do + let!(:account) { create(:account) } + let(:user) { create(:user, account_ids: [account.id], role: :agent) } + let!(:portal_1) { create(:portal, account_id: account.id) } + let!(:portal_2) { create(:portal, account_id: account.id) } + + before do + create(:category, slug: 'category_1', locale: 'en', portal_id: portal_1.id) + create(:category, slug: 'category_2', locale: 'es', portal_id: portal_1.id) + create(:category, slug: 'category_3', locale: 'es', portal_id: portal_2.id) + end + + context 'when no parameters passed' do + it 'returns all the articles in portal' do + records = portal_1.categories.search({}) + expect(records.count).to eq(portal_1.categories.count) + + records = portal_2.categories.search({}) + expect(records.count).to eq(portal_2.categories.count) + end + end + + context 'when params passed' do + it 'returns all the categories with all the params filters' do + params = { locale: 'es' } + records = portal_2.categories.search(params) + expect(records.count).to eq(1) + + params = { locale: 'en' } + records = portal_1.categories.search(params) + expect(records.count).to eq(1) + end + end + end end