trailblazer-finder 0.1.2 → 0.1.3
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 +4 -4
- data/.rubocop_todo.yml +22 -11
- data/.travis.yml +0 -2
- data/CHANGES.md +4 -0
- data/README.md +70 -58
- data/lib/trailblazer/finder/adapters/active_record/sorting.rb +6 -2
- data/lib/trailblazer/finder/adapters/data_mapper.rb +2 -2
- data/lib/trailblazer/finder/adapters/data_mapper/sorting.rb +25 -25
- data/lib/trailblazer/finder/adapters/sequel/sorting.rb +9 -7
- data/lib/trailblazer/finder/base.rb +0 -2
- data/lib/trailblazer/finder/features/predicate.rb +4 -9
- data/lib/trailblazer/finder/features/sorting.rb +70 -28
- data/lib/trailblazer/finder/utils/deep_locate.rb +4 -19
- data/lib/trailblazer/finder/utils/parse.rb +6 -6
- data/lib/trailblazer/finder/version.rb +1 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/spec_helper_active_record.rb +0 -1
- data/spec/support/sorting_shared_example.rb +67 -37
- data/spec/trailblazer/finder/adapters/active_record/predicates_spec.rb +5 -2
- data/spec/trailblazer/finder/adapters/active_record/sorting_spec.rb +93 -80
- data/spec/trailblazer/finder/adapters/data_mapper/base_spec.rb +1 -1
- data/spec/trailblazer/finder/adapters/data_mapper/sorting_spec.rb +85 -85
- data/spec/trailblazer/finder/adapters/sequel/predicates_spec.rb +2 -2
- data/spec/trailblazer/finder/adapters/sequel/sorting_spec.rb +101 -80
- data/spec/trailblazer/finder/features/sorting_spec.rb +59 -62
- data/trailblazer-finder.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2dbad15894d9e56c23ebe4428acfd379f5854f8948e67073ebde8c898814c1cc
|
4
|
+
data.tar.gz: f265dd84d1b25458fa630823c7c526628017d79e786e5bb362a043f75d864295
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ae3bf32a2ece475570a67222bd371e5092df9a56bf6a195da185d6071e6877da9dc946ad3b00eed0cdefe357aab85a56b2f50eba3c0e63acc06df8f9c346354
|
7
|
+
data.tar.gz: 3aca9b67986a6c62a8c7173bc0bf34b9238b55046dc537865405f70e75e3de6b98bb72c98e39839fd8338085ce978013380f9ff4903590010cb1ba8b514f6f9d
|
data/.rubocop_todo.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on 2018-03-
|
3
|
+
# on 2018-03-17 03:54:36 +0100 using RuboCop version 0.53.0.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
@@ -18,6 +18,15 @@ Layout/EmptyLines:
|
|
18
18
|
Exclude:
|
19
19
|
- 'spec/trailblazer/operation/finder_spec.rb'
|
20
20
|
|
21
|
+
# Offense count: 2
|
22
|
+
# Cop supports --auto-correct.
|
23
|
+
# Configuration parameters: EnforcedStyle.
|
24
|
+
# SupportedStyles: empty_lines, no_empty_lines
|
25
|
+
Layout/EmptyLinesAroundBlockBody:
|
26
|
+
Exclude:
|
27
|
+
- 'spec/trailblazer/finder/adapters/active_record/sorting_spec.rb'
|
28
|
+
- 'spec/trailblazer/finder/features/sorting_spec.rb'
|
29
|
+
|
21
30
|
# Offense count: 1
|
22
31
|
# Cop supports --auto-correct.
|
23
32
|
# Configuration parameters: EnforcedStyle.
|
@@ -78,13 +87,12 @@ Layout/SpaceInsideParens:
|
|
78
87
|
Exclude:
|
79
88
|
- 'lib/trailblazer/finder/utils/splitter.rb'
|
80
89
|
|
81
|
-
# Offense count:
|
90
|
+
# Offense count: 4
|
82
91
|
# Cop supports --auto-correct.
|
83
92
|
Layout/TrailingWhitespace:
|
84
93
|
Exclude:
|
85
94
|
- 'lib/trailblazer/finder/filter.rb'
|
86
95
|
- 'spec/trailblazer/finder/adapters/active_record/base_spec.rb'
|
87
|
-
- 'spec/trailblazer/finder/adapters/data_mapper/base_spec.rb'
|
88
96
|
- 'spec/trailblazer/finder/adapters/sequel/base_spec.rb'
|
89
97
|
|
90
98
|
# Offense count: 1
|
@@ -99,19 +107,21 @@ Lint/UnusedMethodArgument:
|
|
99
107
|
Exclude:
|
100
108
|
- 'lib/trailblazer/finder/adapters/sequel/predicates.rb'
|
101
109
|
|
102
|
-
# Offense count:
|
110
|
+
# Offense count: 3
|
103
111
|
Metrics/AbcSize:
|
104
|
-
Max:
|
112
|
+
Max: 17
|
105
113
|
|
106
|
-
# Offense count:
|
114
|
+
# Offense count: 9
|
107
115
|
# Configuration parameters: CountComments.
|
108
116
|
Metrics/MethodLength:
|
109
117
|
Max: 51
|
110
118
|
|
111
|
-
# Offense count:
|
119
|
+
# Offense count: 7
|
112
120
|
RSpec/DescribeClass:
|
113
121
|
Exclude:
|
122
|
+
- 'spec/trailblazer/finder/adapters/active_record/sorting_spec.rb'
|
114
123
|
- 'spec/trailblazer/finder/adapters/sequel/predicates_spec.rb'
|
124
|
+
- 'spec/trailblazer/finder/adapters/sequel/sorting_spec.rb'
|
115
125
|
- 'spec/trailblazer/operation/finder_spec.rb'
|
116
126
|
- 'spec/trailblazer/operation/paging_spec.rb'
|
117
127
|
- 'spec/trailblazer/operation/predicates_spec.rb'
|
@@ -170,20 +180,20 @@ Style/PerlBackrefs:
|
|
170
180
|
Exclude:
|
171
181
|
- 'lib/trailblazer/finder/utils/splitter.rb'
|
172
182
|
|
173
|
-
# Offense count:
|
183
|
+
# Offense count: 153
|
174
184
|
# Cop supports --auto-correct.
|
175
185
|
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
|
176
186
|
# SupportedStyles: single_quotes, double_quotes
|
177
187
|
Style/StringLiterals:
|
178
188
|
Exclude:
|
179
189
|
- 'lib/trailblazer/finder/adapters/sequel/predicates.rb'
|
180
|
-
- 'lib/trailblazer/finder/features/predicate.rb'
|
181
190
|
- 'spec/trailblazer/finder/adapters/active_record/predicates_spec.rb'
|
191
|
+
- 'spec/trailblazer/finder/adapters/active_record/sorting_spec.rb'
|
182
192
|
- 'spec/trailblazer/finder/adapters/sequel/predicates_spec.rb'
|
183
193
|
- 'spec/trailblazer/finder/features/predicates_spec.rb'
|
184
194
|
- 'spec/trailblazer/operation/predicates_spec.rb'
|
185
195
|
|
186
|
-
# Offense count:
|
196
|
+
# Offense count: 20
|
187
197
|
# Cop supports --auto-correct.
|
188
198
|
# Configuration parameters: EnforcedStyle, MinSize, WordRegex.
|
189
199
|
# SupportedStyles: percent, brackets
|
@@ -192,9 +202,10 @@ Style/WordArray:
|
|
192
202
|
- 'spec/trailblazer/finder/adapters/active_record/predicates_spec.rb'
|
193
203
|
- 'spec/trailblazer/finder/adapters/sequel/predicates_spec.rb'
|
194
204
|
- 'spec/trailblazer/finder/features/predicates_spec.rb'
|
205
|
+
- 'spec/trailblazer/finder/features/sorting_spec.rb'
|
195
206
|
- 'spec/trailblazer/operation/predicates_spec.rb'
|
196
207
|
|
197
|
-
# Offense count:
|
208
|
+
# Offense count: 174
|
198
209
|
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
199
210
|
# URISchemes: http, https
|
200
211
|
Metrics/LineLength:
|
data/.travis.yml
CHANGED
data/CHANGES.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
# 0.1.3
|
2
|
+
* Added sorting by multiple columns/directions for Hash, ActiveRecord and Sequel. Temporarily disabled sorting for DataMapper Adapter until i can find the time this weekend to update that Adapter as well.
|
3
|
+
* Due to the above, a big change in the way sorting works, and which methods are available for it
|
4
|
+
|
1
5
|
# 0.1.2
|
2
6
|
|
3
7
|
* Predicate feature introduced, doesn't work for old datamapper adapter yet. Default predicates covered for now: eq, not_eq, blank, not_blank, lt, lte, gt, gte
|
data/README.md
CHANGED
@@ -10,37 +10,37 @@ Description should come here
|
|
10
10
|
* [Installation](#installation)
|
11
11
|
* [Dependencies](#dependencies)
|
12
12
|
* [Usage](#usage)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
13
|
+
* [Finder](#finder)
|
14
|
+
* [Finder Example](#finder-example)
|
15
|
+
* [Operation](#operation)
|
16
|
+
* [Operation Example](#operation-example)
|
17
|
+
* [Usable without Trailblazer](#usable-without-trailblazer)
|
18
|
+
* [Example without Trailblazer](#example-without-trailblazer)
|
19
|
+
* [Example Project](#example-project)
|
20
|
+
* [Features](#features)
|
21
21
|
* [Predicate](#predicate)
|
22
22
|
* [Predicate Example](#predicate-example)
|
23
23
|
* [Paging](#paging)
|
24
|
-
|
24
|
+
* [Paging Example](#paging-example)
|
25
25
|
* [Sorting](#sorting)
|
26
|
-
|
27
|
-
|
26
|
+
* [Sorting Example](#sorting-example)
|
27
|
+
* [Adapters](#adapters)
|
28
28
|
* [Adapters Example](#adapters-example)
|
29
29
|
* [ActiveRecord](#active_record)
|
30
|
-
|
30
|
+
* [Active Record Example](#active-record-example)
|
31
31
|
* [DataMapper](#data_mapper)
|
32
|
-
|
32
|
+
* [Data Mapper Example](#data-mapper-example)
|
33
33
|
* [Sequel](#sequel)
|
34
|
-
|
34
|
+
* [Sequel Example](#sequel-example)
|
35
35
|
* [Kaminari](#kaminari)
|
36
|
-
|
36
|
+
* [Kaminari Example](#kaminari-example)
|
37
37
|
* [WillPaginate](#will_paginate)
|
38
|
-
|
38
|
+
* [Will Paginate Example](#will-paginate-example)
|
39
39
|
* [FriendlyId](#friendly_id)
|
40
|
-
|
40
|
+
* [Friendly Id Example](#friendly-id-example)
|
41
41
|
* [Tips & Tricks](#tips--tricks)
|
42
42
|
* [ORM's are not required](#results-shortcut)
|
43
|
-
|
43
|
+
* [Passing Entity Type as Argument](#passing-entity_type-as-argument)
|
44
44
|
* [Contributing](#contributing)
|
45
45
|
* [License](#license)
|
46
46
|
|
@@ -60,7 +60,7 @@ Or install it yourself as:
|
|
60
60
|
|
61
61
|
$ gem install trailblazer-finder
|
62
62
|
|
63
|
-
###
|
63
|
+
### Dependencies
|
64
64
|
* [Trailblazer](https://github.com/trailblazer/trailblazer) - [actually optional, but requires 2.1+](https://github.com/trailblazer/trailblazer-finder#usable-without-trailblazer)
|
65
65
|
|
66
66
|
## Usage
|
@@ -237,6 +237,11 @@ Coming soon!
|
|
237
237
|
## Features
|
238
238
|
Aside of the default filtering behavior, it offers the following optional features as well.
|
239
239
|
|
240
|
+
Just for the record, you can comma seperate features and load multiple by doing
|
241
|
+
```ruby
|
242
|
+
features Sorting, Paging, Predicate
|
243
|
+
```
|
244
|
+
|
240
245
|
NOTE: FEATURES NEED TO BE SPECIFIED ON TOP OF YOUR CLASS
|
241
246
|
|
242
247
|
### Predicate
|
@@ -257,13 +262,13 @@ NOTE: PREDICATES CURRENTLY DO NOT WORK FOR THE OLD DATAMAPPER ADAPTER
|
|
257
262
|
#### Predicate Example
|
258
263
|
```ruby
|
259
264
|
class Post::Finder < Trailblazer::Finder
|
260
|
-
|
265
|
+
features Predicate
|
261
266
|
|
262
|
-
|
263
|
-
|
267
|
+
# Specify the fields you want predicates enabled for, mind you these fields need to exist on your entity_type
|
268
|
+
predicates_for :name, :category_name
|
264
269
|
|
265
270
|
filter_by :name
|
266
|
-
|
271
|
+
filter_by :published
|
267
272
|
filter_by :category_name
|
268
273
|
|
269
274
|
# per page defaults to 25 (so not required)
|
@@ -280,36 +285,36 @@ end
|
|
280
285
|
This feature extends the result[:finder] object with the following methods
|
281
286
|
```ruby
|
282
287
|
# accessing filters
|
283
|
-
.name
|
284
|
-
.created_at
|
288
|
+
.name # => name filter
|
289
|
+
.created_at # => created at filter
|
285
290
|
|
286
291
|
# Predicate filters
|
287
|
-
.name_eq
|
288
|
-
.name_not_eq
|
289
|
-
.name_blank
|
290
|
-
.name_not_blank
|
291
|
-
.name_lt
|
292
|
-
.name_lte
|
293
|
-
.name_gt
|
294
|
-
.name_gte
|
295
|
-
.category_name_eq
|
296
|
-
.category_name_not_eq
|
297
|
-
.category_name_blank
|
298
|
-
.category_name_not_blank
|
299
|
-
.category_name_lt
|
300
|
-
.category_name_lte
|
301
|
-
.category_name_gt
|
302
|
-
.category_name_gte
|
292
|
+
.name_eq # => name equals filter
|
293
|
+
.name_not_eq # => name not equals filter
|
294
|
+
.name_blank # => name blank filter
|
295
|
+
.name_not_blank # => name not blank filter
|
296
|
+
.name_lt # => name less than filter (converts value to float)
|
297
|
+
.name_lte # => name less than or equal to filter (converts value to float)
|
298
|
+
.name_gt # => name greater than filter (converts value to float)
|
299
|
+
.name_gte # => name greater than or equal to filter (converts value to float)
|
300
|
+
.category_name_eq # => category name equals filter
|
301
|
+
.category_name_not_eq # => category name not equals filter
|
302
|
+
.category_name_blank # => category name blank filter
|
303
|
+
.category_name_not_blank # => category name not blank filter
|
304
|
+
.category_name_lt # => category name less than filter (converts value to float)
|
305
|
+
.category_name_lte # => category name less than or equal to filter (converts value to float)
|
306
|
+
.category_name_gt # => category name greater than filter (converts value to float)
|
307
|
+
.category_name_gte # => category name greater than or equal to filter (converts value to float)
|
303
308
|
|
304
309
|
# accessing results
|
305
|
-
.count
|
306
|
-
.results?
|
307
|
-
.results
|
308
|
-
.all
|
310
|
+
.count # => number of found results
|
311
|
+
.results? # => are there any results found
|
312
|
+
.results # => fetched results
|
313
|
+
.all # => if needed, use it to get dataset (sequel for example requires you use it in some cases)
|
309
314
|
|
310
315
|
# params for url generations
|
311
|
-
.params
|
312
|
-
.params published: false
|
316
|
+
.params # => filter values
|
317
|
+
.params published: false # => overwrites the 'published' filter
|
313
318
|
```
|
314
319
|
|
315
320
|
### Paging
|
@@ -318,7 +323,7 @@ Really simple pagination feature, which uses the plain ```.limit``` and ```.offs
|
|
318
323
|
#### Paging Example
|
319
324
|
```ruby
|
320
325
|
class Post::Finder < Trailblazer::Finder
|
321
|
-
|
326
|
+
features Paging
|
322
327
|
|
323
328
|
filter_by :name
|
324
329
|
filter_by :category_name
|
@@ -342,12 +347,14 @@ This feature extends the result[:finder] object with the following methods
|
|
342
347
|
```
|
343
348
|
|
344
349
|
### Sorting
|
345
|
-
Really simple sorting feature, fixing the pain of dealing with sorting attributes and directions.
|
350
|
+
Really simple sorting feature, fixing the pain of dealing with sorting attributes and directions. Can sort by multiple columns/directions.
|
351
|
+
|
352
|
+
NOTE: AS OF 0.1.3 TEMPORARY NOT WORKING FOR DATAMAPPER ADAPTER, WE ARE WORKING ON FIXING IT
|
346
353
|
|
347
354
|
#### Sorting Example
|
348
355
|
```ruby
|
349
356
|
class Post::Finder < Trailblazer::Finder
|
350
|
-
|
357
|
+
features Sorting
|
351
358
|
|
352
359
|
sortable_by :name, :body
|
353
360
|
end
|
@@ -356,21 +363,26 @@ end
|
|
356
363
|
This feature extends the result[:finder] object with the following methods
|
357
364
|
```ruby
|
358
365
|
.results # => Posts sorted by title DESC
|
359
|
-
.sort_attribute # => 'title'
|
360
|
-
.sort_direction # => 'desc'
|
361
366
|
|
362
367
|
# Smart sort checking
|
363
368
|
.sort?('title') # => true
|
364
|
-
.sort?('title desc') # => true
|
365
|
-
.sort?('title asc') # => false
|
366
369
|
|
367
|
-
# Helpers for
|
368
|
-
.reverted_sort_direction # => 'asc'
|
370
|
+
# Helpers for seeing current sort direction
|
369
371
|
.sort_direction_for('title') # => 'asc'
|
370
|
-
.sort_direction_for('body')
|
372
|
+
.sort_direction_for('body') # => 'desc'
|
371
373
|
|
372
|
-
#
|
374
|
+
# Helpers for seeing reversing sort direction
|
375
|
+
.reverse_sort_direction_for('title') # => 'desc'
|
376
|
+
.reverse_sort_direction_for('body') # => 'asc'
|
377
|
+
|
378
|
+
# Params for sorting links (new if none exists, existing params if exists)
|
373
379
|
.sort_params_for('title')
|
380
|
+
|
381
|
+
# Add Params for sorting links (add to existing / replace with different direction)
|
382
|
+
.add_sort_params_for('title')
|
383
|
+
|
384
|
+
# New Params for sorting links (reset)
|
385
|
+
.new_sort_params_for('title')
|
374
386
|
```
|
375
387
|
|
376
388
|
## Adapters
|
@@ -10,8 +10,12 @@ module Trailblazer
|
|
10
10
|
|
11
11
|
private
|
12
12
|
|
13
|
-
def sort_it(entity_type,
|
14
|
-
entity_type.order
|
13
|
+
def sort_it(entity_type, sort_attributes)
|
14
|
+
entity_type.order(sort_attributes)
|
15
|
+
end
|
16
|
+
|
17
|
+
def sort_orders(sort_attr, sort_dir)
|
18
|
+
{ sort_attr.to_s => sort_dir }
|
15
19
|
end
|
16
20
|
end
|
17
21
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'trailblazer/finder/adapters/data_mapper/paging'
|
2
|
-
require 'trailblazer/finder/adapters/data_mapper/sorting'
|
2
|
+
# require 'trailblazer/finder/adapters/data_mapper/sorting'
|
3
3
|
|
4
4
|
module Trailblazer
|
5
5
|
class Finder
|
@@ -23,7 +23,7 @@ module Trailblazer
|
|
23
23
|
end
|
24
24
|
|
25
25
|
include Paging if defined?(Features::Paging::ClassMethods)
|
26
|
-
include Sorting if defined?(Features::Sorting::ClassMethods)
|
26
|
+
# include Sorting if defined?(Features::Sorting::ClassMethods)
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
@@ -1,25 +1,25 @@
|
|
1
|
-
module Trailblazer
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
1
|
+
# module Trailblazer
|
2
|
+
# class Finder
|
3
|
+
# module Adapters
|
4
|
+
# module DataMapper
|
5
|
+
# # Sequel - Sorting Adapter
|
6
|
+
# module Sorting
|
7
|
+
# def self.included(base)
|
8
|
+
# base.extend Features::Sorting::ClassMethods
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# private
|
12
|
+
#
|
13
|
+
# def sort_it(entity_type, sort_attribute, sort_direction)
|
14
|
+
# case sort_direction
|
15
|
+
# when 'asc', 'ascending'
|
16
|
+
# entity_type.all(order: [sort_attribute.to_sym.asc])
|
17
|
+
# when 'desc', 'descending'
|
18
|
+
# entity_type.all(order: [sort_attribute.to_sym.desc])
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
# end
|
@@ -10,13 +10,15 @@ module Trailblazer
|
|
10
10
|
|
11
11
|
private
|
12
12
|
|
13
|
-
def
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
13
|
+
def sort_orders(sort_attr, sort_dir)
|
14
|
+
::Sequel.send sort_dir, sort_attr.to_sym
|
15
|
+
end
|
16
|
+
|
17
|
+
def sort_it(entity_type, sort_attributes)
|
18
|
+
result = []
|
19
|
+
result << [:order, sort_attributes.first] if sort_attributes.is_a? Array
|
20
|
+
sort_attributes.drop(1).each { |x| result << [:order_append, x] }
|
21
|
+
result.inject(entity_type) { |obj, method_and_args| obj.send(*method_and_args) }
|
20
22
|
end
|
21
23
|
end
|
22
24
|
end
|