-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from mon-territoire/refactor-multiple_contexts
Refactor contexts and allow multiple contexts to be passed
- Loading branch information
Showing
8 changed files
with
230 additions
and
27 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,4 +1,59 @@ | ||
--- | ||
title: "[TODO] Contexts" | ||
title: Contexts | ||
order: -5 | ||
--- | ||
--- | ||
|
||
Contexts allows to create search scopes independently of search criteria. | ||
Because search criteria may comes from user inputs, contexts offers a way to force search scoping : | ||
|
||
```ruby | ||
class ArticleSearch < Caoutsearch::Search::Base | ||
has_context :public do | ||
filters << { term: { published: true } } | ||
end | ||
end | ||
|
||
ArticleSearch.context(:public).search(params[:q]) | ||
``` | ||
|
||
Multiple contexts can be passed together or chained: | ||
|
||
```ruby | ||
ArticleSearch.context(:public, :blog) | ||
ArticleSearch.context(:public).context(:blog) | ||
``` | ||
|
||
Current context can used to alter search queries or filters: | ||
|
||
```ruby | ||
class ArticleSearch < Caoutsearch::Search::Base | ||
has_context :public do | ||
filters << { term: { published: true } } | ||
end | ||
|
||
match_all do |value| | ||
targets = %w[title body] | ||
targets << "author" unless current_context?(:public) | ||
|
||
filter_by(targets, value) | ||
end | ||
end | ||
``` | ||
|
||
Missing contexts doesn't raise an expection and are ignored. | ||
It allows you to apply contexts to any searches, whether the context is implemented or not: | ||
|
||
```ruby | ||
class BlogController < ApplicationController | ||
def index | ||
@articles = ArticleSearch.context(current_context).search(params[:q]) | ||
@tags = TagSearch.context(current_context).search(params[:q]) # TagSearch doesn't implement a "public" context | ||
end | ||
|
||
protected | ||
|
||
def current_context | ||
:public unless current_user&.admin? | ||
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
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
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
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 @@ | ||
# frozen_string_literal: true | ||
|
||
module Caoutsearch | ||
module Search | ||
module QueryBuilder | ||
module Contexts | ||
private | ||
|
||
def build_contexts | ||
call_contexts(*current_contexts) if current_contexts | ||
end | ||
|
||
def call_contexts(*args) | ||
args.each do |arg| | ||
call_context(arg) | ||
end | ||
end | ||
|
||
def call_context(name) | ||
name = name.to_s | ||
|
||
if self.class.contexts.include?(name) | ||
item = self.class.contexts[name] | ||
call_context_item(item) | ||
end | ||
end | ||
|
||
def call_context_item(item) | ||
instance_exec(&item.block) | ||
end | ||
end | ||
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
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,60 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe Caoutsearch::Search::QueryBuilder::Contexts do | ||
let!(:search_class) do | ||
stub_search_class("SampleSearch") do | ||
has_context :public do | ||
filters << {term: {published: true}} | ||
end | ||
|
||
has_context :blog do | ||
filters << {term: {destination: "blog"}} | ||
end | ||
end | ||
end | ||
|
||
it "applies one context to a query" do | ||
search = search_class.new.context(:public) | ||
|
||
expect(search.build).to eq( | ||
query: { | ||
bool: { | ||
filter: [ | ||
{term: {published: true}} | ||
] | ||
} | ||
} | ||
) | ||
end | ||
|
||
it "builds a query with multiple contexts" do | ||
search = search_class.new.context(:public, :blog) | ||
|
||
expect(search.build).to eq( | ||
query: { | ||
bool: { | ||
filter: [ | ||
{term: {published: true}}, | ||
{term: {destination: "blog"}} | ||
] | ||
} | ||
} | ||
) | ||
end | ||
|
||
it "ignores missing contexts" do | ||
search = search_class.new.context(:public, :api) | ||
|
||
expect(search.build).to eq( | ||
query: { | ||
bool: { | ||
filter: [ | ||
{term: {published: true}} | ||
] | ||
} | ||
} | ||
) | ||
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