rao-api-resources_controller 0.0.47.pre → 0.0.49.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -0
- data/app/concerns/rao/api/resource_controller/exception_handling_concern.rb +42 -0
- data/app/concerns/rao/api/resource_controller/resource_concern.rb +11 -0
- data/app/concerns/rao/api/resource_controller/rest_actions_concern.rb +87 -0
- data/app/concerns/rao/api/resource_controller/serialization_concern.rb +19 -0
- data/app/concerns/rao/api/resources_controller/rest_actions_concern.rb +1 -1
- data/app/concerns/rao/api/resources_controller/sorting_concern.rb +30 -0
- data/app/controllers/rao/api/resource_controller/base.rb +12 -0
- data/app/controllers/rao/api/resources_controller/base.rb +1 -0
- metadata +14 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7502d725f38fe2455137199dc9ae053918c5f94621ad475b26166290de74d3e6
|
4
|
+
data.tar.gz: 201c4dc8a40768f51cc941998c140d043c333096884df865eee73d7f27e1b476
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a59c63daf6e4b05bb82dd7f9ced2dcceb1faef3bf23fc709149944d43b8a4fd6500af38055ac20bfa4f54902808af81ea54a34663726d832ae119cc99e4434d
|
7
|
+
data.tar.gz: 3571ce0c54415b95c17df3683e6437262885631f046965561774bbffcae8ee26803f8b94a673a7e5e8bc1d162f2352e3113c9afacfed6e1bea514300500c0cf1
|
data/README.md
CHANGED
@@ -42,6 +42,35 @@ module Api
|
|
42
42
|
end
|
43
43
|
```
|
44
44
|
|
45
|
+
### Sorting collections
|
46
|
+
|
47
|
+
You can specify the sorting via the params "sort_by" and "sort_direction".
|
48
|
+
|
49
|
+
### Including associated records
|
50
|
+
|
51
|
+
See rao-query.
|
52
|
+
|
53
|
+
### Filtering collections
|
54
|
+
|
55
|
+
See rao-query.
|
56
|
+
|
57
|
+
### Using the modules instead inheritance
|
58
|
+
|
59
|
+
Sometimes you want your controller to inherit from a specific controller. In
|
60
|
+
that case you can't use Rao::Api::ResourcesController::Base as the base controller.
|
61
|
+
Instead of using inheritance you can include the needed modules directly:
|
62
|
+
|
63
|
+
class CountriesController < ApplicationController
|
64
|
+
include Rao::Api::ResourcesController::RestActionsConcern
|
65
|
+
include Rao::Api::ResourcesController::ResourcesConcern
|
66
|
+
include Rao::Api::ResourcesController::SerializationConcern
|
67
|
+
include Rao::Api::ResourcesController::CountActionConcern
|
68
|
+
include Rao::Api::ResourcesController::DestroyAllActionConcern
|
69
|
+
include Rao::Api::ResourcesController::DeleteAllActionConcern
|
70
|
+
include Rao::Api::ResourcesController::FirstActionConcern
|
71
|
+
include Rao::Api::ResourcesController::LastActionConcern
|
72
|
+
include Rao::Api::ResourcesController::ExceptionHandlingConcern
|
73
|
+
end
|
45
74
|
|
46
75
|
## Installation
|
47
76
|
Add this line to your application's Gemfile:
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Rao
|
2
|
+
module Api
|
3
|
+
module ResourceController::ExceptionHandlingConcern
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
rescue_from Exception do |exception|
|
8
|
+
handle_exception(exception)
|
9
|
+
end
|
10
|
+
|
11
|
+
rescue_from ActiveRecord::RecordNotFound do |exception|
|
12
|
+
handle_404(exception)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def handle_404(exception = nil)
|
19
|
+
respond_to do |format|
|
20
|
+
format.json { render json: { error: (exception.try(:message) || 'Not found') }, status: 404 }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def handle_exception(exception)
|
25
|
+
if Rails.env.development? || Rails.env.test?
|
26
|
+
error = { message: exception.message }
|
27
|
+
|
28
|
+
error[:application_trace] = Rails.backtrace_cleaner.clean(exception.backtrace)
|
29
|
+
error[:full_trace] = exception.backtrace
|
30
|
+
|
31
|
+
respond_to do |format|
|
32
|
+
format.json { render json: error, status: 500 }
|
33
|
+
end
|
34
|
+
else
|
35
|
+
respond_to do |format|
|
36
|
+
format.json { render json: { error: 'Internal server error.' }, status: 500 }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module Rao
|
2
|
+
module Api
|
3
|
+
module ResourceController::RestActionsConcern
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
include ActionController::MimeResponds
|
8
|
+
|
9
|
+
respond_to :json
|
10
|
+
|
11
|
+
before_action :load_resource_for_show, only: [:show]
|
12
|
+
before_action :load_resource, only: [:update, :destroy, :delete]
|
13
|
+
before_action :initialize_resource_for_create, only: [:create]
|
14
|
+
end
|
15
|
+
|
16
|
+
def show
|
17
|
+
respond_to do |format|
|
18
|
+
if @resource.nil?
|
19
|
+
format.json { render json: { error: "Couldn't find #{resource_class} with ID=#{params[:id]}" }, status: :not_found }
|
20
|
+
else
|
21
|
+
format.json { render json: serialize_resource(@resource), status: :ok }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def create
|
27
|
+
respond_to do |format|
|
28
|
+
if @resource.save
|
29
|
+
format.json { render json: serialize_resource(@resource), status: :created }
|
30
|
+
else
|
31
|
+
format.json { render json: { errors: serialize_errors(@resource.errors) }, status: 422 }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def update
|
37
|
+
respond_to do |format|
|
38
|
+
if @resource.update(permitted_params)
|
39
|
+
format.json { render json: serialize_resource(@resource) }
|
40
|
+
else
|
41
|
+
format.json { render json: { errors: serialize_errors(@resource.errors) }, status: 422 }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def destroy
|
47
|
+
@resource.destroy
|
48
|
+
respond_to do |format|
|
49
|
+
format.json { render json: serialize_resource(@resource) }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def delete
|
54
|
+
@resource.delete
|
55
|
+
respond_to do |format|
|
56
|
+
format.json { render json: serialize_resource(@resource) }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def load_resource
|
63
|
+
raise "Please define #load_resource in #{self.class.name} and tell me how to load @resource."
|
64
|
+
end
|
65
|
+
|
66
|
+
def load_resource_for_show
|
67
|
+
begin
|
68
|
+
@resource = load_resource
|
69
|
+
rescue ActiveRecord::RecordNotFound
|
70
|
+
@resource = nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def initialize_resource
|
75
|
+
@resource = resource_class.new
|
76
|
+
end
|
77
|
+
|
78
|
+
def initialize_resource_for_create
|
79
|
+
@resource = resource_class.new(permitted_params)
|
80
|
+
end
|
81
|
+
|
82
|
+
def permitted_params
|
83
|
+
raise "not implemented"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Rao
|
2
|
+
module Api
|
3
|
+
module ResourceController::SerializationConcern
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
private
|
7
|
+
|
8
|
+
def serialize_resource(resource)
|
9
|
+
json = resource.as_json
|
10
|
+
json[:errors] = serialize_errors(resource.errors) if resource.errors.any?
|
11
|
+
json
|
12
|
+
end
|
13
|
+
|
14
|
+
def serialize_errors(errors)
|
15
|
+
errors.as_json(full_messages: true)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -49,7 +49,7 @@ module Rao
|
|
49
49
|
|
50
50
|
def update
|
51
51
|
respond_to do |format|
|
52
|
-
if @resource.
|
52
|
+
if @resource.update(permitted_params)
|
53
53
|
format.json { render json: serialize_resource(@resource) }
|
54
54
|
else
|
55
55
|
format.json { render json: { errors: serialize_errors(@resource.errors) }, status: 422 }
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Rao
|
2
|
+
module Api
|
3
|
+
module ResourcesController::SortingConcern
|
4
|
+
private
|
5
|
+
|
6
|
+
def load_collection_scope
|
7
|
+
add_order_scope(super)
|
8
|
+
end
|
9
|
+
|
10
|
+
def add_order_scope(base_scope)
|
11
|
+
if params[:sort_by].present?
|
12
|
+
if params[:sort_by].include?(' ') || params[:sort_direction].include?(' ')
|
13
|
+
raise "Possible SQL Injection attempt while trying to sort by #{params[:sort_by]} #{params[:sort_direction]}"
|
14
|
+
end
|
15
|
+
|
16
|
+
sort_by = params[:sort_by]
|
17
|
+
sort_direction = (params[:sort_direction] || :asc)
|
18
|
+
|
19
|
+
if sort_by.include?('.')
|
20
|
+
base_scope.reorder("#{sort_by} #{sort_direction}")
|
21
|
+
else
|
22
|
+
base_scope.reorder(sort_by => sort_direction)
|
23
|
+
end
|
24
|
+
else
|
25
|
+
base_scope
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Rao
|
2
|
+
module Api
|
3
|
+
module ResourceController
|
4
|
+
class Base < Rao::Api::ResourcesController::Configuration.resources_controller_base_class_name.constantize
|
5
|
+
include RestActionsConcern
|
6
|
+
include ResourceConcern
|
7
|
+
include SerializationConcern
|
8
|
+
include ExceptionHandlingConcern
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rao-api-resources_controller
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.49.pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roberto Vasquez Angel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-04-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '6.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '6.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rao
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 1.
|
47
|
+
version: '1.4'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 1.
|
54
|
+
version: '1.4'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: responders
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -160,6 +160,10 @@ files:
|
|
160
160
|
- MIT-LICENSE
|
161
161
|
- README.md
|
162
162
|
- Rakefile
|
163
|
+
- app/concerns/rao/api/resource_controller/exception_handling_concern.rb
|
164
|
+
- app/concerns/rao/api/resource_controller/resource_concern.rb
|
165
|
+
- app/concerns/rao/api/resource_controller/rest_actions_concern.rb
|
166
|
+
- app/concerns/rao/api/resource_controller/serialization_concern.rb
|
163
167
|
- app/concerns/rao/api/resources_controller/count_action_concern.rb
|
164
168
|
- app/concerns/rao/api/resources_controller/delete_all_action_concern.rb
|
165
169
|
- app/concerns/rao/api/resources_controller/destroy_all_action_concern.rb
|
@@ -169,6 +173,8 @@ files:
|
|
169
173
|
- app/concerns/rao/api/resources_controller/resources_concern.rb
|
170
174
|
- app/concerns/rao/api/resources_controller/rest_actions_concern.rb
|
171
175
|
- app/concerns/rao/api/resources_controller/serialization_concern.rb
|
176
|
+
- app/concerns/rao/api/resources_controller/sorting_concern.rb
|
177
|
+
- app/controllers/rao/api/resource_controller/base.rb
|
172
178
|
- app/controllers/rao/api/resources_controller/base.rb
|
173
179
|
- config/locales/de.yml
|
174
180
|
- config/locales/en.yml
|
@@ -192,14 +198,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
192
198
|
requirements:
|
193
199
|
- - ">="
|
194
200
|
- !ruby/object:Gem::Version
|
195
|
-
version:
|
201
|
+
version: 2.6.0
|
196
202
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
197
203
|
requirements:
|
198
204
|
- - ">"
|
199
205
|
- !ruby/object:Gem::Version
|
200
206
|
version: 1.3.1
|
201
207
|
requirements: []
|
202
|
-
rubygems_version: 3.
|
208
|
+
rubygems_version: 3.4.11
|
203
209
|
signing_key:
|
204
210
|
specification_version: 4
|
205
211
|
summary: API Resources Controller for Ruby on Rails.
|