rao-api-resources_controller 0.0.48.pre → 0.0.50.pre
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/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 +17 -6
- data/app/concerns/rao/api/resources_controller/serialization_concern.rb +2 -2
- data/app/concerns/rao/api/resources_controller/sorting_concern.rb +30 -0
- data/app/concerns/rao/api/resources_controller/validation_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 +10 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e51753ea3bc71fb2b7922f4797f44866b73458ed4c6aa10c3ab425e85362950b
|
4
|
+
data.tar.gz: 76a1ddc7c9522381e82745fa010c1a622aba6ebc41152004908c564a72bd6bae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f657f1639bcc4960bd5639913b025b87bb9665593fa8bc55fa5875739b83701f2fff9b3f7fd1105b9db88332b5cf77b1292b9cc753377dbff96466e2e905016
|
7
|
+
data.tar.gz: 619a871ff4c23783c93a149dbda62f98b901956f9d685670a6ef1971461bcea5c16aa5d1d06159cc9f60571b804f08731954fd79933bc2e469610f3de8a641f1
|
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 }
|
@@ -73,17 +73,24 @@ module Rao
|
|
73
73
|
|
74
74
|
private
|
75
75
|
|
76
|
-
def
|
77
|
-
|
78
|
-
|
76
|
+
def load_collection_scope
|
77
|
+
if respond_to?(:with_conditions_from_query, true)
|
78
|
+
with_conditions_from_query(resource_class)
|
79
79
|
else
|
80
80
|
resource_class
|
81
81
|
end
|
82
|
-
|
82
|
+
end
|
83
|
+
|
84
|
+
def load_collection
|
85
|
+
@collection = load_collection_scope.all
|
86
|
+
end
|
87
|
+
|
88
|
+
def load_resource_scope
|
89
|
+
resource_class
|
83
90
|
end
|
84
91
|
|
85
92
|
def load_resource
|
86
|
-
@resource =
|
93
|
+
@resource = load_resource_scope.find(params[:id])
|
87
94
|
end
|
88
95
|
|
89
96
|
def load_resource_for_show
|
@@ -102,6 +109,10 @@ module Rao
|
|
102
109
|
@resource = resource_class.new(permitted_params)
|
103
110
|
end
|
104
111
|
|
112
|
+
def params
|
113
|
+
super.deep_transform_keys!(&:underscore)
|
114
|
+
end
|
115
|
+
|
105
116
|
def permitted_params
|
106
117
|
raise "not implemented"
|
107
118
|
end
|
@@ -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,30 @@
|
|
1
|
+
module Rao
|
2
|
+
module Api
|
3
|
+
module ResourcesController
|
4
|
+
module ValidationConcern
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
before_action :initialize_resource_for_validation, only: [:validate]
|
9
|
+
end
|
10
|
+
|
11
|
+
def validate
|
12
|
+
respond_to do |format|
|
13
|
+
if @resource.valid?
|
14
|
+
format.json { render json: { errors: serialize_errors(@resource.errors, full_messages: false) }, status: 200 }
|
15
|
+
# format.json { render json: serialize_resource(@resource), status: :created }
|
16
|
+
else
|
17
|
+
format.json { render json: { errors: serialize_errors(@resource.errors, full_messages: false) }, status: 422 }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def initialize_resource_for_validation
|
25
|
+
initialize_resource_for_create
|
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.50.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-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -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,9 @@ 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/concerns/rao/api/resources_controller/validation_concern.rb
|
178
|
+
- app/controllers/rao/api/resource_controller/base.rb
|
172
179
|
- app/controllers/rao/api/resources_controller/base.rb
|
173
180
|
- config/locales/de.yml
|
174
181
|
- config/locales/en.yml
|
@@ -199,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
199
206
|
- !ruby/object:Gem::Version
|
200
207
|
version: 1.3.1
|
201
208
|
requirements: []
|
202
|
-
rubygems_version: 3.
|
209
|
+
rubygems_version: 3.4.20
|
203
210
|
signing_key:
|
204
211
|
specification_version: 4
|
205
212
|
summary: API Resources Controller for Ruby on Rails.
|