trailblazer-finder 0.1.3 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2dbad15894d9e56c23ebe4428acfd379f5854f8948e67073ebde8c898814c1cc
4
- data.tar.gz: f265dd84d1b25458fa630823c7c526628017d79e786e5bb362a043f75d864295
3
+ metadata.gz: 60587025c9bddf5a856868fa908615064b1b5a683c187d9606670e00ceb999f4
4
+ data.tar.gz: d110b70e65eef74d6cba432740c4df30b2449cd338c9a933e1c82a2b3bb4e9ef
5
5
  SHA512:
6
- metadata.gz: 2ae3bf32a2ece475570a67222bd371e5092df9a56bf6a195da185d6071e6877da9dc946ad3b00eed0cdefe357aab85a56b2f50eba3c0e63acc06df8f9c346354
7
- data.tar.gz: 3aca9b67986a6c62a8c7173bc0bf34b9238b55046dc537865405f70e75e3de6b98bb72c98e39839fd8338085ce978013380f9ff4903590010cb1ba8b514f6f9d
6
+ metadata.gz: 9ab8dc1682c4a9968af5a442a27ff1be21a98b78c87776cbd477e809ade202bc28732d0bb00fbc6857f107c8a3cb296dd99ffd192557b7de04ba35c24f08ccb1
7
+ data.tar.gz: 5f5b69d3715e35f3350509fd8b32613e7b39c219049c3411bd95cda17a8f0ad2675dce7ee51dc412bfdbe159a7fb87885c18accbc3847efae2c6ed41009cae5e
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2018-03-17 03:54:36 +0100 using RuboCop version 0.53.0.
3
+ # on 2018-03-17 16:23:14 +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
@@ -35,6 +35,14 @@ Layout/EmptyLinesAroundModuleBody:
35
35
  Exclude:
36
36
  - 'lib/trailblazer/finder/adapters/sequel/predicates.rb'
37
37
 
38
+ # Offense count: 2
39
+ # Configuration parameters: EnforcedStyle.
40
+ # SupportedStyles: native, lf, crlf
41
+ Layout/EndOfLine:
42
+ Exclude:
43
+ - 'lib/trailblazer/finder/adapters/data_mapper/predicates.rb'
44
+ - 'spec/trailblazer/finder/adapters/data_mapper/predicates_spec.rb'
45
+
38
46
  # Offense count: 1
39
47
  # Cop supports --auto-correct.
40
48
  # Configuration parameters: EnforcedStyle.
@@ -87,19 +95,23 @@ Layout/SpaceInsideParens:
87
95
  Exclude:
88
96
  - 'lib/trailblazer/finder/utils/splitter.rb'
89
97
 
90
- # Offense count: 4
98
+ # Offense count: 3
91
99
  # Cop supports --auto-correct.
92
100
  Layout/TrailingWhitespace:
93
101
  Exclude:
94
102
  - 'lib/trailblazer/finder/filter.rb'
95
103
  - 'spec/trailblazer/finder/adapters/active_record/base_spec.rb'
96
- - 'spec/trailblazer/finder/adapters/sequel/base_spec.rb'
97
104
 
98
105
  # Offense count: 1
99
106
  Lint/DuplicateMethods:
100
107
  Exclude:
101
108
  - 'lib/trailblazer/finder/utils/splitter.rb'
102
109
 
110
+ # Offense count: 1
111
+ Lint/NestedMethodDefinition:
112
+ Exclude:
113
+ - 'spec/trailblazer/finder/adapters/sequel/base_spec.rb'
114
+
103
115
  # Offense count: 2
104
116
  # Cop supports --auto-correct.
105
117
  # Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods.
@@ -116,10 +128,11 @@ Metrics/AbcSize:
116
128
  Metrics/MethodLength:
117
129
  Max: 51
118
130
 
119
- # Offense count: 7
131
+ # Offense count: 8
120
132
  RSpec/DescribeClass:
121
133
  Exclude:
