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,112 +0,0 @@
|
|
1
|
-
require 'spec_helper_active_record'
|
2
|
-
|
3
|
-
module Trailblazer
|
4
|
-
class Finder
|
5
|
-
module Adapters
|
6
|
-
describe ActiveRecord do
|
7
|
-
# it_behaves_like 'a base orm adapter'
|
8
|
-
|
9
|
-
after do
|
10
|
-
Product.delete_all
|
11
|
-
end
|
12
|
-
|
13
|
-
def define_finder_class(&block)
|
14
|
-
Class.new(Trailblazer::Finder) do
|
15
|
-
class_eval(&block)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def finder_class(&block)
|
20
|
-
define_finder_class do
|
21
|
-
adapters ActiveRecord
|
22
|
-
|
23
|
-
entity_type { Product }
|
24
|
-
|
25
|
-
filter_by :id
|
26
|
-
filter_by :name
|
27
|
-
filter_by :slug
|
28
|
-
|
29
|
-
if block.nil?
|
30
|
-
filter_by :value do |entity_type, value|
|
31
|
-
entity_type.where(slug: value)
|
32
|
-
end
|
33
|
-
else
|
34
|
-
class_eval(&block)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def finder_with_filter(key = nil, name = nil, &block)
|
40
|
-
finder_class(&block).new filter: { key => name }
|
41
|
-
end
|
42
|
-
|
43
|
-
describe 'filter_by' do
|
44
|
-
it 'has a default filter' do
|
45
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
46
|
-
finder = finder_with_filter :name, 'product_2'
|
47
|
-
|
48
|
-
expect(finder.results.first.name).to eq 'product_2'
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'has another default filter' do
|
52
|
-
10.times { |i| Product.create slug: "product_#{i}" }
|
53
|
-
finder = finder_with_filter :value, 'product_2'
|
54
|
-
|
55
|
-
expect(finder.results.first.slug).to eq 'product_2'
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'can use methods from the object' do
|
60
|
-
2.times { |i| Product.create name: "product_#{i}" }
|
61
|
-
finder1 = finder_with_filter :id, 1 do
|
62
|
-
filter_by :id do |entity_type, value|
|
63
|
-
some_instance_method(entity_type, value)
|
64
|
-
end
|
65
|
-
|
66
|
-
private
|
67
|
-
|
68
|
-
def some_instance_method(entity_type, value)
|
69
|
-
entity_type.where(id: value)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
expect(finder1.results.first.id).to eq 1
|
74
|
-
end
|
75
|
-
|
76
|
-
it 'can dispatch with instance methods' do
|
77
|
-
2.times { |i| Product.create name: "product_#{i}" }
|
78
|
-
finder = finder_with_filter :id, 3 do
|
79
|
-
filter_by :id, with: :some_instance_method
|
80
|
-
|
81
|
-
private
|
82
|
-
|
83
|
-
def some_instance_method(entity_type, value)
|
84
|
-
entity_type.where(id: value)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
expect(finder.results.first.id).to eq 3
|
89
|
-
end
|
90
|
-
|
91
|
-
describe 'filter_by attributes' do
|
92
|
-
it 'accesses filter values' do
|
93
|
-
finder = finder_with_filter :value, 1
|
94
|
-
expect(finder.value).to eq 1
|
95
|
-
end
|
96
|
-
|
97
|
-
it 'returns default filter value if filter_by is not specified' do
|
98
|
-
finder = finder_with_filter do
|
99
|
-
filter_by :value, 1
|
100
|
-
end
|
101
|
-
expect(finder.value).to eq 1
|
102
|
-
end
|
103
|
-
|
104
|
-
it 'does not include invalid filters' do
|
105
|
-
finder = finder_with_filter invalid: 'option'
|
106
|
-
expect { finder.invalid }.to raise_error NoMethodError
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
@@ -1,64 +0,0 @@
|
|
1
|
-
require 'spec_helper_active_record'
|
2
|
-
|
3
|
-
module Trailblazer
|
4
|
-
class Finder
|
5
|
-
module Adapters
|
6
|
-
module ActiveRecord
|
7
|
-
describe Paging do
|
8
|
-
it_behaves_like 'a paging feature'
|
9
|
-
|
10
|
-
after do
|
11
|
-
Product.delete_all
|
12
|
-
end
|
13
|
-
|
14
|
-
def define_finder_class(&block)
|
15
|
-
Class.new do
|
16
|
-
include Trailblazer::Finder::Base
|
17
|
-
include Trailblazer::Finder::Features::Paging
|
18
|
-
include Trailblazer::Finder::Adapters::ActiveRecord
|
19
|
-
|
20
|
-
instance_eval(&block) if block_given?
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def finder_class
|
25
|
-
define_finder_class do
|
26
|
-
entity_type { Product }
|
27
|
-
|
28
|
-
per_page 2
|
29
|
-
|
30
|
-
min_per_page 2
|
31
|
-
max_per_page 10
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def finder_with_page(page = nil, per_page = nil)
|
36
|
-
finder_class.new page: page, per_page: per_page
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'can be inherited' do
|
40
|
-
child_class = Class.new(finder_class)
|
41
|
-
expect(child_class.new.per_page).to eq 2
|
42
|
-
end
|
43
|
-
|
44
|
-
describe '#results' do
|
45
|
-
it 'paginates results' do
|
46
|
-
6.times { |i| Product.create name: "product_#{i}" }
|
47
|
-
finder = finder_with_page 2, 2
|
48
|
-
|
49
|
-
expect(finder.results.map(&:name)).to eq %w[product_2 product_3]
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
describe '#count' do
|
54
|
-
it 'gives the real count' do
|
55
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
56
|
-
finder = finder_with_page 1
|
57
|
-
expect(finder.count).to eq 10
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
@@ -1,101 +0,0 @@
|
|
1
|
-
require 'spec_helper_active_record'
|
2
|
-
# require 'spec_helper'
|
3
|
-
|
4
|
-
module Trailblazer
|
5
|
-
class Finder
|
6
|
-
module Adapters
|
7
|
-
module ActiveRecord
|
8
|
-
describe Predicates do
|
9
|
-
after do
|
10
|
-
Product.delete_all
|
11
|
-
Product.reset_pk_sequence
|
12
|
-
end
|
13
|
-
|
14
|
-
class TestFinder < 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
|
-
def finder_with_predicate(filter = nil, value = nil, filters = {})
|
26
|
-
TestFinder.new filter: { filter => value }.merge(filters)
|
27
|
-
end
|
28
|
-
|
29
|
-
describe 'equals' do
|
30
|
-
before do
|
31
|
-
10.times do |i|
|
32
|
-
next Product.create name: "", price: "1#{i}" if i == 7
|
33
|
-
next Product.create name: nil, price: "1#{i}" if i == 8
|
34
|
-
next Product.create name: "product_4", price: "1#{i}" if i == 9
|
35
|
-
Product.create name: "product_#{i}", price: "1#{i}"
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'it finds single row with name equal to product_5' do
|
40
|
-
finder = finder_with_predicate 'name_eq', 'product_5'
|
41
|
-
expect(finder.results.map(&:price)).to eq [15]
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'it finds multiple rows with name equal to product_4' do
|
45
|
-
finder = finder_with_predicate 'name_eq', 'product_4'
|
46
|
-
expect(finder.results.map(&:price)).to eq [14, 19]
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'it finds rows with name not equal to product_4' do
|
50
|
-
finder = finder_with_predicate 'name_not_eq', 'product_4'
|
51
|
-
expect(finder.results.map(&:name)).to eq ["product_0", "product_1", "product_2", "product_3", "product_5", "product_6", ""]
|
52
|
-
expect(finder.results.map(&:price)).to eq [10, 11, 12, 13, 15, 16, 17]
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'it finds multiple rows with name blank (empty/nil)' do
|
56
|
-
finder = finder_with_predicate 'name_blank', ''
|
57
|
-
expect(finder.results.map(&:price)).to eq [17, 18]
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'it finds multiple rows with name not blank (empty/nil)' do
|
61
|
-
finder = finder_with_predicate 'name_not_blank', ''
|
62
|
-
expect(finder.results.map(&:name)).to eq ["product_0", "product_1", "product_2", "product_3", "product_4", "product_5", "product_6", "product_4"]
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'it finds multiple rows with price less than 15' do
|
66
|
-
finder = finder_with_predicate 'price_lt', 15
|
67
|
-
expect(finder.results.map(&:name)).to eq ["product_0", "product_1", "product_2", "product_3", "product_4"]
|
68
|
-
expect(finder.results.map(&:price)).to eq [10, 11, 12, 13, 14]
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'it finds multiple rows with price equal to and less than 15' do
|
72
|
-
finder = finder_with_predicate 'price_lte', 15
|
73
|
-
expect(finder.results.map(&:name)).to eq ["product_0", "product_1", "product_2", "product_3", "product_4", "product_5"]
|
74
|
-
expect(finder.results.map(&:price)).to eq [10, 11, 12, 13, 14, 15]
|
75
|
-
end
|
76
|
-
|
77
|
-
it 'it finds multiple rows with price greater than 15' do
|
78
|
-
finder = finder_with_predicate 'price_gt', 15
|
79
|
-
expect(finder.results.map(&:name)).to eq ["product_6", "", nil, "product_4"]
|
80
|
-
expect(finder.results.map(&:price)).to eq [16, 17, 18, 19]
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'it finds multiple rows with price equal to and greater than 15' do
|
84
|
-
finder = finder_with_predicate 'price_gte', 15
|
85
|
-
expect(finder.results.map(&:name)).to eq ["product_5", "product_6", "", nil, "product_4"]
|
86
|
-
expect(finder.results.map(&:price)).to eq [15, 16, 17, 18, 19]
|
87
|
-
end
|
88
|
-
|
89
|
-
it 'it finds rows with name not equal to product_4 and name not blank and price greater than 14' do
|
90
|
-
params = { name_not_blank: true, price_gt: 14 }
|
91
|
-
finder = finder_with_predicate 'name_not_eq', 'product_4', params
|
92
|
-
|
93
|
-
expect(finder.results.map(&:name)).to eq ["product_5", "product_6"]
|
94
|
-
expect(finder.results.map(&:price)).to eq [15, 16]
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
@@ -1,99 +0,0 @@
|
|
1
|
-
require 'spec_helper_active_record'
|
2
|
-
|
3
|
-
describe "Trailblazer::Finder::Adapters::ActiveRecord::Sorting", :sorting do
|
4
|
-
after do
|
5
|
-
Product.delete_all
|
6
|
-
Product.reset_pk_sequence
|
7
|
-
end
|
8
|
-
|
9
|
-
before do
|
10
|
-
Product.delete_all
|
11
|
-
Product.reset_pk_sequence
|
12
|
-
end
|
13
|
-
|
14
|
-
class TestARFinder < Trailblazer::Finder
|
15
|
-
features Sorting
|
16
|
-
adapters ActiveRecord
|
17
|
-
|
18
|
-
entity_type { Product.all }
|
19
|
-
|
20
|
-
sortable_by :name, :price, :created_at
|
21
|
-
|
22
|
-
filter_by :name
|
23
|
-
filter_by :price
|
24
|
-
end
|
25
|
-
|
26
|
-
def finder_with_sort(sort = nil, filters = {})
|
27
|
-
TestARFinder.new filter: (sort.nil? ? {} : { sort: sort }).merge(filters)
|
28
|
-
end
|
29
|
-
|
30
|
-
describe 'sorting' do
|
31
|
-
|
32
|
-
it 'sorts results based on the sort option desc' do
|
33
|
-
5.times { |i| Product.create! price: i }
|
34
|
-
|
35
|
-
finder = finder_with_sort 'price desc'
|
36
|
-
expect(finder.results.map(&:price)).to eq [4, 3, 2, 1, 0]
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'sorts results based on the sort option asc' do
|
40
|
-
5.times { |i| Product.create! price: i }
|
41
|
-
|
42
|
-
finder = finder_with_sort 'price asc'
|
43
|
-
expect(finder.results.map(&:price)).to eq [0, 1, 2, 3, 4]
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'defaults to defaulted database sort if none is set' do
|
47
|
-
5.times { |i| Product.create! name: "Name#{i}" }
|
48
|
-
|
49
|
-
finder = finder_with_sort
|
50
|
-
expect(finder.results.map(&:name)).to eq %w[Name0 Name1 Name2 Name3 Name4]
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'ignores invalid sort values' do
|
54
|
-
finder = finder_with_sort 'invalid attribute'
|
55
|
-
expect { finder.results.to_a }.not_to raise_error
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'can handle renames of sorting in joins' do
|
59
|
-
older_category = Category.create! title: 'older'
|
60
|
-
newer_category = Category.create! title: 'newer'
|
61
|
-
|
62
|
-
product_of_newer_category = Product.create! name: 'older product', category: newer_category
|
63
|
-
product_of_older_category = Product.create! name: 'newer product', category: older_category
|
64
|
-
|
65
|
-
finder = finder_with_sort 'created_at desc', category: ''
|
66
|
-
|
67
|
-
expect(finder.results.map(&:name)).to eq [product_of_older_category.name, product_of_newer_category.name]
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe 'sorting by multiple' do
|
72
|
-
before do
|
73
|
-
5.times do |i|
|
74
|
-
Product.create name: "Name#{i}", price: "1#{i}"
|
75
|
-
end
|
76
|
-
Product.create name: 'Name3', price: '8'
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'sorts by multiple columns name asc and price asc' do
|
80
|
-
finder = finder_with_sort 'name asc, price asc'
|
81
|
-
expect(finder.results.map(&:name)).to eq %w[Name0 Name1 Name2 Name3 Name3 Name4]
|
82
|
-
expect(finder.results.map(&:price)).to eq [10, 11, 12, 8, 13, 14]
|
83
|
-
end
|
84
|
-
|
85
|
-
it 'sorts by multiple columns name asc and price desc' do
|
86
|
-
finder = finder_with_sort 'name asc, price desc'
|
87
|
-
expect(finder.results.map(&:name)).to eq %w[Name0 Name1 Name2 Name3 Name3 Name4]
|
88
|
-
expect(finder.results.map(&:price)).to eq [10, 11, 12, 13, 8, 14]
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'sorts by multiple columns name desc and price desc' do
|
92
|
-
finder = finder_with_sort 'name desc, price desc'
|
93
|
-
expect(finder.results.map(&:name)).to eq %w[Name4 Name3 Name3 Name2 Name1 Name0]
|
94
|
-
expect(finder.results.map(&:price)).to eq [14, 13, 8, 12, 11, 10]
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
it_behaves_like 'a sorting feature'
|
99
|
-
end
|
@@ -1,112 +0,0 @@
|
|
1
|
-
require 'spec_helper_data_mapper'
|
2
|
-
|
3
|
-
module Trailblazer
|
4
|
-
class Finder
|
5
|
-
module Adapters
|
6
|
-
describe DataMapper do
|
7
|
-
# it_behaves_like 'a base orm adapter'
|
8
|
-
|
9
|
-
after do
|
10
|
-
DProduct.all.destroy
|
11
|
-
end
|
12
|
-
|
13
|
-
def define_finder_class(&block)
|
14
|
-
Class.new(Trailblazer::Finder) do
|
15
|
-
class_eval(&block)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def finder_class(&block)
|
20
|
-
define_finder_class do
|
21
|
-
adapters DataMapper
|
22
|
-
|
23
|
-
entity_type { DProduct }
|
24
|
-
|
25
|
-
filter_by :id
|
26
|
-
filter_by :name
|
27
|
-
filter_by :slug
|
28
|
-
|
29
|
-
if block.nil?
|
30
|
-
filter_by :value do |entity_type, value|
|
31
|
-
entity_type.all(slug: value)
|
32
|
-
end
|
33
|
-
else
|
34
|
-
class_eval(&block)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def finder_with_filter(key = nil, name = nil, &block)
|
40
|
-
finder_class(&block).new filter: { key => name }
|
41
|
-
end
|
42
|
-
|
43
|
-
describe 'filter_by' do
|
44
|
-
it 'has a default filter' do
|
45
|
-
10.times { |i| DProduct.create name: "product_#{i}" }
|
46
|
-
finder = finder_with_filter :name, 'product_2'
|
47
|
-
|
48
|
-
expect(finder.results.first.name).to eq 'product_2'
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'has another default filter' do
|
52
|
-
10.times { |i| DProduct.create slug: "product_#{i}" }
|
53
|
-
finder = finder_with_filter :value, 'product_2'
|
54
|
-
|
55
|
-
expect(finder.results.first.slug).to eq 'product_2'
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'can use methods from the object' do
|
60
|
-
2.times { |i| DProduct.create name: "product_#{i}" }
|
61
|
-
finder1 = finder_with_filter :id, 1 do
|
62
|
-
filter_by :id do |entity_type, value|
|
63
|
-
some_instance_method(entity_type, value)
|
64
|
-
end
|
65
|
-
|
66
|
-
private
|
67
|
-
|
68
|
-
def some_instance_method(entity_type, value)
|
69
|
-
entity_type.all(id: value)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
expect(finder1.results.first.id).to eq 1
|
74
|
-
end
|
75
|
-
|
76
|
-
it 'can dispatch with instance methods' do
|
77
|
-
2.times { |i| DProduct.create name: "product_#{i}" }
|
78
|
-
finder = finder_with_filter :id, 3 do
|
79
|
-
filter_by :id, with: :some_instance_method
|
80
|
-
|
81
|
-
private
|
82
|
-
|
83
|
-
def some_instance_method(entity_type, value)
|
84
|
-
entity_type.all(id: value)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
expect(finder.results.first.id).to eq 3
|
89
|
-
end
|
90
|
-
|
91
|
-
describe 'filter_by attributes' do
|
92
|
-
it 'accesses filter values' do
|
93
|
-
finder = finder_with_filter :value, 1
|
94
|
-
expect(finder.value).to eq 1
|
95
|
-
end
|
96
|
-
|
97
|
-
it 'returns default filter value if filter_by is not specified' do
|
98
|
-
finder = finder_with_filter do
|
99
|
-
filter_by :value, 1
|
100
|
-
end
|
101
|
-
expect(finder.value).to eq 1
|
102
|
-
end
|
103
|
-
|
104
|
-
it 'does not include invalid filters' do
|
105
|
-
finder = finder_with_filter invalid: 'option'
|
106
|
-
expect { finder.invalid }.to raise_error NoMethodError
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|