talent_scout 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -7
- data/lib/talent_scout/controller.rb +3 -3
- data/lib/talent_scout/model_search.rb +68 -38
- data/lib/talent_scout/version.rb +1 -1
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8762e9b2380fffc1760108d9519e9d6e936aa5a0372c0b4a15e426fdf002dde
|
4
|
+
data.tar.gz: fa0c26db874aea1c7a1fddcb0b8270bca03ba604d240c42e961ab3d7d2d21f49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b0d2cd202e33572ca6a24c4235f4e7a36f0975361ba09ed870e03a758c89cfafe07ba544d5e3737a1432535d466993b50383df57fa8ee5f8d7039f77b2acad7
|
7
|
+
data.tar.gz: 19e4fe92e92aa8c0b69151034adf430739bee6e538e8878bcb7606d7fb9738b3f1a2f259ce264b64388bf972853c877703c95d72536a9dfc4d6e38c54db18fbf
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# talent_scout
|
1
|
+
# talent_scout [![Build Status](https://travis-ci.org/jonathanhefner/talent_scout.svg?branch=master)](https://travis-ci.org/jonathanhefner/talent_scout)
|
2
2
|
|
3
3
|
Model-backed searches in Rails. A whiz-bang example:
|
4
4
|
|
@@ -42,10 +42,16 @@ end
|
|
42
42
|
```html+erb
|
43
43
|
<!-- app/views/posts/index.html.erb -->
|
44
44
|
|
45
|
-
<%= form_with model: @search, method: :get do |form| %>
|
45
|
+
<%= form_with model: @search, local: true, method: :get do |form| %>
|
46
|
+
<%= form.label :title_includes %>
|
46
47
|
<%= form.text_field :title_includes %>
|
48
|
+
|
49
|
+
<%= form.label :within %>
|
47
50
|
<%= form.select :within, @search.each_choice(:within), include_blank: true %>
|
51
|
+
|
52
|
+
<%= form.label :only_published %>
|
48
53
|
<%= form.check_box :only_published %>
|
54
|
+
|
49
55
|
<%= form.submit %>
|
50
56
|
<% end %>
|
51
57
|
|
@@ -519,15 +525,21 @@ Search forms can be rendered using Rails' form builder and a search
|
|
519
525
|
object:
|
520
526
|
|
521
527
|
```html+erb
|
522
|
-
<%= form_with model: @search, method: :get do |form| %>
|
528
|
+
<%= form_with model: @search, local: true, method: :get do |form| %>
|
529
|
+
<%= form.label :title_includes %>
|
523
530
|
<%= form.text_field :title_includes %>
|
531
|
+
|
532
|
+
<%= form.label :created_on %>
|
524
533
|
<%= form.date_field :created_on %>
|
534
|
+
|
535
|
+
<%= form.label :only_published %>
|
525
536
|
<%= form.check_box :only_published %>
|
537
|
+
|
526
538
|
<%= form.submit %>
|
527
539
|
<% end %>
|
528
540
|
```
|
529
541
|
|
530
|
-
Notice the `method: :get` argument to `form_with`; this is
|
542
|
+
Notice the `method: :get` argument to `form_with`; **this is required**.
|
531
543
|
|
532
544
|
Form fields will be populated with the criteria input (or default)
|
533
545
|
values of the same name from `@search`. Type-appropriate form fields
|
@@ -646,7 +658,7 @@ end
|
|
646
658
|
```
|
647
659
|
|
648
660
|
```html+erb
|
649
|
-
<%= form_with model: @search, method: :get do |form| %>
|
661
|
+
<%= form_with model: @search, local: true, method: :get do |form| %>
|
650
662
|
<%= form.select :category, @search.each_choice(:category) %>
|
651
663
|
<%= form.submit %>
|
652
664
|
<% end %>
|
@@ -668,7 +680,7 @@ end
|
|
668
680
|
```
|
669
681
|
|
670
682
|
```html+erb
|
671
|
-
<%= form_with model: @search, method: :get do |form| %>
|
683
|
+
<%= form_with model: @search, local: true, method: :get do |form| %>
|
672
684
|
<%= form.select :order, @search.each_choice(:order) %>
|
673
685
|
<%= form.submit %>
|
674
686
|
<% end %>
|
@@ -707,7 +719,6 @@ end
|
|
707
719
|
|
708
720
|
Remember that only one order can be applied at a time, so only one value
|
709
721
|
in the Hash, at most, will be non-`nil`.
|
710
|
-
>>>>>>> b0d201c... Update README
|
711
722
|
|
712
723
|
|
713
724
|
## Installation
|
@@ -4,7 +4,7 @@ module TalentScout
|
|
4
4
|
|
5
5
|
module ClassMethods
|
6
6
|
# Returns the controller model search class. Defaults to a class
|
7
|
-
# corresponding to the singular
|
7
|
+
# corresponding to the singular form of the controller name. The
|
8
8
|
# model search class can also be set with {model_search_class=}.
|
9
9
|
# If the model search class has not been set, and the default
|
10
10
|
# class does not exist, a +NameError+ will be raised.
|
@@ -32,8 +32,8 @@ module TalentScout
|
|
32
32
|
end
|
33
33
|
|
34
34
|
# Similar to {model_search_class}, but returns nil instead of
|
35
|
-
# raising an error when the value has not been set
|
36
|
-
#
|
35
|
+
# raising an error when the value has not been set and the default
|
36
|
+
# class does not exist.
|
37
37
|
#
|
38
38
|
# @return [Class<TalentScout::ModelSearch>, nil]
|
39
39
|
def model_search_class?
|
@@ -47,11 +47,11 @@ module TalentScout
|
|
47
47
|
@model_name ||= ModelName.new(self)
|
48
48
|
end
|
49
49
|
|
50
|
-
# Sets the default scope of the search. Like
|
50
|
+
# Sets the default scope of the search. Like Active Record's
|
51
51
|
# +default_scope+, the scope here is specified as a block which is
|
52
52
|
# evaluated in the context of the {model_class}. Also like
|
53
|
-
#
|
54
|
-
#
|
53
|
+
# Active Record, multiple calls of this method will append to the
|
54
|
+
# default scope.
|
55
55
|
#
|
56
56
|
# @example
|
57
57
|
# class PostSearch < TalentScout::ModelSearch
|
@@ -88,26 +88,26 @@ module TalentScout
|
|
88
88
|
# Model (e.g. +:string+, +:boolean+, +:integer+, etc), with the
|
89
89
|
# addition of a +:void+ type. A +:void+ type is just like a
|
90
90
|
# +:boolean+ type, except that the criteria is not evaluated when
|
91
|
-
# the
|
91
|
+
# the typecasted value is falsey.
|
92
92
|
#
|
93
|
-
# Alternatively, instead of a type, an
|
93
|
+
# Alternatively, instead of a type, an Array or Hash of +choices+
|
94
94
|
# can be specified, and the criteria will be evaluated only if the
|
95
95
|
# passed-in value matches one of the choices.
|
96
96
|
#
|
97
|
-
# Active Model +attribute_options+ can also be specified
|
98
|
-
# notably +:default+
|
99
|
-
# operate on.
|
97
|
+
# Active Model +attribute_options+ can also be specified. Most
|
98
|
+
# notably, the +:default+ option provides the criteria a default
|
99
|
+
# value to operate on.
|
100
100
|
#
|
101
|
-
# Each criteria can specify a block which recieves
|
102
|
-
#
|
103
|
-
# not set on the search object
|
101
|
+
# Each criteria can specify a block which recieves its corresponding
|
102
|
+
# typecasted value as an argument. If the corresponding value is
|
103
|
+
# not set on the search object, and no default value is defined,
|
104
104
|
# the criteria will not be evaluated. Like an Active Record
|
105
105
|
# +scope+ block, a criteria block is evaluated in the context of an
|
106
|
-
# +ActiveRecord::Relation
|
106
|
+
# +ActiveRecord::Relation+, and should return an
|
107
107
|
# +ActiveRecord::Relation+. A criteria block may also return nil,
|
108
108
|
# in which case the criteria will be skipped. If no criteria block
|
109
109
|
# is specified, the criteria will be evaluated as a +where+ clause
|
110
|
-
# using the criteria name and
|
110
|
+
# using the criteria name and typecasted value.
|
111
111
|
#
|
112
112
|
# As a convenient shorthand, Active Record scopes which have been
|
113
113
|
# defined on the {model_class} can be used directly as criteria
|
@@ -171,7 +171,7 @@ module TalentScout
|
|
171
171
|
# PostSearch.new(only_edited: "1").results # == Post.where("modified_at > created_at")
|
172
172
|
#
|
173
173
|
#
|
174
|
-
# @example Specifying choices (
|
174
|
+
# @example Specifying choices (Array)
|
175
175
|
# class PostSearch < TalentScout::ModelSearch
|
176
176
|
# criteria :category, choices: %w[science tech engineering math]
|
177
177
|
# end
|
@@ -180,7 +180,7 @@ module TalentScout
|
|
180
180
|
# PostSearch.new(category: "BLAH").results # == Post.all
|
181
181
|
#
|
182
182
|
#
|
183
|
-
# @example Specifying choices (
|
183
|
+
# @example Specifying choices (Hash)
|
184
184
|
# class PostSearch < TalentScout::ModelSearch
|
185
185
|
# criteria :within, choices: {
|
186
186
|
# "Last 24 hours" => 24.hours,
|
@@ -209,15 +209,38 @@ module TalentScout
|
|
209
209
|
# PostSearch.new(within_days: 2).results # == Post.where("created_at >= ?", 2.days.ago)
|
210
210
|
#
|
211
211
|
#
|
212
|
-
# @
|
213
|
-
#
|
214
|
-
#
|
215
|
-
#
|
216
|
-
#
|
217
|
-
#
|
212
|
+
# @overload criteria(names, type = :string, **attribute_options, &block)
|
213
|
+
# @param names [String, Symbol, Array<String>, Array<Symbol>]
|
214
|
+
# @param type [Symbol, ActiveModel::Type]
|
215
|
+
# @param attribute_options [Hash]
|
216
|
+
# @option attribute_options :default [Object]
|
217
|
+
# @yieldparam value [Object]
|
218
|
+
# @yieldreturn [ActiveRecord::Relation, nil]
|
219
|
+
#
|
220
|
+
# @overload criteria(names, type = :string, **attribute_options)
|
221
|
+
# @param names [String, Symbol, Array<String>, Array<Symbol>]
|
222
|
+
# @param type [Symbol, ActiveModel::Type]
|
223
|
+
# @param attribute_options [Hash]
|
224
|
+
# @option attribute_options :default [Object]
|
225
|
+
#
|
226
|
+
# @overload criteria(names, choices:, **attribute_options, &block)
|
227
|
+
# @param names [String, Symbol, Array<String>, Array<Symbol>]
|
228
|
+
# @param choices [Array<String>, Array<Symbol>, Hash<String, Object>, Hash<Symbol, Object>]
|
229
|
+
# @param attribute_options [Hash]
|
230
|
+
# @option attribute_options :default [Object]
|
231
|
+
# @yieldparam value [Object]
|
232
|
+
# @yieldreturn [ActiveRecord::Relation, nil]
|
233
|
+
#
|
234
|
+
# @overload criteria(names, choices:, **attribute_options)
|
235
|
+
# @param names [String, Symbol, Array<String>, Array<Symbol>]
|
236
|
+
# @param choices [Array<String>, Array<Symbol>, Hash<String, Object>, Hash<Symbol, Object>]
|
237
|
+
# @param attribute_options [Hash]
|
238
|
+
# @option attribute_options :default [Object]
|
239
|
+
# @yieldreturn [ActiveRecord::Relation, nil]
|
240
|
+
#
|
218
241
|
# @return [void]
|
219
242
|
# @raise [ArgumentError]
|
220
|
-
# if +choices+
|
243
|
+
# if +choices+ is specified and +type+ is not +:string+
|
221
244
|
def self.criteria(names, type = :string, choices: nil, **attribute_options, &block)
|
222
245
|
if choices
|
223
246
|
if type != :string
|
@@ -269,7 +292,7 @@ module TalentScout
|
|
269
292
|
# +:default+ option in the order definition. (Note that only one
|
270
293
|
# order can be designated as the default order.)
|
271
294
|
#
|
272
|
-
#
|
295
|
+
# @see toggle_order
|
273
296
|
#
|
274
297
|
#
|
275
298
|
# @example Single-column order
|
@@ -346,25 +369,30 @@ module TalentScout
|
|
346
369
|
order_type.add_definition(definition)
|
347
370
|
end
|
348
371
|
|
349
|
-
# Initializes a +ModelSearch+ instance. Assigns values
|
350
|
-
# to
|
372
|
+
# Initializes a +ModelSearch+ instance. Assigns values from
|
373
|
+
# +params+ to corresponding criteria attributes.
|
351
374
|
#
|
352
375
|
# If +params+ is a +ActionController::Parameters+, blank values are
|
353
376
|
# ignored. This behavior prevents empty search form fields from
|
354
377
|
# affecting search results.
|
355
378
|
#
|
356
379
|
# @param params [Hash<String, Object>, Hash<Symbol, Object>, ActionController::Parameters]
|
380
|
+
# @raise [ActiveModel::UnknownAttributeError]
|
381
|
+
# if +params+ is a Hash, and it contains an unrecognized key
|
357
382
|
def initialize(params = {})
|
383
|
+
# HACK initialize ActiveRecord state required by ActiveRecord::AttributeMethods::BeforeTypeCast
|
384
|
+
@transaction_state ||= nil
|
385
|
+
|
358
386
|
if params.is_a?(ActionController::Parameters)
|
359
387
|
params = params.permit(self.class.attribute_types.keys).reject!{|key, value| value.blank? }
|
360
388
|
end
|
361
389
|
super(params)
|
362
390
|
end
|
363
391
|
|
364
|
-
# Applies search {criteria} with set or default
|
365
|
-
# and the set or default {order}
|
366
|
-
# Returns an +ActiveRecord::Relation+, allowing
|
367
|
-
# as pagination, to be applied post-hoc.
|
392
|
+
# Applies the {default_scope}, search {criteria} with set or default
|
393
|
+
# attribute values, and the set or default {order} to the
|
394
|
+
# {model_class}. Returns an +ActiveRecord::Relation+, allowing
|
395
|
+
# further scopes, such as pagination, to be applied post-hoc.
|
368
396
|
#
|
369
397
|
# @example
|
370
398
|
# class PostSearch < TalentScout::ModelSearch
|
@@ -383,14 +411,15 @@ module TalentScout
|
|
383
411
|
#
|
384
412
|
# @return [ActiveRecord::Relation]
|
385
413
|
def results
|
386
|
-
self.class.criteria_list.reduce(self.class.model_class) do |scope, crit|
|
414
|
+
self.class.criteria_list.reduce(self.class.model_class.all) do |scope, crit|
|
387
415
|
crit.apply(scope, attribute_set)
|
388
416
|
end
|
389
417
|
end
|
390
418
|
|
391
419
|
# Builds a new model search object with +criteria_values+ merged on
|
392
|
-
# top of the subject search object's criteria values.
|
393
|
-
#
|
420
|
+
# top of the subject search object's criteria values.
|
421
|
+
#
|
422
|
+
# Does not modify the subject search object.
|
394
423
|
#
|
395
424
|
# @example
|
396
425
|
# class PostSearch < TalentScout::ModelSearch
|
@@ -414,8 +443,9 @@ module TalentScout
|
|
414
443
|
|
415
444
|
# Builds a new model search object with the subject search object's
|
416
445
|
# criteria values, excluding values specified by +criteria_names+.
|
417
|
-
# Default criteria values will still be applied.
|
418
|
-
#
|
446
|
+
# Default criteria values will still be applied.
|
447
|
+
#
|
448
|
+
# Does not modify the subject search object.
|
419
449
|
#
|
420
450
|
# @example
|
421
451
|
# class PostSearch < TalentScout::ModelSearch
|
@@ -479,10 +509,10 @@ module TalentScout
|
|
479
509
|
|
480
510
|
# Iterates over a specified {criteria}'s defined choices. If the
|
481
511
|
# given block accepts a 2nd argument, a boolean will be passed
|
482
|
-
# indicating whether that choice is currently
|
483
|
-
#
|
484
|
-
# returned.
|
512
|
+
# indicating whether that choice is currently assigned to the
|
513
|
+
# specified criteria.
|
485
514
|
#
|
515
|
+
# An Enumerator is returned if no block is given.
|
486
516
|
#
|
487
517
|
# @example With block
|
488
518
|
# class PostSearch < TalentScout::ModelSearch
|
@@ -549,7 +579,7 @@ module TalentScout
|
|
549
579
|
# {order}. Each key's associated value indicates that order's
|
550
580
|
# currently applied direction -- +:asc+, +:desc+, or +nil+ if the
|
551
581
|
# order is not applied. Note that only one order can be applied at
|
552
|
-
# a time, so
|
582
|
+
# a time, so, at most, one value in the Hash will be non-+nil+.
|
553
583
|
#
|
554
584
|
# @example
|
555
585
|
# class PostSearch < TalentScout::ModelSearch
|
data/lib/talent_scout/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: talent_scout
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Hefner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -58,6 +58,20 @@ dependencies:
|
|
58
58
|
- - "<"
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '4.0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: webdrivers
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
61
75
|
- !ruby/object:Gem::Dependency
|
62
76
|
name: yard
|
63
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -88,7 +102,7 @@ dependencies:
|
|
88
102
|
version: '4.0'
|
89
103
|
description:
|
90
104
|
email:
|
91
|
-
- jonathan
|
105
|
+
- jonathan@hefner.pro
|
92
106
|
executables: []
|
93
107
|
extensions: []
|
94
108
|
extra_rdoc_files: []
|