122
134
  - 'spec/trailblazer/finder/adapters/active_record/sorting_spec.rb'
135
+ - 'spec/trailblazer/finder/adapters/data_mapper/sorting_spec.rb'
123
136
  - 'spec/trailblazer/finder/adapters/sequel/predicates_spec.rb'
124
137
  - 'spec/trailblazer/finder/adapters/sequel/sorting_spec.rb'
125
138
  - 'spec/trailblazer/operation/finder_spec.rb'
@@ -127,11 +140,12 @@ RSpec/DescribeClass:
127
140
  - 'spec/trailblazer/operation/predicates_spec.rb'
128
141
  - 'spec/trailblazer/operation/sorting_spec.rb'
129
142
 
130
- # Offense count: 40
143
+ # Offense count: 50
131
144
  # Configuration parameters: CustomTransform, IgnoredWords.
132
145
  RSpec/ExampleWording:
133
146
  Exclude:
134
147
  - 'spec/trailblazer/finder/adapters/active_record/predicates_spec.rb'
148
+ - 'spec/trailblazer/finder/adapters/data_mapper/predicates_spec.rb'
135
149
  - 'spec/trailblazer/finder/adapters/sequel/predicates_spec.rb'
136
150
  - 'spec/trailblazer/finder/features/predicates_spec.rb'
137
151
  - 'spec/trailblazer/operation/predicates_spec.rb'
@@ -155,6 +169,14 @@ Style/ClassAndModuleChildren:
155
169
  - 'spec/trailblazer/operation/predicates_spec.rb'
156
170
  - 'spec/trailblazer/operation/sorting_spec.rb'
157
171
 
172
+ # Offense count: 1
173
+ # Cop supports --auto-correct.
174
+ # Configuration parameters: EnforcedStyle.
175
+ # SupportedStyles: compact, expanded
176
+ Style/EmptyMethod:
177
+ Exclude:
178
+ - 'spec/trailblazer/finder/adapters/sequel/sorting_spec.rb'
179
+
158
180
  # Offense count: 1
159
181
  # Cop supports --auto-correct.
160
182
  Style/IfUnlessModifier:
@@ -180,7 +202,7 @@ Style/PerlBackrefs:
180
202
  Exclude:
181
203
  - 'lib/trailblazer/finder/utils/splitter.rb'
182
204
 
183
- # Offense count: 153
205
+ # Offense count: 191
184
206
  # Cop supports --auto-correct.
185
207
  # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
186
208
  # SupportedStyles: single_quotes, double_quotes
@@ -189,23 +211,21 @@ Style/StringLiterals:
189
211
  - 'lib/trailblazer/finder/adapters/sequel/predicates.rb'
190
212
  - 'spec/trailblazer/finder/adapters/active_record/predicates_spec.rb'
191
213
  - 'spec/trailblazer/finder/adapters/active_record/sorting_spec.rb'
214
+ - 'spec/trailblazer/finder/adapters/data_mapper/predicates_spec.rb'
215
+ - 'spec/trailblazer/finder/adapters/data_mapper/sorting_spec.rb'
192
216
  - 'spec/trailblazer/finder/adapters/sequel/predicates_spec.rb'
193
217
  - 'spec/trailblazer/finder/features/predicates_spec.rb'
194
218
  - 'spec/trailblazer/operation/predicates_spec.rb'
195
219
 
196
- # Offense count: 20
220
+ # Offense count: 24
197
221
  # Cop supports --auto-correct.
198
- # Configuration parameters: EnforcedStyle, MinSize, WordRegex.
222
+ # Configuration parameters: WordRegex.
199
223
  # SupportedStyles: percent, brackets
200
224
  Style/WordArray:
