trailblazer-finder 0.1.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.rubocop-https---raw-githubusercontent-com-trailblazer-meta-master-rubocop-yml +101 -0
- data/.rubocop.yml +3 -43
- data/.rubocop_todo.yml +42 -196
- data/.travis.yml +5 -8
- data/Gemfile +2 -2
- data/LICENSE.txt +7 -21
- data/README.md +177 -292
- data/Rakefile +12 -12
- data/lib/trailblazer/finder.rb +48 -23
- data/lib/trailblazer/finder/activity/find.rb +57 -0
- data/lib/trailblazer/finder/activity/prepare.rb +29 -0
- data/lib/trailblazer/finder/activity/prepare/adapters.rb +66 -0
- data/lib/trailblazer/finder/activity/prepare/entity.rb +32 -0
- data/lib/trailblazer/finder/activity/prepare/filters.rb +36 -0
- data/lib/trailblazer/finder/activity/prepare/paging.rb +49 -0
- data/lib/trailblazer/finder/activity/prepare/params.rb +33 -0
- data/lib/trailblazer/finder/activity/prepare/properties.rb +47 -0
- data/lib/trailblazer/finder/activity/prepare/sorting.rb +57 -0
- data/lib/trailblazer/finder/activity/process.rb +18 -0
- data/lib/trailblazer/finder/activity/process/adapters.rb +48 -0
- data/lib/trailblazer/finder/activity/process/filters.rb +28 -0
- data/lib/trailblazer/finder/activity/process/paging.rb +26 -0
- data/lib/trailblazer/finder/activity/process/predicates.rb +30 -0
- data/lib/trailblazer/finder/activity/process/sorting.rb +26 -0
- data/lib/trailblazer/finder/adapters/active_record/paging.rb +9 -8
- data/lib/trailblazer/finder/adapters/active_record/predicates.rb +78 -17
- data/lib/trailblazer/finder/adapters/active_record/sorting.rb +14 -12
- data/lib/trailblazer/finder/adapters/basic/paging.rb +21 -0
- data/lib/trailblazer/finder/adapters/basic/predicates.rb +115 -0
- data/lib/trailblazer/finder/adapters/basic/sorting.rb +40 -0
- data/lib/trailblazer/finder/adapters/kaminari/paging.rb +21 -0
- data/lib/trailblazer/finder/adapters/sequel/paging.rb +9 -8
- data/lib/trailblazer/finder/adapters/sequel/predicates.rb +78 -17
- data/lib/trailblazer/finder/adapters/sequel/sorting.rb +17 -15
- data/lib/trailblazer/finder/adapters/will_paginate/paging.rb +21 -0
- data/lib/trailblazer/finder/base.rb +20 -83
- data/lib/trailblazer/finder/dsl.rb +56 -0
- data/lib/trailblazer/finder/find.rb +28 -15
- data/lib/trailblazer/finder/helpers/basic.rb +44 -0
- data/lib/trailblazer/finder/helpers/sorting.rb +54 -0
- data/lib/trailblazer/finder/utils/array.rb +20 -0
- data/lib/trailblazer/finder/utils/extra.rb +10 -20
- data/lib/trailblazer/finder/utils/hash.rb +36 -0
- data/lib/trailblazer/finder/utils/splitter.rb +16 -17
- data/lib/trailblazer/finder/utils/string.rb +20 -10
- data/lib/trailblazer/finder/version.rb +3 -1
- data/lib/trailblazer/operation/finder.rb +20 -19
- data/spec/spec_helper.rb +5 -11
- data/spec/spec_helper_active_record.rb +11 -12
- data/spec/spec_helper_sequel.rb +4 -4
- data/spec/spec_helper_will_paginate.rb +13 -0
- data/spec/trailblazer/finder/adapters/active_record_spec.rb +267 -0
- data/spec/trailblazer/finder/adapters/basic_spec.rb +251 -0
- data/spec/trailblazer/finder/adapters/kaminari_spec.rb +78 -32
- data/spec/trailblazer/finder/adapters/sequel_spec.rb +267 -0
- data/spec/trailblazer/finder/adapters/will_paginate_spec.rb +81 -40
- data/spec/trailblazer/finder/base_spec.rb +292 -204
- data/spec/trailblazer/finder/dsl_spec.rb +213 -0
- data/spec/trailblazer/finder/utils/hash_spec.rb +26 -0
- data/spec/trailblazer/finder/utils/splitter_spec.rb +39 -0
- data/spec/trailblazer/finder/utils/string_spec.rb +52 -9
- data/spec/trailblazer/operation/finder_spec.rb +35 -32
- data/trailblazer-finder.gemspec +31 -30
- metadata +119 -137
- data/lib/trailblazer/finder/adapters.rb +0 -26
- data/lib/trailblazer/finder/adapters/active_record.rb +0 -32
- data/lib/trailblazer/finder/adapters/data_mapper.rb +0 -32
- data/lib/trailblazer/finder/adapters/data_mapper/paging.rb +0 -20
- data/lib/trailblazer/finder/adapters/data_mapper/predicates.rb +0 -42
- data/lib/trailblazer/finder/adapters/data_mapper/sorting.rb +0 -38
- data/lib/trailblazer/finder/adapters/friendly_id.rb +0 -31
- data/lib/trailblazer/finder/adapters/kaminari.rb +0 -18
- data/lib/trailblazer/finder/adapters/sequel.rb +0 -32
- data/lib/trailblazer/finder/adapters/will_paginate.rb +0 -18
- data/lib/trailblazer/finder/errors/block_ignored.rb +0 -11
- data/lib/trailblazer/finder/errors/invalid_defined_by_value.rb +0 -11
- data/lib/trailblazer/finder/errors/invalid_number.rb +0 -16
- data/lib/trailblazer/finder/errors/missing_entity_type.rb +0 -11
- data/lib/trailblazer/finder/errors/with_ignored.rb +0 -11
- data/lib/trailblazer/finder/features.rb +0 -23
- data/lib/trailblazer/finder/features/paging.rb +0 -55
- data/lib/trailblazer/finder/features/predicate.rb +0 -32
- data/lib/trailblazer/finder/features/sorting.rb +0 -108
- data/lib/trailblazer/finder/filter.rb +0 -66
- data/lib/trailblazer/finder/predicates.rb +0 -39
- data/lib/trailblazer/finder/utils/deep_locate.rb +0 -30
- data/lib/trailblazer/finder/utils/params.rb +0 -28
- data/lib/trailblazer/finder/utils/parse.rb +0 -25
- data/spec/spec_helper_data_mapper.rb +0 -35
- data/spec/support/paging_shared_example.rb +0 -65
- data/spec/support/sorting_shared_example.rb +0 -125
- data/spec/trailblazer/finder/adapters/active_record/base_spec.rb +0 -112
- data/spec/trailblazer/finder/adapters/active_record/paging_spec.rb +0 -64
- data/spec/trailblazer/finder/adapters/active_record/predicates_spec.rb +0 -101
- data/spec/trailblazer/finder/adapters/active_record/sorting_spec.rb +0 -99
- data/spec/trailblazer/finder/adapters/data_mapper/base_spec.rb +0 -112
- data/spec/trailblazer/finder/adapters/data_mapper/paging_spec.rb +0 -64
- data/spec/trailblazer/finder/adapters/data_mapper/predicates_spec.rb +0 -100
- data/spec/trailblazer/finder/adapters/data_mapper/sorting_spec.rb +0 -97
- data/spec/trailblazer/finder/adapters/friendly_id_spec.rb +0 -46
- data/spec/trailblazer/finder/adapters/sequel/base_spec.rb +0 -125
- data/spec/trailblazer/finder/adapters/sequel/paging_spec.rb +0 -64
- data/spec/trailblazer/finder/adapters/sequel/predicates_spec.rb +0 -89
- data/spec/trailblazer/finder/adapters/sequel/sorting_spec.rb +0 -110
- data/spec/trailblazer/finder/adapters_spec.rb +0 -110
- data/spec/trailblazer/finder/features/paging_spec.rb +0 -104
- data/spec/trailblazer/finder/features/predicates_spec.rb +0 -99
- data/spec/trailblazer/finder/features/sorting_spec.rb +0 -96
- data/spec/trailblazer/finder/features_spec.rb +0 -55
- data/spec/trailblazer/finder/filter_spec.rb +0 -133
- data/spec/trailblazer/finder/find_spec.rb +0 -72
- data/spec/trailblazer/finder/utils/extra_spec.rb +0 -41
- data/spec/trailblazer/finder/utils/params_spec.rb +0 -39
- data/spec/trailblazer/finder/utils/parse_spec.rb +0 -33
- data/spec/trailblazer/operation/paging_spec.rb +0 -68
- data/spec/trailblazer/operation/predicates_spec.rb +0 -127
- data/spec/trailblazer/operation/sorting_spec.rb +0 -80
- data/spec/trailblazer/test_spec.rb +0 -41
@@ -1,68 +0,0 @@
|
|
1
|
-
require 'delegate'
|
2
|
-
require 'trailblazer'
|
3
|
-
require 'spec_helper_active_record'
|
4
|
-
|
5
|
-
class Product::PagingFinderNoEntity < Trailblazer::Finder
|
6
|
-
features Paging
|
7
|
-
|
8
|
-
adapters ActiveRecord
|
9
|
-
|
10
|
-
per_page 2
|
11
|
-
min_per_page 2
|
12
|
-
max_per_page 10
|
13
|
-
|
14
|
-
filter_by :id
|
15
|
-
filter_by :name
|
16
|
-
end
|
17
|
-
|
18
|
-
class Product::PagingFinderWithEntity < Trailblazer::Finder
|
19
|
-
features Paging
|
20
|
-
|
21
|
-
adapters ActiveRecord
|
22
|
-
|
23
|
-
entity_type { Product }
|
24
|
-
|
25
|
-
per_page 2
|
26
|
-
min_per_page 2
|
27
|
-
max_per_page 10
|
28
|
-
|
29
|
-
filter_by :id
|
30
|
-
filter_by :name
|
31
|
-
end
|
32
|
-
|
33
|
-
class Product::PagingIndex < Trailblazer::Operation
|
34
|
-
step Finder(Product::PagingFinderNoEntity, :all, Product)
|
35
|
-
end
|
36
|
-
|
37
|
-
class Product::PagingIndexNoEntity < Trailblazer::Operation
|
38
|
-
step Finder(Product::PagingFinderWithEntity, :all)
|
39
|
-
end
|
40
|
-
|
41
|
-
describe 'Trailblazer::Operation - Finder Macro - Paging' do
|
42
|
-
before do
|
43
|
-
Product.destroy_all
|
44
|
-
Product.reset_pk_sequence
|
45
|
-
20.times { |i| Product.create name: "product_#{i}" }
|
46
|
-
end
|
47
|
-
|
48
|
-
after do
|
49
|
-
Product.destroy_all
|
50
|
-
Product.reset_pk_sequence
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'Can paginate results' do
|
54
|
-
params = { page: 2 }
|
55
|
-
result = Product::PagingIndex.call(params: params)
|
56
|
-
|
57
|
-
expect(result[:finder].count).to eq 20
|
58
|
-
expect(result[:finder].results).to eq Product.limit(2).offset(2)
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'Can paginate results when no entity type is given in macro' do
|
62
|
-
params = { page: 2 }
|
63
|
-
result = Product::PagingIndexNoEntity.call(params: params)
|
64
|
-
|
65
|
-
expect(result[:finder].count).to eq 20
|
66
|
-
expect(result[:finder].results).to eq Product.limit(2).offset(2)
|
67
|
-
end
|
68
|
-
end
|
@@ -1,127 +0,0 @@
|
|
1
|
-
require 'delegate'
|
2
|
-
require 'trailblazer'
|
3
|
-
require 'spec_helper_active_record'
|
4
|
-
|
5
|
-
class Product::PredicateFinderNoEntity < Trailblazer::Finder
|
6
|
-
features Predicate
|
7
|
-
adapters ActiveRecord
|
8
|
-
|
9
|
-
predicates_for :name, :price, :created_at
|
10
|
-
|
11
|
-
filter_by(:category) { |entity_type, _| entity_type.joins(:category) }
|
12
|
-
end
|
13
|
-
|
14
|
-
class Product::PredicateFinderWithEntity < Trailblazer::Finder
|
15
|
-
features Predicate
|
16
|
-
adapters ActiveRecord
|
17
|
-
|
18
|
-
entity_type { Product }
|
19
|
-
|
20
|
-
predicates_for :name, :price, :created_at
|
21
|
-
|
22
|
-
filter_by(:category) { |entity_type, _| entity_type.joins(:category) }
|
23
|
-
end
|
24
|
-
|
25
|
-
class Product::PredicateIndex < Trailblazer::Operation
|
26
|
-
step Finder(Product::PredicateFinderNoEntity, :all, Product)
|
27
|
-
end
|
28
|
-
|
29
|
-
class Product::PredicateIndexNoEntity < Trailblazer::Operation
|
30
|
-
step Finder(Product::PredicateFinderWithEntity, :all)
|
31
|
-
end
|
32
|
-
|
33
|
-
describe 'Trailblazer::Operation - Finder Macro - Sorting' do
|
34
|
-
before do
|
35
|
-
Product.destroy_all
|
36
|
-
Product.reset_pk_sequence
|
37
|
-
Product.delete_all
|
38
|
-
10.times do |i|
|
39
|
-
next Product.create name: "", price: "1#{i}" if i == 7
|
40
|
-
next Product.create name: nil, price: "1#{i}" if i == 8
|
41
|
-
next Product.create name: "product_4", price: "1#{i}" if i == 9
|
42
|
-
Product.create name: "product_#{i}", price: "1#{i}"
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
after do
|
47
|
-
Product.destroy_all
|
48
|
-
Product.reset_pk_sequence
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'it finds single row with name equal to product_5' do
|
52
|
-
params = { f: { name_eq: 'product_5' } }
|
53
|
-
result = Product::PredicateIndex.call(params: params)
|
54
|
-
|
55
|
-
expect(result[:finder].results.first.name).to eq "product_5"
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'it finds multiple rows with name equal to product_4' do
|
59
|
-
params = { f: { name_eq: 'product_4' } }
|
60
|
-
result = Product::PredicateIndex.call(params: params)
|
61
|
-
|
62
|
-
expect(result[:finder].results.first.price).to eq 14
|
63
|
-
expect(result[:finder].results.last.price).to eq 19
|
64
|
-
end
|
65
|
-
|
66
|
-
it 'it finds rows with name not equal to product_4' do
|
67
|
-
params = { f: { name_not_eq: 'product_4' } }
|
68
|
-
result = Product::PredicateIndex.call(params: params)
|
69
|
-
|
70
|
-
expect(result[:finder].results.map(&:name)).to eq ["product_0", "product_1", "product_2", "product_3", "product_5", "product_6", ""]
|
71
|
-
expect(result[:finder].results.map(&:price)).to eq [10, 11, 12, 13, 15, 16, 17]
|
72
|
-
end
|
73
|
-
|
74
|
-
it 'it finds multiple rows with name blank (empty/nil)' do
|
75
|
-
params = { f: { name_blank: true } }
|
76
|
-
result = Product::PredicateIndex.call(params: params)
|
77
|
-
|
78
|
-
expect(result[:finder].results.map(&:price)).to eq [17, 18]
|
79
|
-
end
|
80
|
-
|
81
|
-
it 'it finds multiple rows with name not blank (empty/nil)' do
|
82
|
-
params = { f: { name_not_blank: true } }
|
83
|
-
result = Product::PredicateIndex.call(params: params)
|
84
|
-
|
85
|
-
expect(result[:finder].results.map(&:name)).to eq ["product_0", "product_1", "product_2", "product_3", "product_4", "product_5", "product_6", "product_4"]
|
86
|
-
end
|
87
|
-
|
88
|
-
it 'it finds multiple rows with price less than 15' do
|
89
|
-
params = { f: { price_lt: 15 } }
|
90
|
-
result = Product::PredicateIndex.call(params: params)
|
91
|
-
|
92
|
-
expect(result[:finder].results.map(&:name)).to eq ["product_0", "product_1", "product_2", "product_3", "product_4"]
|
93
|
-
expect(result[:finder].results.map(&:price)).to eq [10, 11, 12, 13, 14]
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'it finds multiple rows with price equal to and less than 15' do
|
97
|
-
params = { f: { price_lte: 15 } }
|
98
|
-
result = Product::PredicateIndex.call(params: params)
|
99
|
-
|
100
|
-
expect(result[:finder].results.map(&:name)).to eq ["product_0", "product_1", "product_2", "product_3", "product_4", "product_5"]
|
101
|
-
expect(result[:finder].results.map(&:price)).to eq [10, 11, 12, 13, 14, 15]
|
102
|
-
end
|
103
|
-
|
104
|
-
it 'it finds multiple rows with price greater than 15' do
|
105
|
-
params = { f: { price_gt: 15 } }
|
106
|
-
result = Product::PredicateIndex.call(params: params)
|
107
|
-
|
108
|
-
expect(result[:finder].results.map(&:name)).to eq ["product_6", "", nil, "product_4"]
|
109
|
-
expect(result[:finder].results.map(&:price)).to eq [16, 17, 18, 19]
|
110
|
-
end
|
111
|
-
|
112
|
-
it 'it finds multiple rows with price equal to and greater than 15' do
|
113
|
-
params = { f: { price_gte: 15 } }
|
114
|
-
result = Product::PredicateIndex.call(params: params)
|
115
|
-
|
116
|
-
expect(result[:finder].results.map(&:name)).to eq ["product_5", "product_6", "", nil, "product_4"]
|
117
|
-
expect(result[:finder].results.map(&:price)).to eq [15, 16, 17, 18, 19]
|
118
|
-
end
|
119
|
-
|
120
|
-
it 'it finds rows with name not equal to product_4 and name not blank and price greater than 14' do
|
121
|
-
params = { f: { name_not_eq: 'product_4', name_not_blank: true, price_gt: 14 } }
|
122
|
-
result = Product::PredicateIndex.call(params: params)
|
123
|
-
|
124
|
-
expect(result[:finder].results.map(&:name)).to eq ["product_5", "product_6"]
|
125
|
-
expect(result[:finder].results.map(&:price)).to eq [15, 16]
|
126
|
-
end
|
127
|
-
end
|
@@ -1,80 +0,0 @@
|
|
1
|
-
require 'delegate'
|
2
|
-
require 'trailblazer'
|
3
|
-
require 'spec_helper_active_record'
|
4
|
-
|
5
|
-
class Product::SortFinderNoEntity < Trailblazer::Finder
|
6
|
-
features Sorting
|
7
|
-
|
8
|
-
adapters ActiveRecord
|
9
|
-
|
10
|
-
sortable_by :id, :name
|
11
|
-
|
12
|
-
filter_by :id
|
13
|
-
filter_by :name
|
14
|
-
end
|
15
|
-
|
16
|
-
class Product::SortFinderWithEntity < Trailblazer::Finder
|
17
|
-
features Sorting
|
18
|
-
|
19
|
-
adapters ActiveRecord
|
20
|
-
|
21
|
-
entity_type { Product }
|
22
|
-
|
23
|
-
sortable_by :id, :name
|
24
|
-
|
25
|
-
filter_by :id
|
26
|
-
filter_by :name
|
27
|
-
end
|
28
|
-
|
29
|
-
class Product::SortIndex < Trailblazer::Operation
|
30
|
-
step Finder(Product::SortFinderNoEntity, :all, Product)
|
31
|
-
end
|
32
|
-
|
33
|
-
class Product::SortIndexNoEntity < Trailblazer::Operation
|
34
|
-
step Finder(Product::SortFinderWithEntity, :all)
|
35
|
-
end
|
36
|
-
|
37
|
-
describe 'Trailblazer::Operation - Finder Macro - Sorting' do
|
38
|
-
before do
|
39
|
-
Product.destroy_all
|
40
|
-
Product.reset_pk_sequence
|
41
|
-
20.times { |i| Product.create name: "product_#{i}" }
|
42
|
-
end
|
43
|
-
|
44
|
-
after do
|
45
|
-
Product.destroy_all
|
46
|
-
Product.reset_pk_sequence
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'Can sort results by name descending' do
|
50
|
-
params = { f: { sort: 'name desc' } }
|
51
|
-
result = Product::SortIndex.call(params: params)
|
52
|
-
|
53
|
-
expect(result[:finder].count).to eq 20
|
54
|
-
expect(result[:finder].results.last.name).to eq 'product_0'
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'Can sort results by name descending when no entity type is given in macro' do
|
58
|
-
params = { f: { sort: 'name desc' } }
|
59
|
-
result = Product::SortIndexNoEntity.call(params: params)
|
60
|
-
|
61
|
-
expect(result[:finder].count).to eq 20
|
62
|
-
expect(result[:finder].results.last.name).to eq 'product_0'
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'Can sort results by id ascending' do
|
66
|
-
params = { f: { sort: 'id asc' } }
|
67
|
-
result = Product::SortIndex.call(params: params)
|
68
|
-
|
69
|
-
expect(result[:finder].count).to eq 20
|
70
|
-
expect(result[:finder].results.last.name).to eq 'product_19'
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'Can sort results by id ascending when no entity type is given in macro' do
|
74
|
-
params = { f: { sort: 'id asc' } }
|
75
|
-
result = Product::SortIndexNoEntity.call(params: params)
|
76
|
-
|
77
|
-
expect(result[:finder].count).to eq 20
|
78
|
-
expect(result[:finder].results.last.name).to eq 'product_19'
|
79
|
-
end
|
80
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
# require 'spec_helper_active_record'
|
2
|
-
#
|
3
|
-
# class TestFinder < Trailblazer::Finder
|
4
|
-
# features Predicates
|
5
|
-
# adapters ActiveRecord, FriendlyId
|
6
|
-
#
|
7
|
-
# entity_type { Product }
|
8
|
-
#
|
9
|
-
# predicates_for :name, :price, :created_at
|
10
|
-
#
|
11
|
-
# filter_by(:category) { |entity_type, _| entity_type.joins(:category) }
|
12
|
-
# end
|
13
|
-
#
|
14
|
-
# def finder_with_predicate(filter = nil, value = nil, filters = {})
|
15
|
-
# TestFinder.new filter: { filter => value }.merge(filters)
|
16
|
-
# end
|
17
|
-
#
|
18
|
-
# describe 'eqauls' do
|
19
|
-
# before do
|
20
|
-
# Product.delete_all
|
21
|
-
# 10.times do |i|
|
22
|
-
# Product.create name: "product_#{i}", price: "1#{i}"
|
23
|
-
# Product.create name: "product_4", price: "1#{1}" if i == 5
|
24
|
-
# end
|
25
|
-
# end
|
26
|
-
#
|
27
|
-
# it 'it finds single row with name equal to product_5' do
|
28
|
-
# finder = finder_with_predicate 'name_eq', 'product_5'
|
29
|
-
# expect(finder.results.map(&:price)).to eq [15]
|
30
|
-
# end
|
31
|
-
#
|
32
|
-
# it 'it finds multiple rows with name equal to product_4' do
|
33
|
-
# finder = finder_with_predicate 'name_eq', 'product_4'
|
34
|
-
# expect(finder.results.map(&:price)).to eq [14, 11]
|
35
|
-
# end
|
36
|
-
#
|
37
|
-
# it 'it finds rows with name not equal to product_5' do
|
38
|
-
# finder = finder_with_predicate 'name_not_eq', 'product_4'
|
39
|
-
# expect(finder.results.map(&:name)).to eq ["product_0", "product_1", "product_2", "product_3", "product_5", "product_6", "product_7", "product_8", "product_9"]
|
40
|
-
# end
|
41
|
-
# end
|