-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* search entity * refactor search query for properties * refactor field search query * Update search.rb Co-authored-by: Pradeep Kumar <[email protected]>
- Loading branch information
1 parent
135eba8
commit da8c2cb
Showing
5 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,33 @@ | ||
class FieldsResolver < ApplicationResolver | ||
parameter :entityId, types.ID | ||
parameter :projectId, types.ID | ||
parameter :filter, types.String | ||
|
||
def resolve | ||
# For search | ||
if search_params_is_present? | ||
project = Project.find(params[:project_id]) | ||
authorize! project, :view? | ||
|
||
return filtered_fields | ||
end | ||
|
||
entity = resolved_object || Entity.find(params[:entity_id]) | ||
authorize! entity, :view? | ||
|
||
entity.nested_fields | ||
end | ||
|
||
def search_params_is_present? | ||
params[:filter].present? && | ||
params[:project_id].present? | ||
end | ||
|
||
def filtered_fields | ||
Search.scope( | ||
query: params[:filter], | ||
scope: 'field', | ||
project_id: params[:project_id] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,32 @@ | ||
class RecordsResolver < ApplicationResolver | ||
parameter :entityId, types.ID | ||
parameter :filter, types.String | ||
parameter :projectId, types.ID | ||
|
||
def resolve | ||
if search_params_is_present? | ||
project = Project.find(params[:project_id]) | ||
authorize! project, :view? | ||
|
||
return filtered_records | ||
end | ||
|
||
entity = resolved_object || Entity.find(params[:entity_id]) | ||
authorize! entity, :view? | ||
|
||
entity.records | ||
end | ||
|
||
def search_params_is_present? | ||
params[:filter].present? && | ||
params[:project_id].present? | ||
end | ||
|
||
def filtered_records | ||
Search.scope( | ||
query: params[:filter], | ||
scope: 'record', | ||
project_id: params[:project_id] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class Search | ||
ALLOWED_SCOPE = %w[field record].freeze | ||
|
||
class << self | ||
def scope(query:, scope:, project_id:) | ||
return unless ALLOWED_SCOPE.include?(scope) | ||
|
||
public_send("#{scope}_search", query, project_id) | ||
end | ||
|
||
def field_search(query, project_id) | ||
fields = Field.includes( | ||
entity: :project, | ||
referenced_entity: :project | ||
).where( | ||
projects: { | ||
id: project_id | ||
} | ||
) | ||
|
||
fields.where('fields.name ILIKE?', "%#{query}%") | ||
.or(fields.where('fields.label ILIKE?', "%#{query}%")) | ||
end | ||
|
||
def record_search(query, project_id) | ||
record_ids = Property.includes(record: { entity: :project }, linked_record: {entity: :project}) | ||
.where(projects: { id: project_id }) | ||
.where('value ILIKE ?', "%#{query}%") | ||
.pluck(:record_id).uniq | ||
|
||
Record.where(id: record_ids) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FactoryBot.define do | ||
factory :property do | ||
record | ||
field | ||
|
||
sequence(:value) { |n| "Property Name #{n}" } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Search do | ||
describe '#scope' do | ||
let(:project) { create(:project) } | ||
let(:entity) { create(:entity, project: project) } | ||
let(:record) { create(:record, entity: entity) } | ||
|
||
it 'should raise error when scope is not present as argument' do | ||
expect { Search.scope(query: 'Test') }.to raise_error(ArgumentError) | ||
end | ||
|
||
it 'should return nil, for scope which are not in in ALLOWED_SCOPE' do | ||
expect(Search.scope(query: 'Test', scope: 'Test', project_id: project.id)).to be(nil) | ||
end | ||
|
||
context 'when scope is entity' do | ||
context 'when matching entities are found' do | ||
it 'should return object of type Entity' do | ||
5.times { |i| create(:field, name: "Test-#{i}", entity: entity) } | ||
|
||
res = Search.scope(query: 'test', scope: 'field', project_id: project.id) | ||
|
||
expect(res.klass).to be(Field) | ||
end | ||
end | ||
end | ||
|
||
context 'when scope is record' do | ||
context 'when matching records are found' do | ||
it 'should return object of type Record' do | ||
field = create(:field, name: 'Test', entity: entity) | ||
5.times { |i| create(:property, field: field, record: record, value: "Test-#{i}") } | ||
|
||
res = Search.scope(query: 'test', scope: 'record', project_id: project.id) | ||
|
||
expect(res.klass).to be(Record) | ||
end | ||
end | ||
end | ||
end | ||
end |