201
- Exclude:
202
- - 'spec/trailblazer/finder/adapters/active_record/predicates_spec.rb'
203
- - 'spec/trailblazer/finder/adapters/sequel/predicates_spec.rb'
204
- - 'spec/trailblazer/finder/features/predicates_spec.rb'
205
- - 'spec/trailblazer/finder/features/sorting_spec.rb'
206
- - 'spec/trailblazer/operation/predicates_spec.rb'
225
+ EnforcedStyle: percent
226
+ MinSize: 9
207
227
 
208
- # Offense count: 174
228
+ # Offense count: 191
209
229
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
210
230
  # URISchemes: http, https
211
231
  Metrics/LineLength:
data/CHANGES.md CHANGED
@@ -1,3 +1,6 @@
1
+ # 0.1.4
2
+ * Updated DataMapper Adapter to include sorting by multiple columns/directions and the predicate feature.
3
+
1
4
  # 0.1.3
2
5
  * 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
6
  * Due to the above, a big change in the way sorting works, and which methods are available for it
data/README.md CHANGED
@@ -257,8 +257,6 @@ At the moment we support:
257
257
  - gt: greater than (value converts to float)
258
258
  - gte: greater than or equal to (value converts to float)
259
259
 
260
- NOTE: PREDICATES CURRENTLY DO NOT WORK FOR THE OLD DATAMAPPER ADAPTER
261
-
262
260
  #### Predicate Example
263
261
  ```ruby
264
262
  class Post::Finder < Trailblazer::Finder
@@ -349,8 +347,6 @@ This feature extends the result[:finder] object with the following methods
349
347
  ### Sorting
350
348
  Really simple sorting feature, fixing the pain of dealing with sorting attributes and directions. Can sort by multiple columns/directions.
351
349
 
352
- NOTE: AS OF 0.1.3 TEMPORARY NOT WORKING FOR DATAMAPPER ADAPTER, WE ARE WORKING ON FIXING IT
353
-
354
350
  #### Sorting Example
355
351
  ```ruby
356
352
  class Post::Finder < Trailblazer::Finder
@@ -15,7 +15,6 @@ module Trailblazer
15
15
  end
16
16
 
17
17
  module ClassMethods
18
- # :nocov:
19
18
  def normalize_find_handler(handler, key)
20
19
  case handler
21
20
  when Symbol then ->(entity_type, value) { method(handler).call entity_type, value }
@@ -23,7 +22,6 @@ module Trailblazer
23
22
  else ->(entity_type, value) { entity_type.where(key.to_sym => value) unless Utils::String.blank?(value) }
24
23
  end
25
24
  end
26
- # :nocov:
27
25
  end
28
26
 
29
27
  include Paging if defined?(Features::Paging::ClassMethods)
@@ -1,5 +1,6 @@
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
+ require 'trailblazer/finder/adapters/data_mapper/predicates'
3
4
 
4
5
  module Trailblazer
5
6
  class Finder
@@ -8,10 +9,12 @@ module Trailblazer
8
9
  module DataMapper
9
10
  def self.included(base)
10
11
  base.extend ClassMethods
12
+ base.instance_eval do
13
+ include DataMapper::Predicates if defined?(Features::Predicate::ClassMethods)
14
+ end
11
15
  end
12
16
 
13
17
  module ClassMethods
14
- # :nocov:
15
18
  def normalize_find_handler(handler, key)
16
19
  case handler
17
20
  when Symbol then ->(entity_type, value) { method(handler).call entity_type, value }
@@ -19,11 +22,10 @@ module Trailblazer
19
22
  else ->(entity_type, value) { entity_type.all(key.to_sym => value) unless Utils::String.blank?(value) }
20
23
  end
21
24
  end
22
- # :nocov:
23
25
  end
24
26
 
25
27
  include Paging if defined?(Features::Paging::ClassMethods)
26
- # include Sorting if defined?(Features::Sorting::ClassMethods)
28
+ include Sorting if defined?(Features::Sorting::ClassMethods)
27
29
  end
28
30
  end
29
31
  end
