|
1 | 1 | require 'spec_helper'
|
2 | 2 |
|
3 | 3 | RSpec.describe "searching", elasticsearch: true do
|
| 4 | + class MyLookup |
| 5 | + include Virtus.model |
| 6 | + |
| 7 | + def load(values) |
| 8 | + values.map do | value | |
| 9 | + value[:text] = "#{value[:key]} - test" |
| 10 | + value |
| 11 | + end |
| 12 | + end |
| 13 | + end |
4 | 14 |
|
5 | 15 | let(:klass) do
|
6 | 16 | Class.new(Trample::Search) do
|
|
14 | 24 | condition :_name_prefix, query_name: :name, prefix: true
|
15 | 25 |
|
16 | 26 | condition :simple_name, query_name: 'name', single: true
|
| 27 | + condition :company_ids, query_name: 'company_id', |
| 28 | + lookup: { |
| 29 | + key: :company_id, |
| 30 | + label: :company_name |
| 31 | + } |
| 32 | + condition :company_custom_ids, query_name: 'company_id', |
| 33 | + lookup: { |
| 34 | + key: :company_id, |
| 35 | + label: :company_name, |
| 36 | + klass: 'MyLookup' |
| 37 | + } |
17 | 38 | end
|
18 | 39 | end
|
19 | 40 |
|
20 | 41 | before do
|
21 | 42 | Searchkick.client.indices.delete index: '_all'
|
22 |
| - Person.create!(name: 'Homer', tags: ['funny', 'stupid', 'bald'], age: 38) |
23 |
| - Person.create!(name: 'Lisa', tags: ['funny', 'smart', 'kid'], age: 8) |
24 |
| - Person.create!(name: 'Marge', tags: ['motherly'], age: 34) |
25 |
| - Person.create!(name: 'Bart', tags: ['funny', 'stupid', 'kid'], age: 10) |
| 43 | + foo = Company.create!(name: 'Foo Inc') |
| 44 | + bar = Company.create!(name: 'Bar Inc') |
| 45 | + apex = Company.create!(name: 'Apex Inc') |
| 46 | + Person.create!(name: 'Homer', tags: ['funny', 'stupid', 'bald'], age: 38, company: foo) |
| 47 | + Person.create!(name: 'Lisa', tags: ['funny', 'smart', 'kid'], age: 8, company: bar) |
| 48 | + Person.create!(name: 'Marge', tags: ['motherly'], age: 34, company: foo) |
| 49 | + Person.create!(name: 'Bart', tags: ['funny', 'stupid', 'kid'], age: 10, company: apex) |
26 | 50 | Person.reindex
|
27 | 51 |
|
28 | 52 | klass.model(Person)
|
29 | 53 | end
|
30 | 54 |
|
| 55 | + it "should fetch autocomplete labels by default" do |
| 56 | + search = klass.new |
| 57 | + search.condition(:company_ids).in([{key: Company.first.id.to_s}, { key: Company.last.id.to_s, text: Company.last.name }]) |
| 58 | + search.query! |
| 59 | + |
| 60 | + expect(search.conditions.as_json['company_ids'][:values]).to eq([{:key=>"1", :text=>"Foo Inc"}, {:key=>"3", :text=>"Apex Inc"}]) |
| 61 | + end |
| 62 | + |
| 63 | + it "should not fetch autocomplete labels when lookup: false" do |
| 64 | + search = klass.new |
| 65 | + values = [{key: Company.first.id.to_s}, { key: Company.last.id.to_s, text: Company.last.name }] |
| 66 | + search.condition(:company_ids).in(values) |
| 67 | + search.query!(lookup: false) |
| 68 | + |
| 69 | + expect(search.conditions.as_json['company_ids'][:values]).to eq([{:key=>"1"}, {:key=>"3", :text=>"Apex Inc"}]) |
| 70 | + end |
| 71 | + |
| 72 | + it "should fetch autocomplete labels using custom lookup_class" do |
| 73 | + search = klass.new |
| 74 | + search.condition(:company_custom_ids).in([{key: Company.first.id.to_s}, { key: Company.last.id.to_s, text: Company.last.name }]) |
| 75 | + search.query! |
| 76 | + |
| 77 | + expect(search.conditions.as_json['company_custom_ids'][:values]).to eq([{:key=>"1", :text=>"#{Company.first.id} - test"}, {:key=>"3", :text=> "#{Company.last.id} - test"}]) |
| 78 | + end |
| 79 | + |
31 | 80 | it "records time the search took" do
|
32 | 81 | search = klass.new
|
33 | 82 | search.query!
|
|
0 commit comments