add search by terms on posts
This commit is contained in:
6
Gemfile
6
Gemfile
@@ -2,11 +2,12 @@ source "https://rubygems.org"
|
|||||||
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
||||||
|
|
||||||
ruby "3.1.1"
|
ruby "3.1.1"
|
||||||
|
|
||||||
gem "rails", "~> 7.0.2", ">= 7.0.2.2"
|
gem "rails", "~> 7.0.2", ">= 7.0.2.2"
|
||||||
gem "pg", "~> 1.1"
|
gem "pg", "~> 1.1"
|
||||||
gem "puma", "~> 5.0"
|
gem "puma", "~> 5.0"
|
||||||
gem "jbuilder"
|
gem "jbuilder"
|
||||||
|
gem "pg_search", "~> 2.3"
|
||||||
gem "bootsnap", require: false
|
gem "bootsnap", require: false
|
||||||
|
|
||||||
group :development, :test do
|
group :development, :test do
|
||||||
@@ -14,6 +15,5 @@ group :development, :test do
|
|||||||
|
|
||||||
gem "rspec-rails", "~> 5.1"
|
gem "rspec-rails", "~> 5.1"
|
||||||
gem "factory_bot_rails", "~> 6.2"
|
gem "factory_bot_rails", "~> 6.2"
|
||||||
end
|
|
||||||
|
|
||||||
gem "faker", "~> 2.19"
|
gem "faker", "~> 2.19"
|
||||||
|
end
|
||||||
|
|||||||
@@ -124,6 +124,9 @@ GEM
|
|||||||
nokogiri (1.13.3-x86_64-linux)
|
nokogiri (1.13.3-x86_64-linux)
|
||||||
racc (~> 1.4)
|
racc (~> 1.4)
|
||||||
pg (1.3.3)
|
pg (1.3.3)
|
||||||
|
pg_search (2.3.6)
|
||||||
|
activerecord (>= 5.2)
|
||||||
|
activesupport (>= 5.2)
|
||||||
puma (5.6.2)
|
puma (5.6.2)
|
||||||
nio4r (~> 2.0)
|
nio4r (~> 2.0)
|
||||||
racc (1.6.0)
|
racc (1.6.0)
|
||||||
@@ -196,6 +199,7 @@ DEPENDENCIES
|
|||||||
faker (~> 2.19)
|
faker (~> 2.19)
|
||||||
jbuilder
|
jbuilder
|
||||||
pg (~> 1.1)
|
pg (~> 1.1)
|
||||||
|
pg_search (~> 2.3)
|
||||||
puma (~> 5.0)
|
puma (~> 5.0)
|
||||||
rails (~> 7.0.2, >= 7.0.2.2)
|
rails (~> 7.0.2, >= 7.0.2.2)
|
||||||
rspec-rails (~> 5.1)
|
rspec-rails (~> 5.1)
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ class PostsController < ApplicationController
|
|||||||
private
|
private
|
||||||
|
|
||||||
def query_params
|
def query_params
|
||||||
params.permit(:scope) || {}
|
params.permit(:scope, :terms)
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_params
|
def post_params
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
class Post < ApplicationRecord
|
class Post < ApplicationRecord
|
||||||
|
include PgSearch::Model
|
||||||
|
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :quoted_post, optional: true, class_name: 'Post'
|
belongs_to :quoted_post, optional: true, class_name: 'Post'
|
||||||
|
|
||||||
validates :content, length: { maximum: 777 }
|
validates :content, length: { maximum: 777 }
|
||||||
validates :content, presence: true, unless: :repost?
|
validates :content, presence: true, unless: :repost?
|
||||||
|
|
||||||
validate :user, :limit_of_post_per_day
|
validate :user, :limit_of_post_per_day
|
||||||
|
|
||||||
scope :by_user_follows, ->(user) {
|
pg_search_scope :by_terms, against: :content
|
||||||
where(user_id: user.following_ids)
|
scope :by_user_follows, ->(user) { where(user_id: user.following_ids) }
|
||||||
}
|
|
||||||
|
|
||||||
def repost?
|
def repost?
|
||||||
kind == :quoted_post
|
kind == :quoted_post
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ class PostsQueryResolverService
|
|||||||
scope = scope.by_user_follows(current_user)
|
scope = scope.by_user_follows(current_user)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if filter[:terms]
|
||||||
|
scope = scope.by_terms(filter[:terms])
|
||||||
|
end
|
||||||
|
|
||||||
scope
|
scope
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user