@@ -0,0 +1,42 @@
1
+ module Trailblazer
2
+ class Finder
3
+ module Adapters
4
+ module DataMapper
5
+ # ActiveRecord - Predicate Adapter
6
+ module Predicates
7
+ def not_eq(attribute, value, entity_type)
8
+ entity_type.all(attribute.to_sym.not => value)
9
+ end
10
+
11
+ def eq(attribute, value, entity_type)
12
+ entity_type.all(attribute.to_sym => value)
13
+ end
14
+
15
+ def blank(attribute, _value, entity_type)
16
+ entity_type.all(conditions: ["#{attribute} IS NULL OR #{attribute} = ?", ''])
17
+ end
18
+
19
+ def not_blank(attribute, _value, entity_type)
20
+ entity_type.all(conditions: ["#{attribute} IS NOT NULL AND #{attribute} != ?", ''])
21
+ end
22
+
23
+ def gt(attribute, value, entity_type)
24
+ entity_type.all(attribute.to_sym.gt => value)
25
+ end
26
+
27
+ def gte(attribute, value, entity_type)
28
+ entity_type.all(attribute.to_sym.gte => value)
29
+ end
30
+
31
+ def lt(attribute, value, entity_type)
32
+ entity_type.all(attribute.to_sym.lt => value)
33
+ end
34
+
35
+ def lte(attribute, value, entity_type)
36
+ entity_type.all(attribute.to_sym.lte => value)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,25 +1,38 @@
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
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
+
22
+ def sort_orders(sort_attr, sort_dir)
23
+ case sort_dir
24
+ when 'asc', 'ascending'
25
+ sort_attr.to_sym.asc
26
+ when 'desc', 'descending'
27
+ sort_attr.to_sym.desc
28
+ end
29
+ end
30
+
31
+ def sort_it(entity_type, sort_attributes)
32
+ entity_type.all(order: sort_attributes.flatten)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -12,11 +12,9 @@ module Trailblazer
12
12
  base.instance_eval do
13
13
  include Sequel::Predicates if defined?(Features::Predicate::ClassMethods)
14
14
  end
15
- # require 'trailblazer/finder/adapters/sequel/predicates' if defined?(Features::Predicates::ClassMethods)
16
15
  end
17
16
 
18
17
  module ClassMethods
19
- # :nocov:
20
18
  def normalize_find_handler(handler, key)
21
19
  case handler
22
20
  when Symbol then ->(entity_type, value) { method(handler).call entity_type, value }
@@ -24,7 +22,6 @@ module Trailblazer
24
22
  else ->(entity_type, value) { entity_type.where(key.to_sym => value) unless Utils::String.blank?(value) }
25
23
  end
26
24
  end
27
- # :nocov:
28
25
  end
29
26
 
30
27
  include Paging if defined?(Features::Paging::ClassMethods)
@@ -1,5 +1,5 @@
1
1
  module Trailblazer
2
2
  class Finder
3
- VERSION = '0.1.3'.freeze
3
+ VERSION = '0.1.4'.freeze
4
4
  end
5
5
  end
@@ -9,8 +9,8 @@ RSpec.configure do |config|
9
9
  config.expect_with(:rspec) { |c| c.syntax = :expect }
10
10
  end
11
11
 
12
- require 'coveralls'
13
- Coveralls.wear!
12
+ # require 'coveralls'
13
+ # Coveralls.wear!
14
14
 
15
15
  require_relative 'support/sorting_shared_example'
16
16
  require 'trailblazer/finder'
@@ -0,0 +1,100 @@
1
+ require 'spec_helper_data_mapper'
2
+ # require 'spec_helper'
3
+
4
+ module Trailblazer
5
+ class Finder
6
+ module Adapters
7
+ module DataMapper
8
+ describe Predicates do
9
+ after do
10
+ DProduct.all.destroy
11
+ end
12
+
13
+ class TestFinder < Trailblazer::Finder
14
+ features Predicate
15
+ adapters DataMapper
16
+
17
+ entity_type { DProduct }
18
+
19
+ predicates_for :name, :price, :created_at
20
+
21
+ filter_by(:category) { |entity_type, _| entity_type.joins(:category) }
22
+ end
23
+
24
+ def finder_with_predicate(filter = nil, value = nil, filters = {})
25
+ TestFinder.new filter: { filter => value }.merge(filters)
26
+ end
27
+
28
+ describe 'equals' do
29
+ before do
30
+ 10.times do |i|
31
+ next DProduct.create name: "", price: "1#{i}" if i == 7
32
+ next DProduct.create name: nil, price: "1#{i}" if i == 8
33
+ next DProduct.create name: "product_4", price: "1#{i}" if i == 9
34
+ DProduct.create name: "product_#{i}", price: "1#{i}"
35
+ end
36
+ end
37
+
38
+ it 'it finds single row with name equal to product_5' do
39
+ finder = finder_with_predicate 'name_eq', 'product_5'
40
+ expect(finder.results.map(&:price)).to eq [15]
41
+ end
42
+
43
+ it 'it finds multiple rows with name equal to product_4' do
44
+ finder = finder_with_predicate 'name_eq', 'product_4'
45
+ expect(finder.results.map(&:price)).to eq [14, 19]
46
+ end
47
+
48
+ it 'it finds rows with name not equal to product_4' do
49
+ finder = finder_with_predicate 'name_not_eq', 'product_4'
50
+ expect(finder.results.map(&:name)).to eq ["product_0", "product_1", "product_2", "product_3", "product_5", "product_6", ""]
51
+ expect(finder.results.map(&:price)).to eq [10, 11, 12, 13, 15, 16, 17]
52
+ end
53
+
54
+ it 'it finds multiple rows with name blank (empty/nil)' do
55
+ finder = finder_with_predicate 'name_blank', ''
56
+ expect(finder.results.map(&:price)).to eq [17, 18]
57
+ end
58
+
59
+ it 'it finds multiple rows with name not blank (empty/nil)' do
60
+ finder = finder_with_predicate 'name_not_blank', ''
61
+ expect(finder.results.map(&:name)).to eq ["product_0", "product_1", "product_2", "product_3", "product_4", "product_5", "product_6", "product_4"]
62
+ end
63
+
64
+ it 'it finds multiple rows with price less than 15' do
65
+ finder = finder_with_predicate 'price_lt', 15
66
+ expect(finder.results.map(&:name)).to eq ["product_0", "product_1", "product_2", "product_3", "product_4"]
67
+ expect(finder.results.map(&:price)).to eq [10, 11, 12, 13, 14]
68
+ end
69
+
70
+ it 'it finds multiple rows with price equal to and less than 15' do
71
+ finder = finder_with_predicate 'price_lte', 15
72
+ expect(finder.results.map(&:name)).to eq ["product_0", "product_1", "product_2", "product_3", "product_4", "product_5"]
73
+ expect(finder.results.map(&:price)).to eq [10, 11, 12, 13, 14, 15]
74
+ end
75
+
76
+ it 'it finds multiple rows with price greater than 15' do
77
+ finder = finder_with_predicate 'price_gt', 15
78
+ expect(finder.results.map(&:name)).to eq ["product_6", "", nil, "product_4"]
79
+ expect(finder.results.map(&:price)).to eq [16, 17, 18, 19]
80
+ end
81
+
82
+ it 'it finds multiple rows with price equal to and greater than 15' do
83
+ finder = finder_with_predicate 'price_gte', 15
84
+ expect(finder.results.map(&:name)).to eq ["product_5", "product_6", "", nil, "product_4"]
85
+ expect(finder.results.map(&:price)).to eq [15, 16, 17, 18, 19]
86
+ end
87
+
88
+ it 'it finds rows with name not equal to product_4 and name not blank and price greater than 14' do
89
+ params = { name_not_blank: true, price_gt: 14 }
90
+ finder = finder_with_predicate 'name_not_eq', 'product_4', params
91
+
92
+ expect(finder.results.map(&:name)).to eq ["product_5", "product_6"]
93
+ expect(finder.results.map(&:price)).to eq [15, 16]
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -1,85 +1,97 @@
1
- # require 'spec_helper_data_mapper'
2
- #
3
- # module Trailblazer
4
- # class Finder
5
- # module Adapters
6
- # module DataMapper
7
- # describe Sorting do
8
- # def finder_class
9
- # Class.new do
10
- # include Trailblazer::Finder::Base
11
- # include Trailblazer::Finder::Features::Sorting
12
- # include Trailblazer::Finder::Adapters::DataMapper
13
- #
14
- # entity_type { DProduct }
15
- #
16
- # sortable_by :name, :price, :created_at
17
- #
18
- # filter_by :name
19
- # filter_by :price
20
- #
21
- # # # Need to find a fix for this, not sure how to handle joins with data mapper properly
22
- # # filter_by(:d_category) { |entity_type, _| entity_type }
23
- # end
24
- # end
25
- #
26
- # def finder_with_sort(sort = nil, filters = {})
27
- # finder_class.new filter: { sort: sort }.merge(filters)
28
- # end
29
- #
30
- # it 'can be inherited' do
31
- # child_class = Class.new(finder_class)
32
- # expect(child_class.new.sort_attribute).to eq 'name'
33
- # end
34
- #
35
- # describe 'sorting' do
36
- # after do
37
- # DProduct.all.destroy
38
- # end
39
- #
40
- # it 'sorts results based on the sort option desc' do
41
- # 5.times { |i| DProduct.create price: i }
42
- #
43
- # finder = finder_with_sort 'price desc'
44
- # expect(finder.results.map(&:price)).to eq [4, 3, 2, 1, 0]
45
- # end
46
- #
47
- # it 'sorts results based on the sort option asc' do
48
- # 5.times { |i| DProduct.create price: i }
49
- #
50
- # finder = finder_with_sort 'price asc'
51
- # expect(finder.results.map(&:price)).to eq [0, 1, 2, 3, 4]
52
- # end
53
- #
54
- # it 'defaults to first sort by option' do
55
- # 5.times { |i| DProduct.create name: "Name#{i}" }
56
- #
57
- # finder = finder_with_sort
58
- # expect(finder.results.map(&:name)).to eq %w[Name4 Name3 Name2 Name1 Name0]
59
- # end
60
- #
61
- # it 'ignores invalid sort values' do
62
- # finder = finder_with_sort 'invalid attribute'
63
- # expect { finder.results.to_a }.not_to raise_error
64
- # end
65
- #
66
- # # TODO: Need to find a fix for this, not sure how to handle joins with data mapper properly
67
- # it 'can handle renames of sorting in joins' do
68
- # # older_category = DCategory.create title: 'older'
69
- # # newer_category = DCategory.create title: 'newer'
70
- # #
71
- # # product_of_newer_category = DProduct.create name: 'older product', d_category: newer_category
72
- # # product_of_older_category = DProduct.create name: 'newer product', d_category: older_category
73
- # #
74
- # # finder = finder_with_sort 'created_at desc', d_category: ''
75
- # #
76
- # # expect(finder.results.map(&:name)).to eq [product_of_older_category.name, product_of_newer_category.name]
77
- # end
78
- # end
79
- #
80
- # it_behaves_like 'a sorting feature'
81
- # end
82
- # end
83
- # end
84
- # end
85
- # end
1
+ require 'spec_helper_data_mapper'
2
+
3
+ describe "Trailblazer::Finder::Adapters::DataMapper::Sorting", :sorting do
4
+ class TestDMFinder < Trailblazer::Finder
5
+ features Sorting
6
+ adapters DataMapper
7
+
8
+ entity_type { DProduct }
9
+
10
+ sortable_by :name, :price, :created_at
11
+
12
+ filter_by :name
13
+ filter_by :price
14
+ end
15
+
16
+ def finder_with_sort(sort = nil, filters = {})
17
+ TestDMFinder.new filter: { sort: sort }.merge(filters)
18
+ end
19
+
20
+ describe 'sorting' do
21
+ after do
22
+ DProduct.all.destroy
23
+ end
24
+
25
+ it 'sorts results based on the sort option desc' do
26
+ 5.times { |count| DProduct.create price: count }
27
+
28
+ finder = finder_with_sort 'price desc'
29
+ expect(finder.results.map(&:price)).to eq [4, 3, 2, 1, 0]
30
+ end
31
+
32
+ it 'sorts results based on the sort option asc' do
33
+ 5.times { |count| DProduct.create price: count }
34
+
35
+ finder = finder_with_sort 'price asc'
36
+ expect(finder.results.map(&:price)).to eq [0, 1, 2, 3, 4]
37
+ end
38
+
39
+ it 'defaults to first sort by option' do
40
+ 5.times { |count| DProduct.create name: "Name#{count}" }
41
+
42
+ finder = finder_with_sort
43
+ expect(finder.results.map(&:name)).to eq %w[Name4 Name3 Name2 Name1 Name0]
44
+ end
45
+
46
+ it 'ignores invalid sort values' do
47
+ finder = finder_with_sort 'invalid attribute'
48
+ expect { finder.results.to_a }.not_to raise_error
49
+ end
50
+
51
+ # TODO: Need to find a fix for this, not sure how to handle joins with data mapper properly
52
+ it 'can handle renames of sorting in joins' do
53
+ # older_category = DCategory.create title: 'older'
54
+ # newer_category = DCategory.create title: 'newer'
55
+ #
56
+ # product_of_newer_category = DProduct.create name: 'older product', d_category: newer_category
57
+ # product_of_older_category = DProduct.create name: 'newer product', d_category: older_category
58
+ #
59
+ # finder = finder_with_sort 'created_at desc', d_category: ''
60
+ #
61
+ # expect(finder.results.map(&:name)).to eq [product_of_older_category.name, product_of_newer_category.name]
62
+ end
63
+ end
64
+
65
+ describe 'sorting by multiple' do
66
+ before do
67
+ 5.times do |i|
68
+ DProduct.create name: "Name#{i}", price: "1#{i}"
69
+ end
70
+ DProduct.create name: 'Name3', price: '8'
71
+ end
72
+
73
+ after do
74
+ DProduct.all.destroy
75
+ end
76
+
77
+ it 'sorts by multiple columns name asc and price asc' do
78
+ finder = finder_with_sort 'name asc, price asc'
79
+ expect(finder.results.map(&:name)).to eq %w[Name0 Name1 Name2 Name3 Name3 Name4]
80
+ expect(finder.results.map(&:price)).to eq [10, 11, 12, 8, 13, 14]
81
+ end
82
+
83
+ it 'sorts by multiple columns name asc and price desc' do
84
+ finder = finder_with_sort 'name asc, price desc'
85
+ expect(finder.results.map(&:name)).to eq %w[Name0 Name1 Name2 Name3 Name3 Name4]
86
+ expect(finder.results.map(&:price)).to eq [10, 11, 12, 13, 8, 14]
87
+ end
88
+
89
+ it 'sorts by multiple columns name desc and price desc' do
90
+ finder = finder_with_sort 'name desc, price desc'
91
+ expect(finder.results.map(&:name)).to eq %w[Name4 Name3 Name3 Name2 Name1 Name0]
92
+ expect(finder.results.map(&:price)).to eq [14, 13, 8, 12, 11, 10]
93
+ end
94
+ end
95
+
96
+ it_behaves_like 'a sorting feature'
97
+ end
@@ -16,7 +16,7 @@ module Trailblazer
16
16
  end
17
17
  end
18
18
 
19
- def finder_class(&block)
19
+ def finder_class(&block)
20
20
  define_finder_class do
21
21
  adapters ActiveRecord
22
22
 
@@ -25,6 +25,12 @@ module Trailblazer
25
25
  filter_by :id
26
26
  filter_by :name
27
27
  filter_by :slug
28
+ filter_by :escaped_name, with: :apply_escaped_name
29
+
30
+ def apply_escaped_name(entity_type, value)
31
+ return unless value.present?
32
+ entity_type.where slug: value
33
+ end
28
34
 
29
35
  if block.nil?
30
36
  filter_by :value do |entity_type, value|
@@ -105,6 +111,13 @@ module Trailblazer
105
111
  finder = finder_with_filter invalid: 'option'
106
112
  expect { finder.invalid }.to raise_error NoMethodError
107
113
  end
114
+
115
+ it 'can filter by symbol specified filters' do
116
+ 10.times { |i| SProduct.create slug: "product_#{i}" }
117
+ finder = finder_with_filter :escaped_name, 'product_2'
118
+
119
+ expect(finder.results.first.slug).to eq 'product_2'
120
+ end
108
121
  end
109
122
  end
110
123
  end
@@ -17,13 +17,16 @@ describe 'Trailblazer::Finder::Adapters::Sequel::Sorting', :sorting do
17
17
 
