api_me 0.8.3 → 0.9.0
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 +4 -4
- data/Gemfile +4 -7
- data/Gemfile.lock +100 -81
- data/README.md +39 -0
- data/Rakefile +1 -1
- data/api_me.gemspec +4 -4
- data/lib/api_me.rb +22 -7
- data/lib/api_me/engine.rb +1 -1
- data/lib/api_me/pagination.rb +3 -6
- data/lib/api_me/sorting.rb +9 -10
- data/lib/api_me/version.rb +1 -1
- data/lib/generators/api_me/controller/controller_generator.rb +9 -9
- data/lib/generators/api_me/filter/filter_generator.rb +9 -9
- data/lib/generators/api_me/policy/policy_generator.rb +9 -9
- data/lib/generators/api_me/resource/resource_generator.rb +1 -1
- data/spec/acceptance/api/v1/posts_spec.rb +7 -13
- data/spec/internal/app/models/test_model.rb +1 -2
- data/spec/internal/app/policies/user_policy.rb +1 -1
- data/spec/internal/db/schema.rb +2 -2
- data/spec/spec_helper.rb +0 -1
- metadata +12 -12
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 645e13a4bad82a3438471c239b114e274c8359f8
|
|
4
|
+
data.tar.gz: 4fd73fa2d420709e31ed318f3b8811c67c93f7e6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 22cb87900699c9c798aba454a68f55dcd089a4eab02a7256bb165ab41690950ec6a426dcbd88b4fea165fa1c228273c3daac6788b494b472d4ad50e988d32405
|
|
7
|
+
data.tar.gz: bf3d38090a2960accb601856c2f372719fbcd8410087df49f262074496f555d8b85fbe7c1df2b1d983e1e822c6e0f7de159146f24c8103ded53495a91232869e
|
data/Gemfile
CHANGED
|
@@ -3,12 +3,9 @@ source 'https://rubygems.org'
|
|
|
3
3
|
gemspec
|
|
4
4
|
|
|
5
5
|
group :test do
|
|
6
|
-
gem '
|
|
7
|
-
gem 'activerecord'
|
|
8
|
-
gem 'activesupport'
|
|
9
|
-
gem 'actionpack' # action_controller, action_view
|
|
10
|
-
gem 'combustion', '~> 0.5.2'
|
|
11
|
-
gem 'rack-test'
|
|
12
|
-
gem 'pundit'
|
|
6
|
+
gem 'actionpack'
|
|
13
7
|
gem 'active_model_serializers', '~> 0.10.0'
|
|
8
|
+
gem 'combustion', '~> 0.5.2'
|
|
9
|
+
gem 'pry'
|
|
10
|
+
gem 'rack-test', '~> 0.6.2'
|
|
14
11
|
end
|
data/Gemfile.lock
CHANGED
|
@@ -1,119 +1,138 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
api_me (0.
|
|
4
|
+
api_me (0.9.0)
|
|
5
5
|
active_model_serializers (~> 0.10.0)
|
|
6
|
-
activerecord (>=
|
|
7
|
-
activesupport (>=
|
|
6
|
+
activerecord (>= 4.1.16)
|
|
7
|
+
activesupport (>= 4.1.16)
|
|
8
8
|
kaminari (~> 0.16.3)
|
|
9
|
-
pundit (~> 1.0)
|
|
9
|
+
pundit (~> 1.1.0)
|
|
10
10
|
search_object (~> 1.0)
|
|
11
11
|
|
|
12
12
|
GEM
|
|
13
13
|
remote: https://rubygems.org/
|
|
14
14
|
specs:
|
|
15
|
-
actionpack (4.
|
|
16
|
-
actionview (= 4.
|
|
17
|
-
activesupport (= 4.
|
|
18
|
-
rack (~> 1.
|
|
15
|
+
actionpack (4.2.10)
|
|
16
|
+
actionview (= 4.2.10)
|
|
17
|
+
activesupport (= 4.2.10)
|
|
18
|
+
rack (~> 1.6)
|
|
19
19
|
rack-test (~> 0.6.2)
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
|
21
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
|
22
|
+
actionview (4.2.10)
|
|
23
|
+
activesupport (= 4.2.10)
|
|
22
24
|
builder (~> 3.1)
|
|
23
25
|
erubis (~> 2.7.0)
|
|
24
|
-
|
|
26
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
|
27
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
|
28
|
+
active_model_serializers (0.10.7)
|
|
25
29
|
actionpack (>= 4.1, < 6)
|
|
26
30
|
activemodel (>= 4.1, < 6)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
activemodel (4.
|
|
30
|
-
activesupport (= 4.
|
|
31
|
+
case_transform (>= 0.2)
|
|
32
|
+
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
|
|
33
|
+
activemodel (4.2.10)
|
|
34
|
+
activesupport (= 4.2.10)
|
|
31
35
|
builder (~> 3.1)
|
|
32
|
-
activerecord (4.
|
|
33
|
-
activemodel (= 4.
|
|
34
|
-
activesupport (= 4.
|
|
35
|
-
arel (~>
|
|
36
|
-
activesupport (4.
|
|
37
|
-
i18n (~> 0.
|
|
38
|
-
json (~> 1.7, >= 1.7.7)
|
|
36
|
+
activerecord (4.2.10)
|
|
37
|
+
activemodel (= 4.2.10)
|
|
38
|
+
activesupport (= 4.2.10)
|
|
39
|
+
arel (~> 6.0)
|
|
40
|
+
activesupport (4.2.10)
|
|
41
|
+
i18n (~> 0.7)
|
|
39
42
|
minitest (~> 5.1)
|
|
40
|
-
thread_safe (~> 0.
|
|
43
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
|
41
44
|
tzinfo (~> 1.1)
|
|
42
|
-
arel (
|
|
43
|
-
ast (2.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
coderay (1.1.
|
|
45
|
+
arel (6.0.4)
|
|
46
|
+
ast (2.4.0)
|
|
47
|
+
builder (3.2.3)
|
|
48
|
+
case_transform (0.2)
|
|
49
|
+
activesupport
|
|
50
|
+
coderay (1.1.2)
|
|
48
51
|
colored (1.2)
|
|
49
|
-
combustion (0.5.
|
|
52
|
+
combustion (0.5.5)
|
|
50
53
|
activesupport (>= 3.0.0)
|
|
51
54
|
railties (>= 3.0.0)
|
|
52
55
|
thor (>= 0.14.6)
|
|
53
|
-
|
|
56
|
+
concurrent-ruby (1.0.5)
|
|
57
|
+
crass (1.0.3)
|
|
58
|
+
diff-lcs (1.3)
|
|
54
59
|
erubis (2.7.0)
|
|
55
|
-
i18n (0.
|
|
56
|
-
|
|
57
|
-
jsonapi (0.
|
|
58
|
-
json (~> 1.8)
|
|
60
|
+
i18n (0.9.5)
|
|
61
|
+
concurrent-ruby (~> 1.0)
|
|
62
|
+
jsonapi-renderer (0.2.0)
|
|
59
63
|
kaminari (0.16.3)
|
|
60
64
|
actionpack (>= 3.0.0)
|
|
61
65
|
activesupport (>= 3.0.0)
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
loofah (2.2.2)
|
|
67
|
+
crass (~> 1.0.2)
|
|
68
|
+
nokogiri (>= 1.5.9)
|
|
69
|
+
method_source (0.9.0)
|
|
70
|
+
mini_portile2 (2.3.0)
|
|
71
|
+
minitest (5.11.3)
|
|
72
|
+
nokogiri (1.8.2)
|
|
73
|
+
mini_portile2 (~> 2.3.0)
|
|
74
|
+
parallel (1.12.1)
|
|
75
|
+
parser (2.5.0.4)
|
|
76
|
+
ast (~> 2.4.0)
|
|
77
|
+
powerpack (0.1.1)
|
|
78
|
+
pry (0.11.3)
|
|
69
79
|
coderay (~> 1.1.0)
|
|
70
|
-
method_source (~> 0.
|
|
71
|
-
|
|
72
|
-
pundit (1.0.1)
|
|
80
|
+
method_source (~> 0.9.0)
|
|
81
|
+
pundit (1.1.0)
|
|
73
82
|
activesupport (>= 3.0.0)
|
|
74
|
-
rack (1.
|
|
75
|
-
rack-test (0.6.
|
|
83
|
+
rack (1.6.9)
|
|
84
|
+
rack-test (0.6.3)
|
|
76
85
|
rack (>= 1.0)
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
86
|
+
rails-deprecated_sanitizer (1.0.3)
|
|
87
|
+
activesupport (>= 4.2.0.alpha)
|
|
88
|
+
rails-dom-testing (1.0.9)
|
|
89
|
+
activesupport (>= 4.2.0, < 5.0)
|
|
90
|
+
nokogiri (~> 1.6)
|
|
91
|
+
rails-deprecated_sanitizer (>= 1.0.1)
|
|
92
|
+
rails-html-sanitizer (1.0.3)
|
|
93
|
+
loofah (~> 2.0)
|
|
94
|
+
railties (4.2.10)
|
|
95
|
+
actionpack (= 4.2.10)
|
|
96
|
+
activesupport (= 4.2.10)
|
|
80
97
|
rake (>= 0.8.7)
|
|
81
98
|
thor (>= 0.18.1, < 2.0)
|
|
82
|
-
rainbow (
|
|
83
|
-
rake (
|
|
99
|
+
rainbow (3.0.0)
|
|
100
|
+
rake (12.3.1)
|
|
84
101
|
rake-notes (0.2.0)
|
|
85
102
|
colored
|
|
86
103
|
rake
|
|
87
|
-
rspec-core (3.1
|
|
88
|
-
rspec-support (~> 3.
|
|
89
|
-
rspec-expectations (3.
|
|
104
|
+
rspec-core (3.7.1)
|
|
105
|
+
rspec-support (~> 3.7.0)
|
|
106
|
+
rspec-expectations (3.7.0)
|
|
107
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
108
|
+
rspec-support (~> 3.7.0)
|
|
109
|
+
rspec-mocks (3.7.0)
|
|
90
110
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
91
|
-
rspec-support (~> 3.
|
|
92
|
-
rspec-
|
|
93
|
-
rspec-support (~> 3.1.0)
|
|
94
|
-
rspec-rails (3.1.0)
|
|
111
|
+
rspec-support (~> 3.7.0)
|
|
112
|
+
rspec-rails (3.7.2)
|
|
95
113
|
actionpack (>= 3.0)
|
|
96
114
|
activesupport (>= 3.0)
|
|
97
115
|
railties (>= 3.0)
|
|
98
|
-
rspec-core (~> 3.
|
|
99
|
-
rspec-expectations (~> 3.
|
|
100
|
-
rspec-mocks (~> 3.
|
|
101
|
-
rspec-support (~> 3.
|
|
102
|
-
rspec-support (3.1
|
|
103
|
-
rubocop (0.
|
|
104
|
-
|
|
105
|
-
parser (>= 2.
|
|
106
|
-
powerpack (~> 0.
|
|
107
|
-
rainbow (>=
|
|
108
|
-
ruby-progressbar (~> 1.
|
|
109
|
-
|
|
116
|
+
rspec-core (~> 3.7.0)
|
|
117
|
+
rspec-expectations (~> 3.7.0)
|
|
118
|
+
rspec-mocks (~> 3.7.0)
|
|
119
|
+
rspec-support (~> 3.7.0)
|
|
120
|
+
rspec-support (3.7.1)
|
|
121
|
+
rubocop (0.54.0)
|
|
122
|
+
parallel (~> 1.10)
|
|
123
|
+
parser (>= 2.5)
|
|
124
|
+
powerpack (~> 0.1)
|
|
125
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
126
|
+
ruby-progressbar (~> 1.7)
|
|
127
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
|
128
|
+
ruby-progressbar (1.9.0)
|
|
110
129
|
search_object (1.2.0)
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
tzinfo (1.2.2)
|
|
130
|
+
sqlite3 (1.3.13)
|
|
131
|
+
thor (0.20.0)
|
|
132
|
+
thread_safe (0.3.6)
|
|
133
|
+
tzinfo (1.2.5)
|
|
116
134
|
thread_safe (~> 0.1)
|
|
135
|
+
unicode-display_width (1.3.0)
|
|
117
136
|
|
|
118
137
|
PLATFORMS
|
|
119
138
|
ruby
|
|
@@ -121,14 +140,14 @@ PLATFORMS
|
|
|
121
140
|
DEPENDENCIES
|
|
122
141
|
actionpack
|
|
123
142
|
active_model_serializers (~> 0.10.0)
|
|
124
|
-
activerecord
|
|
125
|
-
activesupport
|
|
126
143
|
api_me!
|
|
127
144
|
combustion (~> 0.5.2)
|
|
128
145
|
pry
|
|
129
|
-
|
|
130
|
-
rack-test
|
|
146
|
+
rack-test (~> 0.6.2)
|
|
131
147
|
rake-notes (>= 0.2.0)
|
|
132
148
|
rspec-rails (~> 3)
|
|
133
|
-
rubocop (
|
|
149
|
+
rubocop (~> 0.49)
|
|
134
150
|
sqlite3 (~> 1.3.7)
|
|
151
|
+
|
|
152
|
+
BUNDLED WITH
|
|
153
|
+
1.16.1
|
data/README.md
CHANGED
|
@@ -121,6 +121,45 @@ return this.store.query('some-model', {
|
|
|
121
121
|
});
|
|
122
122
|
````
|
|
123
123
|
|
|
124
|
+
### Pagination
|
|
125
|
+
|
|
126
|
+
To enable pagination just pass `page` in your request with `size` and `offset`.
|
|
127
|
+
|
|
128
|
+
Ember Example
|
|
129
|
+
````js
|
|
130
|
+
return this.store.query('some-model', {
|
|
131
|
+
filters: {
|
|
132
|
+
cool_models_only: true
|
|
133
|
+
},
|
|
134
|
+
sort: {
|
|
135
|
+
criteria: 'createdAt', // Property to sort on.
|
|
136
|
+
reverse: false, // True reverses the sort.
|
|
137
|
+
},
|
|
138
|
+
page: {
|
|
139
|
+
size: 50, // Size of a page. Number of results per page.
|
|
140
|
+
offset: false, /* The page number you want to see.
|
|
141
|
+
Imagine 103 records are available.
|
|
142
|
+
If the size of 50 is given as a parameter
|
|
143
|
+
then valid inputs to offset would be 1, 2, and 3. */
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
````
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
Your response will contain a meta object with the following data:
|
|
150
|
+
```js
|
|
151
|
+
meta: {
|
|
152
|
+
size: n // Size input above
|
|
153
|
+
offset: m // Offset input above
|
|
154
|
+
record_count: i /* The number of records in the page.
|
|
155
|
+
In the `offset` example above pages 1 and 2
|
|
156
|
+
would have a record_count of 50 while page 3
|
|
157
|
+
would have a record count of 3. */
|
|
158
|
+
total_records: j // Total number of records accessible
|
|
159
|
+
total_pages: k // Total number of pages accessible
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
124
163
|
### Overrides
|
|
125
164
|
Overriding the default model class, serializer class, filter class, and filter parameter can be done like so:
|
|
126
165
|
|
data/Rakefile
CHANGED
data/api_me.gemspec
CHANGED
|
@@ -18,9 +18,9 @@ Gem::Specification.new do |s|
|
|
|
18
18
|
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
|
19
19
|
s.require_paths = %w(lib app)
|
|
20
20
|
|
|
21
|
-
s.add_runtime_dependency 'activerecord', '>=
|
|
22
|
-
s.add_runtime_dependency 'activesupport', '>=
|
|
23
|
-
s.add_runtime_dependency 'pundit', '~> 1.0'
|
|
21
|
+
s.add_runtime_dependency 'activerecord', '>= 4.1.16'
|
|
22
|
+
s.add_runtime_dependency 'activesupport', '>= 4.1.16'
|
|
23
|
+
s.add_runtime_dependency 'pundit', '~> 1.1.0'
|
|
24
24
|
s.add_runtime_dependency 'active_model_serializers', '~> 0.10.0'
|
|
25
25
|
s.add_runtime_dependency 'search_object', '~> 1.0'
|
|
26
26
|
s.add_runtime_dependency 'kaminari', '~> 0.16.3'
|
|
@@ -28,6 +28,6 @@ Gem::Specification.new do |s|
|
|
|
28
28
|
s.add_development_dependency 'combustion', '~> 0.5.1'
|
|
29
29
|
s.add_development_dependency 'rspec-rails', '~> 3'
|
|
30
30
|
s.add_development_dependency 'sqlite3', '~> 1.3.7'
|
|
31
|
-
s.add_development_dependency 'rubocop', '
|
|
31
|
+
s.add_development_dependency 'rubocop', '~> 0.49'
|
|
32
32
|
s.add_development_dependency 'rake-notes', '>= 0.2.0'
|
|
33
33
|
end
|
data/lib/api_me.rb
CHANGED
|
@@ -18,11 +18,11 @@ module ApiMe
|
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
module ClassMethods
|
|
21
|
-
def model(klass)
|
|
21
|
+
def model(klass)
|
|
22
22
|
@model_klass = klass
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
def serializer(klass)
|
|
25
|
+
def serializer(klass)
|
|
26
26
|
@serializer_klass = klass
|
|
27
27
|
end
|
|
28
28
|
|
|
@@ -68,7 +68,7 @@ module ApiMe
|
|
|
68
68
|
def index
|
|
69
69
|
@policy_scope = policy_scope(resource_scope)
|
|
70
70
|
@filter_scope = filter_scope(@policy_scope)
|
|
71
|
-
@sorted_scope = sort_scope(@filter_scope
|
|
71
|
+
@sorted_scope = sort_scope(@filter_scope)
|
|
72
72
|
@pagination_object = paginate_scope(@sorted_scope, page_params)
|
|
73
73
|
|
|
74
74
|
render json: @pagination_object.results, root: collection_root_key, each_serializer: serializer_klass, meta: { page: @pagination_object.page_meta }
|
|
@@ -162,19 +162,26 @@ module ApiMe
|
|
|
162
162
|
self.class.filter_klass
|
|
163
163
|
end
|
|
164
164
|
|
|
165
|
+
def sort_klass
|
|
166
|
+
ApiMe::Sorting
|
|
167
|
+
end
|
|
168
|
+
|
|
165
169
|
def resource_scope
|
|
166
170
|
model_klass.all
|
|
167
171
|
end
|
|
168
172
|
|
|
169
173
|
def filter_scope(scope)
|
|
170
174
|
filter_klass.new(
|
|
171
|
-
|
|
172
|
-
|
|
175
|
+
scope: scope,
|
|
176
|
+
filters: filters_hash
|
|
173
177
|
).results
|
|
174
178
|
end
|
|
175
179
|
|
|
176
|
-
def sort_scope(scope
|
|
177
|
-
|
|
180
|
+
def sort_scope(scope)
|
|
181
|
+
sort_klass.new(
|
|
182
|
+
scope: scope,
|
|
183
|
+
sort_params: sort_hash
|
|
184
|
+
).results
|
|
178
185
|
end
|
|
179
186
|
|
|
180
187
|
def paginate_scope(scope, params)
|
|
@@ -194,6 +201,14 @@ module ApiMe
|
|
|
194
201
|
params[:filters]
|
|
195
202
|
end
|
|
196
203
|
|
|
204
|
+
def sort_hash
|
|
205
|
+
sort_params
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def sort_params
|
|
209
|
+
params[:sort]
|
|
210
|
+
end
|
|
211
|
+
|
|
197
212
|
def find_resource
|
|
198
213
|
model_klass.find_by_id!(params[:id])
|
|
199
214
|
end
|
data/lib/api_me/engine.rb
CHANGED
data/lib/api_me/pagination.rb
CHANGED
|
@@ -16,7 +16,7 @@ module ApiMe
|
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def page_meta
|
|
19
|
-
return
|
|
19
|
+
return {} unless paging?
|
|
20
20
|
{
|
|
21
21
|
size: page_size.nil? ? default_page_size : page_size,
|
|
22
22
|
offset: page_offset,
|
|
@@ -29,14 +29,12 @@ module ApiMe
|
|
|
29
29
|
protected
|
|
30
30
|
|
|
31
31
|
def page
|
|
32
|
-
self.scope = scope.page(
|
|
32
|
+
self.scope = scope.page(page_offset ? page_offset : 1)
|
|
33
33
|
self
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
def per
|
|
37
|
-
if page_size
|
|
38
|
-
self.scope = scope.per(page_size)
|
|
39
|
-
end
|
|
37
|
+
self.scope = scope.per(page_size) if page_size
|
|
40
38
|
self
|
|
41
39
|
end
|
|
42
40
|
|
|
@@ -49,6 +47,5 @@ module ApiMe
|
|
|
49
47
|
def paging?
|
|
50
48
|
page_size || page_offset
|
|
51
49
|
end
|
|
52
|
-
|
|
53
50
|
end
|
|
54
51
|
end
|
data/lib/api_me/sorting.rb
CHANGED
|
@@ -15,35 +15,34 @@ module ApiMe
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def sort_meta
|
|
18
|
-
return
|
|
18
|
+
return {} unless sorting?
|
|
19
19
|
{
|
|
20
|
-
criteria: sort_criteria.is_blank? || sort_criteria ===
|
|
20
|
+
criteria: sort_criteria.is_blank? || sort_criteria === '' ? default_sort_criteria : sort_criteria,
|
|
21
21
|
reverse: sort_reverse,
|
|
22
22
|
record_count: scope.size,
|
|
23
|
-
total_records: scope.total_count
|
|
23
|
+
total_records: scope.total_count
|
|
24
24
|
}
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
protected
|
|
28
28
|
|
|
29
29
|
def sort(criteria = default_sort_criteria)
|
|
30
|
-
if sort_reverse ===
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
self.scope = if sort_reverse === 'true'
|
|
31
|
+
scope.order(criteria => :desc)
|
|
32
|
+
else
|
|
33
|
+
scope.order(criteria => :asc)
|
|
34
|
+
end
|
|
35
35
|
self
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
private
|
|
39
39
|
|
|
40
40
|
def default_sort_criteria
|
|
41
|
-
sort_criteria =
|
|
41
|
+
sort_criteria = 'id'
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
def sorting?
|
|
45
45
|
sort_criteria || sort_reverse
|
|
46
46
|
end
|
|
47
|
-
|
|
48
47
|
end
|
|
49
48
|
end
|
data/lib/api_me/version.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module ApiMe
|
|
2
2
|
module Generators
|
|
3
3
|
class ControllerGenerator < ::Rails::Generators::NamedBase
|
|
4
|
-
source_root File.expand_path('
|
|
4
|
+
source_root File.expand_path('templates', __dir__)
|
|
5
5
|
check_class_collision suffix: 'Controller'
|
|
6
6
|
|
|
7
7
|
argument :attributes, type: :array, default: [], banner: 'field field'
|
|
@@ -33,7 +33,7 @@ module ApiMe
|
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
def attributes_names
|
|
36
|
-
[:id] + attributes.
|
|
36
|
+
[:id] + attributes.reject(&:reference?).map { |a| a.name.to_sym }
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
def associations
|
|
@@ -41,19 +41,19 @@ module ApiMe
|
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
def nonpolymorphic_attribute_names
|
|
44
|
-
associations.select { |attr| attr.type.in?([
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
associations.select { |attr| attr.type.in?(%i[belongs_to references]) }
|
|
45
|
+
.reject { |attr| attr.attr_options.fetch(:polymorphic, false) }
|
|
46
|
+
.map { |attr| "#{attr.name}_id".to_sym }
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
def polymorphic_attribute_names
|
|
50
|
-
associations.select { |attr| attr.type.in?([
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
associations.select { |attr| attr.type.in?(%i[belongs_to references]) }
|
|
51
|
+
.select { |attr| attr.attr_options.fetch(:polymorphic, false) }
|
|
52
|
+
.map { |attr| ["#{attr.name}_id".to_sym, "#{attr.name}_type".to_sym] }.flatten
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
def association_attribute_names
|
|
56
|
-
nonpolymorphic_attribute_names +
|
|
56
|
+
nonpolymorphic_attribute_names + polymorphic_attribute_names
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
def strong_parameters
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module ApiMe
|
|
2
2
|
module Generators
|
|
3
3
|
class FilterGenerator < ::Rails::Generators::NamedBase
|
|
4
|
-
source_root File.expand_path('
|
|
4
|
+
source_root File.expand_path('templates', __dir__)
|
|
5
5
|
check_class_collision suffix: 'Filter'
|
|
6
6
|
|
|
7
7
|
argument :attributes, type: :array, default: [], banner: 'field field'
|
|
@@ -17,7 +17,7 @@ module ApiMe
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def attributes_names
|
|
20
|
-
attributes.
|
|
20
|
+
attributes.reject(&:reference?).map { |a| a.name.to_sym }
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
def associations
|
|
@@ -25,19 +25,19 @@ module ApiMe
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
def nonpolymorphic_attribute_names
|
|
28
|
-
associations.select { |attr| attr.type.in?([
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
associations.select { |attr| attr.type.in?(%i[belongs_to references]) }
|
|
29
|
+
.reject { |attr| attr.attr_options.fetch(:polymorphic, false) }
|
|
30
|
+
.map { |attr| "#{attr.name}_id".to_sym }
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
def polymorphic_attribute_names
|
|
34
|
-
associations.select { |attr| attr.type.in?([
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
associations.select { |attr| attr.type.in?(%i[belongs_to references]) }
|
|
35
|
+
.select { |attr| attr.attr_options.fetch(:polymorphic, false) }
|
|
36
|
+
.map { |attr| ["#{attr.name}_id".to_sym, "#{attr.name}_type".to_sym] }.flatten
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
def association_attribute_names
|
|
40
|
-
nonpolymorphic_attribute_names +
|
|
40
|
+
nonpolymorphic_attribute_names + polymorphic_attribute_names
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
def strong_parameters
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module ApiMe
|
|
2
2
|
module Generators
|
|
3
3
|
class PolicyGenerator < ::Rails::Generators::NamedBase
|
|
4
|
-
source_root File.expand_path('
|
|
4
|
+
source_root File.expand_path('templates', __dir__)
|
|
5
5
|
check_class_collision suffix: 'Policy'
|
|
6
6
|
|
|
7
7
|
argument :attributes, type: :array, default: [], banner: 'field field'
|
|
@@ -17,7 +17,7 @@ module ApiMe
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def attributes_names
|
|
20
|
-
attributes.
|
|
20
|
+
attributes.reject(&:reference?).map { |a| a.name.to_sym }
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
def associations
|
|
@@ -25,19 +25,19 @@ module ApiMe
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
def nonpolymorphic_attribute_names
|
|
28
|
-
associations.select { |attr| attr.type.in?([
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
associations.select { |attr| attr.type.in?(%i[belongs_to references]) }
|
|
29
|
+
.reject { |attr| attr.attr_options.fetch(:polymorphic, false) }
|
|
30
|
+
.map { |attr| "#{attr.name}_id".to_sym }
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
def polymorphic_attribute_names
|
|
34
|
-
associations.select { |attr| attr.type.in?([
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
associations.select { |attr| attr.type.in?(%i[belongs_to references]) }
|
|
35
|
+
.select { |attr| attr.attr_options.fetch(:polymorphic, false) }
|
|
36
|
+
.map { |attr| ["#{attr.name}_id".to_sym, "#{attr.name}_type".to_sym] }.flatten
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
def association_attribute_names
|
|
40
|
-
nonpolymorphic_attribute_names +
|
|
40
|
+
nonpolymorphic_attribute_names + polymorphic_attribute_names
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
def strong_parameters
|
|
@@ -4,7 +4,7 @@ module ApiMe
|
|
|
4
4
|
def run_generators
|
|
5
5
|
params = @_initializer[0]
|
|
6
6
|
invoke 'model', params
|
|
7
|
-
invoke 'serializer', params + %w
|
|
7
|
+
invoke 'serializer', params + %w[created_at updated_at]
|
|
8
8
|
invoke 'api_me:policy', params
|
|
9
9
|
invoke 'api_me:controller', params
|
|
10
10
|
end
|
|
@@ -23,7 +23,6 @@ describe 'Posts API' do
|
|
|
23
23
|
get '/api/v1/posts?page%5Boffset%5D=1'
|
|
24
24
|
json = JSON.parse(last_response.body)
|
|
25
25
|
expect(json['posts'].length).to eq(25)
|
|
26
|
-
|
|
27
26
|
end
|
|
28
27
|
|
|
29
28
|
it 'is page offset of 2 working for size of 40 default page size of 25' do
|
|
@@ -34,11 +33,10 @@ describe 'Posts API' do
|
|
|
34
33
|
get '/api/v1/posts?page%5Boffset%5D=2'
|
|
35
34
|
json = JSON.parse(last_response.body)
|
|
36
35
|
expect(json['posts'].length).to eq(15)
|
|
37
|
-
|
|
38
36
|
end
|
|
39
37
|
|
|
40
38
|
it 'is page offset of -1 working for size of 40 default page size of 25' do
|
|
41
|
-
#min page offset is 1, anything less gets converted to 1 and results in page size records
|
|
39
|
+
# min page offset is 1, anything less gets converted to 1 and results in page size records
|
|
42
40
|
40.times do
|
|
43
41
|
Post.create(name: 'page item')
|
|
44
42
|
end
|
|
@@ -46,11 +44,10 @@ describe 'Posts API' do
|
|
|
46
44
|
get '/api/v1/posts?page%5Boffset%5D=-1'
|
|
47
45
|
json = JSON.parse(last_response.body)
|
|
48
46
|
expect(json['posts'].length).to eq(25)
|
|
49
|
-
|
|
50
47
|
end
|
|
51
48
|
|
|
52
49
|
it 'is page offset of 3 working for size of 40 default page size of 25' do
|
|
53
|
-
#overflow on page offset will result in 0 records
|
|
50
|
+
# overflow on page offset will result in 0 records
|
|
54
51
|
40.times do
|
|
55
52
|
Post.create(name: 'page item')
|
|
56
53
|
end
|
|
@@ -61,7 +58,7 @@ describe 'Posts API' do
|
|
|
61
58
|
end
|
|
62
59
|
|
|
63
60
|
it 'is page size of 10 working for default offset of 1' do
|
|
64
|
-
#overflow on page offset will result in 0 records
|
|
61
|
+
# overflow on page offset will result in 0 records
|
|
65
62
|
40.times do
|
|
66
63
|
Post.create(name: 'page item')
|
|
67
64
|
end
|
|
@@ -69,11 +66,10 @@ describe 'Posts API' do
|
|
|
69
66
|
get '/api/v1/posts?page%5Bsize%5D=10'
|
|
70
67
|
json = JSON.parse(last_response.body)
|
|
71
68
|
expect(json['posts'].length).to eq(10)
|
|
72
|
-
|
|
73
69
|
end
|
|
74
70
|
|
|
75
71
|
it 'is page size working when the value is negative' do
|
|
76
|
-
#overflow on page offset will result in 0 records
|
|
72
|
+
# overflow on page offset will result in 0 records
|
|
77
73
|
40.times do
|
|
78
74
|
Post.create(name: 'page item')
|
|
79
75
|
end
|
|
@@ -81,11 +77,10 @@ describe 'Posts API' do
|
|
|
81
77
|
get '/api/v1/posts?page%5Bsize%5D=-10'
|
|
82
78
|
json = JSON.parse(last_response.body)
|
|
83
79
|
expect(json['posts'].length).to eq(25)
|
|
84
|
-
|
|
85
80
|
end
|
|
86
81
|
|
|
87
82
|
it 'is page size working when the value is higher than the total record count' do
|
|
88
|
-
#overflow on page offset will result in 0 records
|
|
83
|
+
# overflow on page offset will result in 0 records
|
|
89
84
|
40.times do
|
|
90
85
|
Post.create(name: 'page item')
|
|
91
86
|
end
|
|
@@ -96,7 +91,7 @@ describe 'Posts API' do
|
|
|
96
91
|
end
|
|
97
92
|
|
|
98
93
|
it 'is the result all records when no page paramas are sent' do
|
|
99
|
-
#overflow on page offset will result in 0 records
|
|
94
|
+
# overflow on page offset will result in 0 records
|
|
100
95
|
100.times do
|
|
101
96
|
Post.create(name: 'page item')
|
|
102
97
|
end
|
|
@@ -104,7 +99,6 @@ describe 'Posts API' do
|
|
|
104
99
|
get '/api/v1/posts'
|
|
105
100
|
json = JSON.parse(last_response.body)
|
|
106
101
|
expect(json['posts'].length).to eq(100)
|
|
107
|
-
|
|
108
102
|
end
|
|
109
103
|
|
|
110
104
|
it 'restricts page_size when max_per_page config is less' do
|
|
@@ -129,7 +123,7 @@ describe 'Posts API' do
|
|
|
129
123
|
filtered_posts = [all_posts[0], all_posts[2]]
|
|
130
124
|
|
|
131
125
|
get '/api/v1/posts?ids%5B%5D=' + filtered_posts[0].id.to_s +
|
|
132
|
-
|
|
126
|
+
'&ids%5B%5D=' + filtered_posts[1].id.to_s
|
|
133
127
|
|
|
134
128
|
expect(last_response.status).to eq(200)
|
|
135
129
|
json = JSON.parse(last_response.body)
|
data/spec/internal/db/schema.rb
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
ActiveRecord::Schema.define do
|
|
2
2
|
create_table :users, force: true do |t|
|
|
3
3
|
t.string :username
|
|
4
|
-
t.timestamps
|
|
4
|
+
t.timestamps null: false
|
|
5
5
|
end
|
|
6
6
|
|
|
7
7
|
create_table :posts, force: true do |t|
|
|
8
8
|
t.string :name
|
|
9
9
|
t.belongs_to :user, foreign_key: true
|
|
10
|
-
t.timestamps
|
|
10
|
+
t.timestamps null: false
|
|
11
11
|
end
|
|
12
12
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: api_me
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.9.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sam Clopton
|
|
@@ -9,7 +9,7 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date:
|
|
12
|
+
date: 2018-03-22 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: activerecord
|
|
@@ -17,42 +17,42 @@ dependencies:
|
|
|
17
17
|
requirements:
|
|
18
18
|
- - ">="
|
|
19
19
|
- !ruby/object:Gem::Version
|
|
20
|
-
version:
|
|
20
|
+
version: 4.1.16
|
|
21
21
|
type: :runtime
|
|
22
22
|
prerelease: false
|
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
|
24
24
|
requirements:
|
|
25
25
|
- - ">="
|
|
26
26
|
- !ruby/object:Gem::Version
|
|
27
|
-
version:
|
|
27
|
+
version: 4.1.16
|
|
28
28
|
- !ruby/object:Gem::Dependency
|
|
29
29
|
name: activesupport
|
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
|
31
31
|
requirements:
|
|
32
32
|
- - ">="
|
|
33
33
|
- !ruby/object:Gem::Version
|
|
34
|
-
version:
|
|
34
|
+
version: 4.1.16
|
|
35
35
|
type: :runtime
|
|
36
36
|
prerelease: false
|
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
|
38
38
|
requirements:
|
|
39
39
|
- - ">="
|
|
40
40
|
- !ruby/object:Gem::Version
|
|
41
|
-
version:
|
|
41
|
+
version: 4.1.16
|
|
42
42
|
- !ruby/object:Gem::Dependency
|
|
43
43
|
name: pundit
|
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
|
45
45
|
requirements:
|
|
46
46
|
- - "~>"
|
|
47
47
|
- !ruby/object:Gem::Version
|
|
48
|
-
version:
|
|
48
|
+
version: 1.1.0
|
|
49
49
|
type: :runtime
|
|
50
50
|
prerelease: false
|
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
|
52
52
|
requirements:
|
|
53
53
|
- - "~>"
|
|
54
54
|
- !ruby/object:Gem::Version
|
|
55
|
-
version:
|
|
55
|
+
version: 1.1.0
|
|
56
56
|
- !ruby/object:Gem::Dependency
|
|
57
57
|
name: active_model_serializers
|
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -141,16 +141,16 @@ dependencies:
|
|
|
141
141
|
name: rubocop
|
|
142
142
|
requirement: !ruby/object:Gem::Requirement
|
|
143
143
|
requirements:
|
|
144
|
-
- - "
|
|
144
|
+
- - "~>"
|
|
145
145
|
- !ruby/object:Gem::Version
|
|
146
|
-
version: 0.
|
|
146
|
+
version: '0.49'
|
|
147
147
|
type: :development
|
|
148
148
|
prerelease: false
|
|
149
149
|
version_requirements: !ruby/object:Gem::Requirement
|
|
150
150
|
requirements:
|
|
151
|
-
- - "
|
|
151
|
+
- - "~>"
|
|
152
152
|
- !ruby/object:Gem::Version
|
|
153
|
-
version: 0.
|
|
153
|
+
version: '0.49'
|
|
154
154
|
- !ruby/object:Gem::Dependency
|
|
155
155
|
name: rake-notes
|
|
156
156
|
requirement: !ruby/object:Gem::Requirement
|