api_me 0.10.5 → 0.13.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/lib/api_me.rb +14 -17
- data/lib/api_me/sorting.rb +17 -3
- data/lib/api_me/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6edbf66319db35e695cd3b4b686e3b5688a2c2fe56d108eb954976732bc11bb5
|
4
|
+
data.tar.gz: 6063c1ca77185b64178360c103f6c3511b6fe9652575ee00da2a99d97d626dfc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d08b61cc42bcaa27102961bea0b54c651e1fa18adbc7034d69531147076e1acdcd5fd09204c83741880b8b527a6269d0ee2634e339159910dd043a7b8bb664c4
|
7
|
+
data.tar.gz: 7c610c2d85ca5009c265c1456e9c3e21d653a762e933ce8f00d6f245dd2efa57fa480c9feee608a9c52a4ff2299bb3f4ed6fd60b471989fb9071e04ffca37589
|
data/lib/api_me.rb
CHANGED
@@ -17,6 +17,8 @@ module ApiMe
|
|
17
17
|
included do
|
18
18
|
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
|
19
19
|
rescue_from ActiveRecord::RecordInvalid, with: :handle_active_record_errors
|
20
|
+
rescue_from ActiveRecord::RecordNotDestroyed, with: :handle_active_record_errors
|
21
|
+
rescue_from ActiveRecord::ReadOnlyRecord, with: :handle_active_record_errors
|
20
22
|
rescue_from ActiveRecord::RecordNotFound, with: :resource_not_found
|
21
23
|
end
|
22
24
|
|
@@ -93,10 +95,7 @@ module ApiMe
|
|
93
95
|
end
|
94
96
|
|
95
97
|
def new
|
96
|
-
|
97
|
-
authorize_resource @object
|
98
|
-
|
99
|
-
render_errors(['new endpoint not supported'], 404)
|
98
|
+
render_errors(['new endpoint not supported'], :not_found)
|
100
99
|
end
|
101
100
|
|
102
101
|
def create
|
@@ -104,22 +103,20 @@ module ApiMe
|
|
104
103
|
authorize_resource @object
|
105
104
|
create_resource!
|
106
105
|
|
107
|
-
render status:
|
106
|
+
render status: :created, json: @object, root: singular_root_key, serializer: serializer_klass
|
108
107
|
end
|
109
108
|
|
110
109
|
def edit
|
111
|
-
|
112
|
-
authorize_resource @object
|
113
|
-
|
114
|
-
render_errors(['edit endpoint not supported'], 404)
|
110
|
+
render_errors(['edit endpoint not supported'], :not_found)
|
115
111
|
end
|
116
112
|
|
117
113
|
def update
|
118
114
|
@object = find_resource
|
115
|
+
@object.assign_attributes(object_params)
|
119
116
|
authorize_resource @object
|
120
117
|
update_resource!
|
121
118
|
|
122
|
-
|
119
|
+
render status: :ok, json: @object, root: singular_root_key, serializer: serializer_klass
|
123
120
|
end
|
124
121
|
|
125
122
|
def destroy
|
@@ -127,7 +124,7 @@ module ApiMe
|
|
127
124
|
authorize_resource @object
|
128
125
|
destroy_resource!
|
129
126
|
|
130
|
-
head
|
127
|
+
head :no_content
|
131
128
|
end
|
132
129
|
|
133
130
|
protected
|
@@ -152,7 +149,7 @@ module ApiMe
|
|
152
149
|
params[:sort]
|
153
150
|
end
|
154
151
|
|
155
|
-
def render_errors(errors, status =
|
152
|
+
def render_errors(errors, status = :unprocessable_entity)
|
156
153
|
render(json: { errors: errors }, status: status)
|
157
154
|
end
|
158
155
|
|
@@ -164,11 +161,11 @@ module ApiMe
|
|
164
161
|
|
165
162
|
def user_not_authorized
|
166
163
|
payload = { message: "User is not allowed to access #{params[:action]} on this resource" }
|
167
|
-
render json: payload, status:
|
164
|
+
render json: payload, status: :forbidden
|
168
165
|
end
|
169
166
|
|
170
167
|
def resource_not_found
|
171
|
-
head
|
168
|
+
head :not_found
|
172
169
|
end
|
173
170
|
|
174
171
|
def model_klass
|
@@ -230,7 +227,7 @@ module ApiMe
|
|
230
227
|
end
|
231
228
|
|
232
229
|
def find_resource
|
233
|
-
@find_resource ||= model_klass.
|
230
|
+
@find_resource ||= model_klass.find_by!(id: params[:id])
|
234
231
|
end
|
235
232
|
|
236
233
|
def build_resource
|
@@ -238,11 +235,11 @@ module ApiMe
|
|
238
235
|
end
|
239
236
|
|
240
237
|
def create_resource!
|
241
|
-
@object.save!
|
238
|
+
@object.save!
|
242
239
|
end
|
243
240
|
|
244
241
|
def update_resource!
|
245
|
-
@object.
|
242
|
+
@object.save!
|
246
243
|
end
|
247
244
|
|
248
245
|
def destroy_resource!
|
data/lib/api_me/sorting.rb
CHANGED
@@ -2,13 +2,16 @@
|
|
2
2
|
|
3
3
|
module ApiMe
|
4
4
|
class Sorting
|
5
|
-
attr_accessor :sort_criteria, :sort_reverse, :scope
|
5
|
+
attr_accessor :sort_criteria, :sort_reverse, :scope, :custom_sort_options
|
6
6
|
|
7
|
-
def initialize(scope:, sort_params:)
|
7
|
+
def initialize(scope:, sort_params:, custom_sort_options: {})
|
8
8
|
self.scope = scope
|
9
|
+
|
9
10
|
return unless sort_params
|
11
|
+
|
10
12
|
self.sort_criteria = sort_params[:criteria] || default_sort_criteria
|
11
13
|
self.sort_reverse = sort_params[:reverse] || false
|
14
|
+
self.custom_sort_options = custom_sort_options
|
12
15
|
end
|
13
16
|
|
14
17
|
def results
|
@@ -39,7 +42,14 @@ module ApiMe
|
|
39
42
|
end
|
40
43
|
|
41
44
|
def sorted_scope(criteria)
|
42
|
-
|
45
|
+
criteria_key = criteria.to_sym
|
46
|
+
if custom_sort_options.key?(criteria_key)
|
47
|
+
if sort_reverse == 'true'
|
48
|
+
custom_sort_scope(criteria_key).order(Arel.sql("#{custom_sort_options[criteria_key][:column]} DESC"))
|
49
|
+
else
|
50
|
+
custom_sort_scope(criteria_key).order(Arel.sql("#{custom_sort_options[criteria_key][:column]} ASC"))
|
51
|
+
end
|
52
|
+
elsif sort_reverse == 'true'
|
43
53
|
scope.order(criteria => :desc)
|
44
54
|
else
|
45
55
|
scope.order(criteria => :asc)
|
@@ -48,6 +58,10 @@ module ApiMe
|
|
48
58
|
|
49
59
|
private
|
50
60
|
|
61
|
+
def custom_sort_scope(criteria)
|
62
|
+
custom_sort_options[criteria].key?(:joins) ? scope.joins(custom_sort_options[criteria][:joins]) : scope
|
63
|
+
end
|
64
|
+
|
51
65
|
def default_sort_criteria
|
52
66
|
'id'
|
53
67
|
end
|
data/lib/api_me/version.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.13.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: 2020-08-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -199,7 +199,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
199
199
|
version: '0'
|
200
200
|
requirements: []
|
201
201
|
rubyforge_project:
|
202
|
-
rubygems_version: 2.7.6
|
202
|
+
rubygems_version: 2.7.6.2
|
203
203
|
signing_key:
|
204
204
|
specification_version: 4
|
205
205
|
summary: Api Me
|