18
18
  sortable_by :name, :price, :created_at
19
19
 
20
- filter_by :name
20
+ filter_by :name, with: :apply_name_filter
21
21
  filter_by :price
22
22
  filter_by(:s_category) { |entity_type, _| entity_type.association_join(:s_category) }
23
+
24
+ def apply_name_filter(entity_type, value)
25
+ end
23
26
  end
24
27
 
25
28
  def finder_with_sort(sort = nil, filters = {})
26
- TestSequelSortFinder.new filter: (sort.nil? ? {} : { sort: sort }).merge(filters)
29
+ TestSequelSortFinder.new filter: (sort.nil? ? {} : { name: 'test', sort: sort }).merge(filters)
27
30
  end
28
31
 
29
32
  def finder_with_nil_sort
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trailblazer-finder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
@@ -274,6 +274,7 @@ files:
274
274
  - lib/trailblazer/finder/adapters/active_record/sorting.rb
275
275
  - lib/trailblazer/finder/adapters/data_mapper.rb
276
276
  - lib/trailblazer/finder/adapters/data_mapper/paging.rb
277
+ - lib/trailblazer/finder/adapters/data_mapper/predicates.rb
277
278
  - lib/trailblazer/finder/adapters/data_mapper/sorting.rb
278
279
  - lib/trailblazer/finder/adapters/friendly_id.rb
