pursuit 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9584065901bd2b27fce1df13ee13586151c96be3e04f222b81c6ee93d8f078a4
4
- data.tar.gz: 81f233b5d39526129b8bdf7c7bba997e9a5b964b9bd5024bc3e3351f1105c927
3
+ metadata.gz: c5d86e6be4355b96bdaf0d8c02ea680e5cf0f6c3c9f5fdcd3d8e9a20785aad8b
4
+ data.tar.gz: 310009d659879f97ae8bc9947594533fdb0c11f20702b2bf10e1003c072b4425
5
5
  SHA512:
6
- metadata.gz: 83fcec1d5657de087a5a5fdf71b131e34632fc02328cd66d2a551c438201f9125fd8d0861d239a96a4b6b36c6ac3bde342efaa4b610a1689646540229fe456bd
7
- data.tar.gz: 47d981997a463f5203b3316410d6f5e2395fc1aa448329a86a625a732c2751a3e281b4581973cb9893f3c76d68f6999f38a0fbe0020c56ecc608f99b968101b0
6
+ metadata.gz: 49991dd50ed278c76d9dbc7f0dd8f48a7f41fa7905f284964a27df9bc90a55ad6833a5911b86ed159d4d4c88fc2aac736ac5feb2c1b8469fb9adbf02abca3f49
7
+ data.tar.gz: 6663dc11f0f6e6adc08f87834d303ed59e0269415132d7c0fdb54d96ee7ca926be69097dd26dd58d0bc836b9a9bf7f8ccfc35fd3e8f9c6976340e52b0719c5b1
data/README.md CHANGED
@@ -26,10 +26,15 @@ 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
30
29
  o.attribute :rating, unkeyed: false
31
30
 
32
- # You can also create virtual attributes to search by passing in a block that returns an arel node.
31
+ # You can shorten the search keyword by passing the desired search term first, and then the real attribute name
32
+ # as the second argument.
33
+ # => "desc*=foo"
34
+ o.attribute :desc, :description
35
+
36
+ # It's also possible to query entirely custom Arel nodes by passing a block which returns the Arel node to query.
37
+ # You could use this to query a person's full name by concatenating their first and last name columns, for example.
33
38
  o.attribute :title_length, unkeyed: false do
34
39
  Arel::Nodes::NamedFunction.new('LENGTH', [
35
40
  arel_table[:title]
@@ -3,5 +3,5 @@
3
3
  module Pursuit
4
4
  # @return [String] The gem's semantic version number.
5
5
  #
6
- VERSION = '0.3.0'
6
+ VERSION = '0.3.1'
7
7
  end
@@ -54,7 +54,7 @@ module Pursuit
54
54
  keys.map(&:to_s).uniq
55
55
  end
56
56
 
57
- # Add a relation to the search options.
57
+ # Add a relation to search.
58
58
  #
59
59
  # @param name [Symbol] The name of the relationship attribute.
60
60
  # @param attribute_names [Splat] The name of the attributes within the relationship to search.
@@ -64,15 +64,22 @@ module Pursuit
64
64
  nil
65
65
  end
66
66
 
67
- # Add a keyed attribute to search.
67
+ # Add an attribute to search.
68
68
  #
69
- # @param name [Symbol] The name of the attribute.
70
- # @param keyed [Boolean] `true` when the attribute should be searchable using a keyed term, `false` otherwise.
71
- # @param unkeyed [Boolean] `true` when the attribute should be searchable using an unkeyed term, `false` otherwise.
72
- # @param block [Proc] A block which returns an Arel node to query, instead of the matching table column.
69
+ # @param term_name [Symbol] The keyed search term (can be an existing attribute, or a custom value when
70
+ # passing either the `attribute_name` or a block returning an Arel node).
71
+ # @param attribute_name [Symbol] The attribute name to search (defaults to the keyword, when left blank and no
72
+ # block is passed).
73
+ # @param keyed [Boolean] `true` when the attribute should be searchable using a keyed term,
74
+ # `false` otherwise.
75
+ # @param unkeyed [Boolean] `true` when the attribute should be searchable using an unkeyed term,
76
+ # `false` otherwise.
77
+ # @param block [Proc] A block which returns the Arel node to query against. When left blank, the
78
+ # matching attribute from `.arel_table` is queried instead.
73
79
  #
74
- def attribute(name, keyed: true, unkeyed: true, &block)
75
- attributes[name] = AttributeOptions.new(keyed, unkeyed, block || -> { record_class.arel_table[name] })
80
+ def attribute(term_name, attribute_name = nil, keyed: true, unkeyed: true, &block)
81
+ block ||= -> { record_class.arel_table[attribute_name || term_name] }
82
+ attributes[term_name] = AttributeOptions.new(keyed, unkeyed, block)
76
83
  nil
77
84
  end
78
85
  end
@@ -104,11 +104,20 @@ RSpec.describe Pursuit::SearchOptions do
104
104
  expect(search_options.attributes[:description].unkeyed).to eq(true)
105
105
  end
106
106
 
107
- it 'is expected to use the matching table column node builder by default' do
107
+ it 'is expected to query the #term_name attribute' do
108
108
  attribute
109
109
  expect(search_options.attributes[:description].block.call).to eq(Product.arel_table[:description])
110
110
  end
111
111
 
112
+ context 'when passing the attribute name to search' do
113
+ subject(:attribute) { search_options.attribute(:desc, :description) }
114
+
115
+ it 'is expected to query the #attribute_name attribute' do
116
+ attribute
117
+ expect(search_options.attributes[:desc].block.call).to eq(Product.arel_table[:description])
118
+ end
119
+ end
120
+
112
121
  context 'when passing :keyed eq false' do
113
122
  subject(:attribute) { search_options.attribute(:description, keyed: false) }
114
123
 
@@ -130,7 +139,7 @@ RSpec.describe Pursuit::SearchOptions do
130
139
  context 'when passing a block' do
131
140
  subject(:attribute) { search_options.attribute(:description, &title_length_node_builder) }
132
141
 
133
- it 'is expected to use the custom node builder' do
142
+ it 'is expected to query the result of the passed block' do
134
143
  attribute
135
144
  expect(search_options.attributes[:description].block).to eq(title_length_node_builder)
136
145
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pursuit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nialto Services
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-19 00:00:00.000000000 Z
11
+ date: 2021-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord