trailblazer-finder 0.70.0 → 0.80.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- module Trailblazer
6
- class Finder
7
- module Utils
8
- describe String do
9
- describe ".blank?" do
10
- it "checks if value is blank or nil" do
11
- expect(described_class.blank?("")).to eq true
12
- expect(described_class.blank?(nil)).to eq true
13
- expect(described_class.blank?("what")).to eq false
14
- expect(described_class.blank?(1)).to eq false
15
- end
16
- end
17
-
18
- describe ".numeric?" do
19
- it "checks if value is numeric" do
20
- expect(described_class.numeric?("")).to eq false
21
- expect(described_class.numeric?(nil)).to eq false
22
- expect(described_class.numeric?(1)).to eq true
23
- expect(described_class.numeric?(0)).to eq true
24
- expect(described_class.numeric?(1.00000)).to eq true
25
- expect(described_class.numeric?("1")).to eq true
26
- end
27
- end
28
-
29
- describe ".date?" do
30
- it "checks if value is a date" do
31
- expect(described_class.date?("2018-01-01")).to eq true
32
- expect(described_class.date?(nil)).to eq false
33
- expect(described_class.date?("random")).to eq false
34
- expect(described_class.date?(1)).to eq false
35
- expect(described_class.date?("2018/01/01")).to eq true
36
- expect(described_class.date?("2018.01.01")).to eq true
37
- expect(described_class.date?("21-12-2018")).to eq true
38
- expect(described_class.date?("0fae2de1-6537-4d36-9cdb-30edf1e37990")).to eq false
39
- end
40
- end
41
-
42
- describe ".to_date" do
43
- it "transforms a date like value to a valid date" do
44
- expect(described_class.to_date("28/09/2018")).to eq "2018-09-28"
45
- expect(described_class.to_date("2018/09/28")).to eq "2018-09-28"
46
- expect(described_class.to_date("28 september 2018")).to eq "2018-09-28"
47
- expect(described_class.to_date("third month of this year")).to eq nil
48
- end
49
- end
50
-
51
- describe ".camelize" do
52
- it "transforms a string by capatalizing each word's first letter" do
53
- expect(described_class.camelize(:paging)).to eq "Paging"
54
- expect(described_class.camelize(:some_random_test)).to eq "SomeRandomTest"
55
- end
56
- end
57
-
58
- describe ".underscore" do
59
- it "transforms a string by dividing words with underscores" do
60
- expect(described_class.underscore(:veryPopular)).to eq "very_popular"
61
- expect(described_class.underscore(:VeryPopular)).to eq "very_popular"
62
- expect(described_class.underscore(:SomethingveryPopularButRandom)).to eq "somethingvery_popular_but_random"
63
- expect(described_class.underscore("Very Popular")).to eq "very_popular"
64
- end
65
- end
66
- end
67
- end
68
- end
69
- end
@@ -1,121 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "delegate"
4
- require "trailblazer"
5
- require "spec_helper_active_record"
6
-
7
- class Product::FinderNoEntity < Trailblazer::Finder
8
- adapter "ActiveRecord"
9
-
10
- property :id, type: Types::Integer
11
- property :name, type: Types::String, sortable: true
12
- filter_by :escaped_name, with: :apply_escaped_name
13
-
14
- def apply_escaped_name(entity, _attribute, value)
15
- return unless value.present?
16
-
17
- entity.where "lower(name) LIKE ?", "%#{value.downcase}%"
18
- end
19
- end
20
-
21
- class Product::FinderWithEntity < Trailblazer::Finder
22
- adapter "ActiveRecord"
23
-
24
- entity { Product }
25
-
26
- property :id, type: Types::Integer
27
- property :name, type: Types::String, sortable: true
28
- filter_by :escaped_name, with: :apply_escaped_name
29
-
30
- def apply_escaped_name(entity, _attribute, value)
31
- return unless value.present?
32
-
33
- entity.where "lower(name) LIKE ?", "%#{value.downcase}%"
34
- end
35
- end
36
-
37
- class Product::Index < Trailblazer::Operation
38
- step Finder(Product::FinderNoEntity, :all, Product)
39
- end
40
-
41
- class Product::Show < Trailblazer::Operation
42
- step Finder(Product::FinderNoEntity, :single, Product)
43
- end
44
-
45
- class Product::IndexNoEntity < Trailblazer::Operation
46
- step Finder(Product::FinderWithEntity, :all)
47
- end
48
-
49
- class Product::ShowNoEntity < Trailblazer::Operation
50
- step Finder(Product::FinderWithEntity, :single)
51
- end
52
-
53
- describe "Trailblazer::Operation - Finder Macro" do
54
- before do
55
- Product.destroy_all
56
- Product.reset_pk_sequence
57
- 22.times { |i| Product.create name: "product_#{i}" }
58
- end
59
-
60
- after do
61
- Product.destroy_all
62
- Product.reset_pk_sequence
63
- end
64
-
65
- it "Can find a single row by id" do
66
- params = {id: 5}
67
- result = Product::Show.call(params: params)
68
-
69
- expect(result[:finder].name).to eq "product_4"
70
- end
71
-
72
- it "Can find a single row by id from ActiveSupport::HashWithIndifferentAccess" do
73
- params = ActiveSupport::HashWithIndifferentAccess.new({id: 6})
74
- result = Product::Show.call(params: params)
75
-
76
- expect(result[:finder].name).to eq "product_5"
77
- end
78
-
79
- it "Can find a single row by name" do
80
- params = {name_eq: "product_2"}
81
- result = Product::Show.call(params: params)
82
-
83
- expect(result[:finder].name).to eq "product_2"
84
- end
85
-
86
- it "Can find multiple rows by escaped name" do
87
- params = {escaped_name: "product_1"}
88
- result = Product::Index.call(params: params)
89
-
90
- expect(result[:finder].result.last.name).to eq "product_19"
91
- end
92
-
93
- it "Can find multiple rows by escaped name if key is string" do
94
- params = {"escaped_name" => "product_1"}
95
- result = Product::Index.call(params: params)
96
-
97
- expect(result[:finder].result.last.name).to eq "product_19"
98
- end
99
-
100
- it "Can find a single row by id when no entity type is given in macro" do
101
- params = {id: 8}
102
- result = Product::ShowNoEntity.call(params: params)
103
-
104
- expect(result[:finder].name).to eq "product_7"
105
- end
106
-
107
- it "Can find a single row by name when no entity type is given in macro" do
108
- params = {name_eq: "product_2"}
109
- result = Product::ShowNoEntity.call(params: params)
110
-
111
- expect(result[:finder].name).to eq "product_2"
112
- end
113
-
114
- it "Can find multiple rows by escaped name when no entity type is given in macro" do
115
- params = {escaped_name: "product_1"}
116
- result = Product::IndexNoEntity.call(params: params)
117
-
118
- expect(result[:finder].result.count).to eq 11
119
- expect(result[:finder].result.last.name).to eq "product_19"
120
- end
121
- end