Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/logical_query_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ def new
LogicalQueryParserParser.new
end

def search(query, relations, *options)
def search(query, relations, *args, parser: new, **options)
relations = relations.all if relations.respond_to?(:all)
assoc = resolve_assocs(relations.klass, *options)
sql = new.parse(query).to_sql(model: relations.klass, columns: assoc.column_mapping)
args << options if options.any?
assoc = resolve_assocs(relations.klass, *args)
sql = parser.parse(query).to_sql(model: relations.klass, columns: assoc.column_mapping)
relations.joins(assoc.structure).where(sql)
end

Expand Down
30 changes: 30 additions & 0 deletions spec/logical_query_parser/nodes/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,34 @@
expect(relations.to_a).not_to be_nil
end
end

context 'search with parser' do
it 'searches' do
relations = LogicalQueryParser.search("aa AND bb", Doc, :title, :body, parser: LogicalQueryParserParser.new)
debug(relations.to_sql)
expect(relations.to_sql).to match sequence %W|( ( title aa OR body aa ) AND ( title bb OR body bb )|
expect(relations.to_a).not_to be_nil
end

it 'searches one association' do
relations = LogicalQueryParser.search("aa AND bb", Doc, :title, { tags: :name }, parser: LogicalQueryParserParser.new)
debug(relations.to_sql)
expect(relations.to_sql).to match sequence %W|( ( title aa OR tags name aa ) AND ( title bb OR tags name bb )|
expect(relations.to_a).not_to be_nil
end

it 'searches nested association' do
relations = LogicalQueryParser.search("aa AND bb", Doc, :title, tags: [:name, users: :name], parser: LogicalQueryParserParser.new)
debug(relations.to_sql)
expect(relations.to_sql).to match sequence %W|( ( ( title aa OR tags name aa ) OR users name aa ) AND ( ( title bb OR tags name bb ) OR users name bb )|
expect(relations.to_a).not_to be_nil
end

it 'searches nested association with array' do
relations = LogicalQueryParser.search("aa AND bb", Doc, [:title, tags: [:name, users: :name]], parser: LogicalQueryParserParser.new)
debug(relations.to_sql)
expect(relations.to_sql).to match sequence %W|( ( ( title aa OR tags name aa ) OR users name aa ) AND ( ( title bb OR tags name bb ) OR users name bb )|
expect(relations.to_a).not_to be_nil
end
end
end
Loading