Skip to content

Commit

Permalink
Search entity (API) (#12)
Browse files Browse the repository at this point in the history
* search entity

* refactor search query for properties

* refactor field search query

* Update search.rb

Co-authored-by: Pradeep Kumar <[email protected]>
  • Loading branch information
abhinaykumar and RabidFire authored Nov 2, 2020
1 parent 135eba8 commit da8c2cb
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
23 changes: 23 additions & 0 deletions api/app/graphql/resolvers/fields_resolver.rb
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
22 changes: 22 additions & 0 deletions api/app/graphql/resolvers/records_resolver.rb
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
34 changes: 34 additions & 0 deletions api/app/models/search.rb
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
8 changes: 8 additions & 0 deletions api/spec/factories/properties.rb
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
42 changes: 42 additions & 0 deletions api/spec/models/search_spec.rb
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

0 comments on commit da8c2cb

Please sign in to comment.