@@ -5,60 +5,55 @@ class Builder
5
5
attr_reader :klass
6
6
delegate :key_formatter , to : :klass
7
7
8
- def initialize ( klass )
9
- @klass = klass
10
- @primary_key = nil
11
- @pagination_params = { }
12
- @path_params = { }
13
- @additional_params = { }
14
- @filters = { }
15
- @includes = [ ]
16
- @orders = [ ]
17
- @fields = [ ]
8
+ def initialize ( klass , opts = { } )
9
+ @klass = klass
10
+ @primary_key = opts . fetch ( :primary_key , nil )
11
+ @pagination_params = opts . fetch ( :pagination_params , { } )
12
+ @path_params = opts . fetch ( :path_params , { } )
13
+ @additional_params = opts . fetch ( :additional_params , { } )
14
+ @filters = opts . fetch ( :filters , { } )
15
+ @includes = opts . fetch ( :includes , [ ] )
16
+ @orders = opts . fetch ( :orders , [ ] )
17
+ @fields = opts . fetch ( :fields , [ ] )
18
18
end
19
19
20
20
def where ( conditions = { } )
21
21
# pull out any path params here
22
- @path_params . merge! ( conditions . slice ( *klass . prefix_params ) )
23
- @filters . merge! ( conditions . except ( *klass . prefix_params ) )
24
- self
22
+ path_conditions = conditions . slice ( *klass . prefix_params )
23
+ unpathed_conditions = conditions . except ( *klass . prefix_params )
24
+
25
+ _new_scope ( path_params : path_conditions , filters : unpathed_conditions )
25
26
end
26
27
27
28
def order ( *args )
28
- @orders += parse_orders ( *args )
29
- self
29
+ _new_scope ( orders : parse_orders ( *args ) )
30
30
end
31
31
32
32
def includes ( *tables )
33
- @includes += parse_related_links ( *tables )
34
- self
33
+ _new_scope ( includes : parse_related_links ( *tables ) )
35
34
end
36
35
37
36
def select ( *fields )
38
- @fields += parse_fields ( *fields )
39
- self
37
+ _new_scope ( fields : parse_fields ( *fields ) )
40
38
end
41
39
42
40
def paginate ( conditions = { } )
43
- scope = self
41
+ scope = _new_scope
44
42
scope = scope . page ( conditions [ :page ] ) if conditions [ :page ]
45
43
scope = scope . per ( conditions [ :per_page ] ) if conditions [ :per_page ]
46
44
scope
47
45
end
48
46
49
47
def page ( number )
50
- @pagination_params [ klass . paginator . page_param ] = number || 1
51
- self
48
+ _new_scope ( pagination_params : { klass . paginator . page_param => number || 1 } )
52
49
end
53
50
54
51
def per ( size )
55
- @pagination_params [ klass . paginator . per_page_param ] = size
56
- self
52
+ _new_scope ( pagination_params : { klass . paginator . per_page_param => size } )
57
53
end
58
54
59
55
def with_params ( more_params )
60
- @additional_params . merge! ( more_params )
61
- self
56
+ _new_scope ( additional_params : more_params )
62
57
end
63
58
64
59
def first
@@ -92,12 +87,12 @@ def to_a
92
87
def find ( args = { } )
93
88
case args
94
89
when Hash
95
- where ( args )
90
+ scope = where ( args )
96
91
else
97
- @primary_key = args
92
+ scope = _new_scope ( primary_key : args )
98
93
end
99
94
100
- klass . requestor . get ( params )
95
+ klass . requestor . get ( scope . params )
101
96
end
102
97
103
98
def method_missing ( method_name , *args , &block )
@@ -106,6 +101,18 @@ def method_missing(method_name, *args, &block)
106
101
107
102
private
108
103
104
+ def _new_scope ( opts = { } )
105
+ self . class . new ( @klass ,
106
+ primary_key : opts . fetch ( :primary_key , @primary_key ) ,
107
+ pagination_params : @pagination_params . merge ( opts . fetch ( :pagination_params , { } ) ) ,
108
+ path_params : @path_params . merge ( opts . fetch ( :path_params , { } ) ) ,
109
+ additional_params : @additional_params . merge ( opts . fetch ( :additional_params , { } ) ) ,
110
+ filters : @filters . merge ( opts . fetch ( :filters , { } ) ) ,
111
+ includes : @includes + opts . fetch ( :includes , [ ] ) ,
112
+ orders : @orders + opts . fetch ( :orders , [ ] ) ,
113
+ fields : @fields + opts . fetch ( :fields , [ ] ) )
114
+ end
115
+
109
116
def path_params
110
117
@path_params . empty? ? { } : { path : @path_params }
111
118
end
0 commit comments