pursuit 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c5d86e6be4355b96bdaf0d8c02ea680e5cf0f6c3c9f5fdcd3d8e9a20785aad8b
4
- data.tar.gz: 310009d659879f97ae8bc9947594533fdb0c11f20702b2bf10e1003c072b4425
3
+ metadata.gz: 5cf47bc77d2ccf93d20d66ee529c6554a995ee612e3135f90fd7d437f0fa5b08
4
+ data.tar.gz: 8ac3ace51a32566144c34cbe9ebb33d0c8327327f8ce1d2d8ae120c079a494f8
5
5
  SHA512:
6
- metadata.gz: 49991dd50ed278c76d9dbc7f0dd8f48a7f41fa7905f284964a27df9bc90a55ad6833a5911b86ed159d4d4c88fc2aac736ac5feb2c1b8469fb9adbf02abca3f49
7
- data.tar.gz: 6663dc11f0f6e6adc08f87834d303ed59e0269415132d7c0fdb54d96ee7ca926be69097dd26dd58d0bc836b9a9bf7f8ccfc35fd3e8f9c6976340e52b0719c5b1
6
+ metadata.gz: 184fd8c2520cedd0c3ac45e83e4040295de141065419ee676ea159d8a419222a9bac51a950d6e0decb0e7106f8a3839d76f1a3144786647db528b9115d10bb9b
7
+ data.tar.gz: 65462af7fb13f713f4680845ba297bf659d230d9285b89b2b4c1de1bde2a64402a022c7929298d022f2a9b7a8fa1f4e0bbb7e569fb88cba99e39c01db17f244d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pursuit (0.3.0)
4
+ pursuit (0.3.2)
5
5
  activerecord (>= 5.2.0, < 6.2.0)
6
6
  activesupport (>= 5.2.0, < 6.2.0)
7
7
 
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
- # => "desc*=foo"
34
- o.attribute :desc, :description
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.
@@ -3,5 +3,5 @@
3
3
  module Pursuit
4
4
  # @return [String] The gem's semantic version number.
5
5
  #
6
- VERSION = '0.3.1'
6
+ VERSION = '0.3.2'
7
7
  end
@@ -51,12 +51,13 @@ module Pursuit
51
51
  return nil if terms.blank?
52
52
 
53
53
  terms.reduce(nil) do |chain, term|
54
- reflection = options.record_class.reflections[term.key]
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[term.key.to_sym]
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[term.key.to_sym]
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
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ProductCategory < ActiveRecord::Base
4
+ has_many :products, class_name: 'Product', foreign_key: :category_id, inverse_of: :category, dependent: :nullify
5
+
6
+ validates :name, presence: true
7
+ end
@@ -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
@@ -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.1
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