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.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop-https---raw-githubusercontent-com-trailblazer-meta-master-rubocop-yml +101 -0
  4. data/.rubocop.yml +3 -43
  5. data/.rubocop_todo.yml +42 -196
  6. data/.travis.yml +5 -8
  7. data/Gemfile +2 -2
  8. data/LICENSE.txt +7 -21
  9. data/README.md +177 -292
  10. data/Rakefile +12 -12
  11. data/lib/trailblazer/finder.rb +48 -23
  12. data/lib/trailblazer/finder/activity/find.rb +57 -0
  13. data/lib/trailblazer/finder/activity/prepare.rb +29 -0
  14. data/lib/trailblazer/finder/activity/prepare/adapters.rb +66 -0
  15. data/lib/trailblazer/finder/activity/prepare/entity.rb +32 -0
  16. data/lib/trailblazer/finder/activity/prepare/filters.rb +36 -0
  17. data/lib/trailblazer/finder/activity/prepare/paging.rb +49 -0
  18. data/lib/trailblazer/finder/activity/prepare/params.rb +33 -0
  19. data/lib/trailblazer/finder/activity/prepare/properties.rb +47 -0
  20. data/lib/trailblazer/finder/activity/prepare/sorting.rb +57 -0
  21. data/lib/trailblazer/finder/activity/process.rb +18 -0
  22. data/lib/trailblazer/finder/activity/process/adapters.rb +48 -0
  23. data/lib/trailblazer/finder/activity/process/filters.rb +28 -0
  24. data/lib/trailblazer/finder/activity/process/paging.rb +26 -0
  25. data/lib/trailblazer/finder/activity/process/predicates.rb +30 -0
  26. data/lib/trailblazer/finder/activity/process/sorting.rb +26 -0
  27. data/lib/trailblazer/finder/adapters/active_record/paging.rb +9 -8
  28. data/lib/trailblazer/finder/adapters/active_record/predicates.rb +78 -17
  29. data/lib/trailblazer/finder/adapters/active_record/sorting.rb +14 -12
  30. data/lib/trailblazer/finder/adapters/basic/paging.rb +21 -0
  31. data/lib/trailblazer/finder/adapters/basic/predicates.rb +115 -0
  32. data/lib/trailblazer/finder/adapters/basic/sorting.rb +40 -0
  33. data/lib/trailblazer/finder/adapters/kaminari/paging.rb +21 -0
  34. data/lib/trailblazer/finder/adapters/sequel/paging.rb +9 -8
  35. data/lib/trailblazer/finder/adapters/sequel/predicates.rb +78 -17
  36. data/lib/trailblazer/finder/adapters/sequel/sorting.rb +17 -15
  37. data/lib/trailblazer/finder/adapters/will_paginate/paging.rb +21 -0
  38. data/lib/trailblazer/finder/base.rb +20 -83
  39. data/lib/trailblazer/finder/dsl.rb +56 -0
  40. data/lib/trailblazer/finder/find.rb +28 -15
  41. data/lib/trailblazer/finder/helpers/basic.rb +44 -0
  42. data/lib/trailblazer/finder/helpers/sorting.rb +54 -0
  43. data/lib/trailblazer/finder/utils/array.rb +20 -0
  44. data/lib/trailblazer/finder/utils/extra.rb +10 -20
  45. data/lib/trailblazer/finder/utils/hash.rb +36 -0
  46. data/lib/trailblazer/finder/utils/splitter.rb +16 -17
  47. data/lib/trailblazer/finder/utils/string.rb +20 -10
  48. data/lib/trailblazer/finder/version.rb +3 -1
  49. data/lib/trailblazer/operation/finder.rb +20 -19
  50. data/spec/spec_helper.rb +5 -11
  51. data/spec/spec_helper_active_record.rb +11 -12
  52. data/spec/spec_helper_sequel.rb +4 -4
  53. data/spec/spec_helper_will_paginate.rb +13 -0
  54. data/spec/trailblazer/finder/adapters/active_record_spec.rb +267 -0
  55. data/spec/trailblazer/finder/adapters/basic_spec.rb +251 -0
  56. data/spec/trailblazer/finder/adapters/kaminari_spec.rb +78 -32
  57. data/spec/trailblazer/finder/adapters/sequel_spec.rb +267 -0
  58. data/spec/trailblazer/finder/adapters/will_paginate_spec.rb +81 -40
  59. data/spec/trailblazer/finder/base_spec.rb +292 -204
  60. data/spec/trailblazer/finder/dsl_spec.rb +213 -0
  61. data/spec/trailblazer/finder/utils/hash_spec.rb +26 -0
  62. data/spec/trailblazer/finder/utils/splitter_spec.rb +39 -0
  63. data/spec/trailblazer/finder/utils/string_spec.rb +52 -9
  64. data/spec/trailblazer/operation/finder_spec.rb +35 -32
  65. data/trailblazer-finder.gemspec +31 -30
  66. metadata +119 -137
  67. data/lib/trailblazer/finder/adapters.rb +0 -26
  68. data/lib/trailblazer/finder/adapters/active_record.rb +0 -32
  69. data/lib/trailblazer/finder/adapters/data_mapper.rb +0 -32
  70. data/lib/trailblazer/finder/adapters/data_mapper/paging.rb +0 -20
  71. data/lib/trailblazer/finder/adapters/data_mapper/predicates.rb +0 -42
  72. data/lib/trailblazer/finder/adapters/data_mapper/sorting.rb +0 -38
  73. data/lib/trailblazer/finder/adapters/friendly_id.rb +0 -31
  74. data/lib/trailblazer/finder/adapters/kaminari.rb +0 -18
  75. data/lib/trailblazer/finder/adapters/sequel.rb +0 -32
  76. data/lib/trailblazer/finder/adapters/will_paginate.rb +0 -18
  77. data/lib/trailblazer/finder/errors/block_ignored.rb +0 -11
  78. data/lib/trailblazer/finder/errors/invalid_defined_by_value.rb +0 -11
  79. data/lib/trailblazer/finder/errors/invalid_number.rb +0 -16
  80. data/lib/trailblazer/finder/errors/missing_entity_type.rb +0 -11
  81. data/lib/trailblazer/finder/errors/with_ignored.rb +0 -11
  82. data/lib/trailblazer/finder/features.rb +0 -23
  83. data/lib/trailblazer/finder/features/paging.rb +0 -55
  84. data/lib/trailblazer/finder/features/predicate.rb +0 -32
  85. data/lib/trailblazer/finder/features/sorting.rb +0 -108
  86. data/lib/trailblazer/finder/filter.rb +0 -66
  87. data/lib/trailblazer/finder/predicates.rb +0 -39
  88. data/lib/trailblazer/finder/utils/deep_locate.rb +0 -30
  89. data/lib/trailblazer/finder/utils/params.rb +0 -28
  90. data/lib/trailblazer/finder/utils/parse.rb +0 -25
  91. data/spec/spec_helper_data_mapper.rb +0 -35
  92. data/spec/support/paging_shared_example.rb +0 -65
  93. data/spec/support/sorting_shared_example.rb +0 -125
  94. data/spec/trailblazer/finder/adapters/active_record/base_spec.rb +0 -112
  95. data/spec/trailblazer/finder/adapters/active_record/paging_spec.rb +0 -64
  96. data/spec/trailblazer/finder/adapters/active_record/predicates_spec.rb +0 -101
  97. data/spec/trailblazer/finder/adapters/active_record/sorting_spec.rb +0 -99
  98. data/spec/trailblazer/finder/adapters/data_mapper/base_spec.rb +0 -112
  99. data/spec/trailblazer/finder/adapters/data_mapper/paging_spec.rb +0 -64
  100. data/spec/trailblazer/finder/adapters/data_mapper/predicates_spec.rb +0 -100
  101. data/spec/trailblazer/finder/adapters/data_mapper/sorting_spec.rb +0 -97
  102. data/spec/trailblazer/finder/adapters/friendly_id_spec.rb +0 -46
  103. data/spec/trailblazer/finder/adapters/sequel/base_spec.rb +0 -125
  104. data/spec/trailblazer/finder/adapters/sequel/paging_spec.rb +0 -64
  105. data/spec/trailblazer/finder/adapters/sequel/predicates_spec.rb +0 -89
  106. data/spec/trailblazer/finder/adapters/sequel/sorting_spec.rb +0 -110
  107. data/spec/trailblazer/finder/adapters_spec.rb +0 -110
  108. data/spec/trailblazer/finder/features/paging_spec.rb +0 -104
  109. data/spec/trailblazer/finder/features/predicates_spec.rb +0 -99
  110. data/spec/trailblazer/finder/features/sorting_spec.rb +0 -96
  111. data/spec/trailblazer/finder/features_spec.rb +0 -55
  112. data/spec/trailblazer/finder/filter_spec.rb +0 -133
  113. data/spec/trailblazer/finder/find_spec.rb +0 -72
  114. data/spec/trailblazer/finder/utils/extra_spec.rb +0 -41
  115. data/spec/trailblazer/finder/utils/params_spec.rb +0 -39
  116. data/spec/trailblazer/finder/utils/parse_spec.rb +0 -33
  117. data/spec/trailblazer/operation/paging_spec.rb +0 -68
  118. data/spec/trailblazer/operation/predicates_spec.rb +0 -127
  119. data/spec/trailblazer/operation/sorting_spec.rb +0 -80
  120. 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