pursuit 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +3 -2
- data/lib/pursuit/constants.rb +1 -1
- data/lib/pursuit/search.rb +4 -3
- data/spec/internal/app/models/product.rb +4 -0
- data/spec/internal/app/models/product_category.rb +7 -0
- data/spec/internal/db/schema.rb +8 -0
- data/spec/pursuit/search_spec.rb +23 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cf47bc77d2ccf93d20d66ee529c6554a995ee612e3135f90fd7d437f0fa5b08
|
4
|
+
data.tar.gz: 8ac3ace51a32566144c34cbe9ebb33d0c8327327f8ce1d2d8ae120c079a494f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 184fd8c2520cedd0c3ac45e83e4040295de141065419ee676ea159d8a419222a9bac51a950d6e0decb0e7106f8a3839d76f1a3144786647db528b9115d10bb9b
|
7
|
+
data.tar.gz: 65462af7fb13f713f4680845ba297bf659d230d9285b89b2b4c1de1bde2a64402a022c7929298d022f2a9b7a8fa1f4e0bbb7e569fb88cba99e39c01db17f244d
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -26,12 +26,13 @@ class Product < ActiveRecord::Base
|
|
26
26
|
# Attributes can be used for both keyed and unkeyed searching by default, but you can pass either `keyed: false` or
|
27
27
|
# `unkeyed: false` to restrict when the attribute is searched.
|
28
28
|
o.attribute :title
|
29
|
+
o.attribute :description
|
29
30
|
o.attribute :rating, unkeyed: false
|
30
31
|
|
31
32
|
# You can shorten the search keyword by passing the desired search term first, and then the real attribute name
|
32
33
|
# as the second argument.
|
33
|
-
# => "
|
34
|
-
o.attribute :
|
34
|
+
# => "category*=shirts"
|
35
|
+
o.attribute :category, :category_id
|
35
36
|
|
36
37
|
# It's also possible to query entirely custom Arel nodes by passing a block which returns the Arel node to query.
|
37
38
|
# You could use this to query a person's full name by concatenating their first and last name columns, for example.
|
data/lib/pursuit/constants.rb
CHANGED
data/lib/pursuit/search.rb
CHANGED
@@ -51,12 +51,13 @@ module Pursuit
|
|
51
51
|
return nil if terms.blank?
|
52
52
|
|
53
53
|
terms.reduce(nil) do |chain, term|
|
54
|
-
|
54
|
+
attribute_name = term.key.to_sym
|
55
|
+
reflection = options.relations.key?(attribute_name) ? options.record_class.reflections[term.key] : nil
|
55
56
|
node = if reflection.present?
|
56
|
-
attribute_names = options.relations[
|
57
|
+
attribute_names = options.relations[attribute_name]
|
57
58
|
build_arel_for_reflection(reflection, attribute_names, term.operator, term.value)
|
58
59
|
else
|
59
|
-
node_builder = options.keyed_attributes[
|
60
|
+
node_builder = options.keyed_attributes[attribute_name]
|
60
61
|
build_arel_for_node(node_builder.call, term.operator, term.value)
|
61
62
|
end
|
62
63
|
|
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Product < ActiveRecord::Base
|
4
|
+
belongs_to :category, class_name: 'ProductCategory', inverse_of: :products, optional: true
|
5
|
+
|
4
6
|
has_many :variations, class_name: 'ProductVariation', inverse_of: :product
|
5
7
|
|
6
8
|
validates :title, presence: true
|
@@ -16,5 +18,7 @@ class Product < ActiveRecord::Base
|
|
16
18
|
arel_table[:title]
|
17
19
|
])
|
18
20
|
end
|
21
|
+
|
22
|
+
o.attribute :category, :category_id
|
19
23
|
end
|
20
24
|
end
|
data/spec/internal/db/schema.rb
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
ActiveRecord::Schema.define do
|
4
|
+
create_table :product_categories, id: :string, force: true do |t|
|
5
|
+
t.string :name, null: false
|
6
|
+
|
7
|
+
t.timestamps null: false
|
8
|
+
end
|
9
|
+
|
4
10
|
create_table :products, force: true do |t|
|
11
|
+
t.belongs_to :category, type: :string, foreign_key: { to_table: 'product_categories' }
|
12
|
+
|
5
13
|
t.string :title, null: false
|
6
14
|
|
7
15
|
t.text :description
|
data/spec/pursuit/search_spec.rb
CHANGED
@@ -15,6 +15,8 @@ RSpec.describe Pursuit::Search do
|
|
15
15
|
Product.arel_table[:title]
|
16
16
|
])
|
17
17
|
end
|
18
|
+
|
19
|
+
o.attribute :category, :category_id
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
@@ -462,5 +464,26 @@ RSpec.describe Pursuit::Search do
|
|
462
464
|
expect(perform).to contain_exactly(product_a)
|
463
465
|
end
|
464
466
|
end
|
467
|
+
|
468
|
+
context 'when querying a custom attribute whose name matches a reflection' do
|
469
|
+
let(:query) { 'category==shirts' }
|
470
|
+
|
471
|
+
let(:shirts_category) { ProductCategory.create!(id: 'shirts', name: 'The Shirt Collection') }
|
472
|
+
let(:socks_category) { ProductCategory.create!(id: 'socks', name: 'The Sock Collection') }
|
473
|
+
|
474
|
+
let(:product_a) { Product.create!(title: 'Plain Shirt', category: shirts_category) }
|
475
|
+
let(:product_b) { Product.create!(title: 'Funky Shirt', category: shirts_category) }
|
476
|
+
let(:product_c) { Product.create!(title: 'Socks - Pack of 4', category: socks_category) }
|
477
|
+
|
478
|
+
before do
|
479
|
+
product_a
|
480
|
+
product_b
|
481
|
+
product_c
|
482
|
+
end
|
483
|
+
|
484
|
+
it 'is expected to contain the matching records' do
|
485
|
+
expect(perform).to contain_exactly(product_a, product_b)
|
486
|
+
end
|
487
|
+
end
|
465
488
|
end
|
466
489
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pursuit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nialto Services
|
@@ -207,6 +207,7 @@ files:
|
|
207
207
|
- lib/pursuit/search_term_parser.rb
|
208
208
|
- pursuit.gemspec
|
209
209
|
- spec/internal/app/models/product.rb
|
210
|
+
- spec/internal/app/models/product_category.rb
|
210
211
|
- spec/internal/app/models/product_variation.rb
|
211
212
|
- spec/internal/config/database.yml
|
212
213
|
- spec/internal/db/schema.rb
|
@@ -246,6 +247,7 @@ specification_version: 4
|
|
246
247
|
summary: Advanced key-based searching for ActiveRecord objects.
|
247
248
|
test_files:
|
248
249
|
- spec/internal/app/models/product.rb
|
250
|
+
- spec/internal/app/models/product_category.rb
|
249
251
|
- spec/internal/app/models/product_variation.rb
|
250
252
|
- spec/internal/config/database.yml
|
251
253
|
- spec/internal/db/schema.rb
|