279
280
  - lib/trailblazer/finder/adapters/kaminari.rb
@@ -315,6 +316,7 @@ files:
315
316
  - spec/trailblazer/finder/adapters/active_record/sorting_spec.rb
316
317
  - spec/trailblazer/finder/adapters/data_mapper/base_spec.rb
317
318
  - spec/trailblazer/finder/adapters/data_mapper/paging_spec.rb
319
+ - spec/trailblazer/finder/adapters/data_mapper/predicates_spec.rb
318
320
  - spec/trailblazer/finder/adapters/data_mapper/sorting_spec.rb
319
321
  - spec/trailblazer/finder/adapters/friendly_id_spec.rb
320
322
  - spec/trailblazer/finder/adapters/kaminari_spec.rb
@@ -378,6 +380,7 @@ test_files:
378
380
  - spec/trailblazer/finder/adapters/active_record/sorting_spec.rb
379
381
  - spec/trailblazer/finder/adapters/data_mapper/base_spec.rb
380
382
  - spec/trailblazer/finder/adapters/data_mapper/paging_spec.rb
383
+ - spec/trailblazer/finder/adapters/data_mapper/predicates_spec.rb
381
384
  - spec/trailblazer/finder/adapters/data_mapper/sorting_spec.rb
382
385
  - spec/trailblazer/finder/adapters/friendly_id_spec.rb
383
386
  - spec/trailblazer/finder/adapters/kaminari_spec.rb