rails-rapido 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +52 -0
- data/.ruby-version +1 -0
- data/README.md +0 -4
- data/lib/rapido/api_controller.rb +63 -11
- data/lib/rapido/app_controller.rb +38 -38
- data/lib/rapido/auth/api_key.rb +1 -1
- data/lib/rapido/controller.rb +18 -18
- data/lib/rapido/version.rb +1 -1
- data/rapido.gemspec +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6d9f200d77def28ea4f71d5f762d6989f11005129072a3bcd711e0aac99a0e5
|
4
|
+
data.tar.gz: 8b150492c2c5a75fdb197453ca6e2be63206899486b379e5f0b85d05402cc17f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 073b9199bc61ab3d01ff15e8eee7d454137323fbcac21cdb156cd604b481acd99aced9ecdb612a75328bc10777c9b01812d1ca33e11b423d7410bfa08ae67568
|
7
|
+
data.tar.gz: 7f5cd3ab73eb1b7eaba8b9791e81c07ee41baf75da6ab4f3968bdf6395d124aadafee024fd84154f22ca2aa57ae5c95389d6c02259606ba774503d9018f747d4
|
data/.codeclimate.yml
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
version: "2"
|
2
|
+
checks:
|
3
|
+
argument-count:
|
4
|
+
enabled: true
|
5
|
+
config:
|
6
|
+
threshold: 4
|
7
|
+
complex-logic:
|
8
|
+
enabled: true
|
9
|
+
config:
|
10
|
+
threshold: 8
|
11
|
+
file-lines:
|
12
|
+
enabled: true
|
13
|
+
config:
|
14
|
+
threshold: 350
|
15
|
+
method-complexity:
|
16
|
+
enabled: true
|
17
|
+
config:
|
18
|
+
threshold: 8
|
19
|
+
method-count:
|
20
|
+
enabled: true
|
21
|
+
config:
|
22
|
+
threshold: 30
|
23
|
+
method-lines:
|
24
|
+
enabled: true
|
25
|
+
config:
|
26
|
+
threshold: 25
|
27
|
+
nested-control-flow:
|
28
|
+
enabled: true
|
29
|
+
config:
|
30
|
+
threshold: 4
|
31
|
+
return-statements:
|
32
|
+
enabled: false
|
33
|
+
similar-code:
|
34
|
+
enabled: false
|
35
|
+
identical-code:
|
36
|
+
enabled: false
|
37
|
+
plugins:
|
38
|
+
rubocop:
|
39
|
+
enabled: true
|
40
|
+
channel: rubocop-0-70
|
41
|
+
brakeman:
|
42
|
+
enabled: true
|
43
|
+
exclude_patterns:
|
44
|
+
- "config/"
|
45
|
+
- "db/"
|
46
|
+
- "dist/"
|
47
|
+
- "features/"
|
48
|
+
- "**/node_modules/"
|
49
|
+
- "script/"
|
50
|
+
- "public/"
|
51
|
+
- "tmp/"
|
52
|
+
- "**/vendor/"
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.3
|
data/README.md
CHANGED
@@ -76,10 +76,6 @@ Accepts a list of symbols. These symbols specify the attributes that are supplie
|
|
76
76
|
|
77
77
|
This will disallow any parameters in `create` and `update` actions.
|
78
78
|
|
79
|
-
#### `permit_all_params!`
|
80
|
-
|
81
|
-
This will permit all parameters in `create` and `update` actions.
|
82
|
-
|
83
79
|
## Notes
|
84
80
|
|
85
81
|
Authentication & AppController functionality will be deprecated in v0.6 and removed in v1.0. With the 1.0 release, Rapido will remove all functionality that is not strictly oriented to streamlining API Controller development and security.
|
@@ -13,6 +13,8 @@ module Rapido
|
|
13
13
|
rescue_from RecordNotFound do |e|
|
14
14
|
render json: { errors: [ e.to_s ] }, status: 404
|
15
15
|
end
|
16
|
+
|
17
|
+
before_action :permit_only_allowed_actions
|
16
18
|
end
|
17
19
|
|
18
20
|
def index
|
@@ -24,42 +26,92 @@ module Rapido
|
|
24
26
|
end
|
25
27
|
|
26
28
|
def create
|
29
|
+
before_build
|
27
30
|
new_resource = build_resource(resource_params)
|
31
|
+
before_create(new_resource)
|
28
32
|
if new_resource.save
|
33
|
+
after_create_success(new_resource)
|
34
|
+
return if performed?
|
29
35
|
render json: present_resource(new_resource), status: :created
|
30
36
|
else
|
37
|
+
after_create_failure(new_resource)
|
38
|
+
return if performed?
|
31
39
|
render json: { errors: new_resource.errors.full_messages }, status: 422
|
32
40
|
end
|
33
41
|
end
|
34
42
|
|
35
43
|
def destroy
|
36
44
|
resource_before_destruction = present_resource(resource)
|
45
|
+
before_destroy
|
37
46
|
resource.destroy
|
47
|
+
after_destroy_success
|
48
|
+
return if performed?
|
38
49
|
render json: resource_before_destruction
|
39
50
|
end
|
40
51
|
|
41
52
|
def update
|
53
|
+
before_assign_attributes
|
42
54
|
resource.assign_attributes(resource_params)
|
55
|
+
before_update
|
43
56
|
if resource.save
|
57
|
+
after_update_success
|
58
|
+
return if performed?
|
44
59
|
render json: present_resource(resource)
|
45
60
|
else
|
61
|
+
after_update_failure
|
62
|
+
return if performed?
|
46
63
|
render json: { errors: resource.errors.full_messages }, status: 422
|
47
64
|
end
|
48
65
|
end
|
49
66
|
|
50
67
|
private
|
51
68
|
|
52
|
-
|
53
|
-
|
54
|
-
return presenter.new(*[resource, *args]).as_json if presenter
|
55
|
-
resource.to_h
|
56
|
-
end
|
69
|
+
def after_create_failure(new_resource)
|
70
|
+
end
|
57
71
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
72
|
+
def after_create_success(new_resource)
|
73
|
+
end
|
74
|
+
|
75
|
+
def after_destroy_success
|
76
|
+
end
|
77
|
+
|
78
|
+
def after_update_failure
|
79
|
+
end
|
80
|
+
|
81
|
+
def after_update_success
|
82
|
+
end
|
83
|
+
|
84
|
+
def before_assign_attributes
|
85
|
+
end
|
86
|
+
|
87
|
+
def before_build
|
88
|
+
end
|
89
|
+
|
90
|
+
def before_create(new_resource)
|
91
|
+
end
|
92
|
+
|
93
|
+
def before_destroy
|
94
|
+
end
|
95
|
+
|
96
|
+
def before_update
|
97
|
+
end
|
98
|
+
|
99
|
+
def permit_only_allowed_actions
|
100
|
+
return unless allowed_actions
|
101
|
+
head :unauthorized unless allowed_actions.include?(params[:action].to_sym)
|
102
|
+
end
|
103
|
+
|
104
|
+
def present_resource(resource)
|
105
|
+
args = presenter_args.nil? ? nil : presenter_args.map { |arg| params[arg] }
|
106
|
+
return presenter.new(*[resource, *args]).as_json if presenter
|
107
|
+
resource.to_h
|
108
|
+
end
|
109
|
+
|
110
|
+
def present_resource_collection(resource_collection)
|
111
|
+
args = collection_presenter_args.nil? ? nil : collection_presenter_args.map { |arg| params[arg] }
|
112
|
+
return collection_presenter.new(*[resource_collection, *args]).as_json if collection_presenter
|
113
|
+
return resource_collection.map { |r| presenter.new(r).as_json } if presenter
|
114
|
+
resource_collection.map(&:to_h)
|
115
|
+
end
|
64
116
|
end
|
65
117
|
end
|
@@ -8,7 +8,7 @@ module Rapido
|
|
8
8
|
include Rapido::Errors
|
9
9
|
|
10
10
|
included do
|
11
|
-
Rails.logger.info
|
11
|
+
Rails.logger.info 'The AppController has been deprecated and will be removed in the v1.0 release.'
|
12
12
|
before_action do
|
13
13
|
resource_permitted_params
|
14
14
|
end
|
@@ -71,54 +71,54 @@ module Rapido
|
|
71
71
|
|
72
72
|
private
|
73
73
|
|
74
|
-
|
75
|
-
|
74
|
+
def after_create_success(*)
|
75
|
+
end
|
76
76
|
|
77
|
-
|
78
|
-
|
77
|
+
def after_create_failure(*)
|
78
|
+
end
|
79
79
|
|
80
|
-
|
81
|
-
|
80
|
+
def after_update_success(*)
|
81
|
+
end
|
82
82
|
|
83
|
-
|
84
|
-
|
83
|
+
def after_update_failure(*)
|
84
|
+
end
|
85
85
|
|
86
|
-
|
87
|
-
|
86
|
+
def after_destroy_success(*)
|
87
|
+
end
|
88
88
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
89
|
+
def resource_path(action, resource = nil)
|
90
|
+
keys = { controller: params[:controller], action: action }
|
91
|
+
keys[resource_lookup_param] = resource.send(resource_lookup_param) if resource
|
92
|
+
keys[owner_lookup_param] = owner.send(owner_lookup_field) if owner_lookup_param.present?
|
93
|
+
url_for(keys)
|
94
|
+
end
|
95
95
|
|
96
|
-
|
97
|
-
|
98
|
-
|
96
|
+
def edit_path(resource)
|
97
|
+
@edit_path ||= resource_path(:edit, resource)
|
98
|
+
end
|
99
99
|
|
100
|
-
|
101
|
-
|
102
|
-
|
100
|
+
def show_path(resource)
|
101
|
+
@show_path ||= resource_path(:show, resource)
|
102
|
+
end
|
103
103
|
|
104
|
-
|
105
|
-
|
106
|
-
|
104
|
+
def new_path
|
105
|
+
@new_path ||= resource_path(:new)
|
106
|
+
end
|
107
107
|
|
108
|
-
|
109
|
-
|
110
|
-
|
108
|
+
def index_path
|
109
|
+
@index_path ||= resource_path(:index)
|
110
|
+
end
|
111
111
|
|
112
|
-
|
113
|
-
|
114
|
-
|
112
|
+
def after_create_path(resource)
|
113
|
+
show_path(resource)
|
114
|
+
end
|
115
115
|
|
116
|
-
|
117
|
-
|
118
|
-
|
116
|
+
def after_update_path(resource)
|
117
|
+
show_path(resource)
|
118
|
+
end
|
119
119
|
|
120
|
-
|
121
|
-
|
122
|
-
|
120
|
+
def after_delete_path(*)
|
121
|
+
index_path
|
122
|
+
end
|
123
123
|
end
|
124
124
|
end
|
data/lib/rapido/auth/api_key.rb
CHANGED
@@ -4,7 +4,7 @@ module Rapido
|
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
6
|
included do
|
7
|
-
Rails.logger.info
|
7
|
+
Rails.logger.info 'The Auth module has been deprecated and will be removed in the 1.0 release.'
|
8
8
|
before_action :load_authority
|
9
9
|
|
10
10
|
rescue_from LackAuthority do |e|
|
data/lib/rapido/controller.rb
CHANGED
@@ -24,6 +24,14 @@ module Rapido
|
|
24
24
|
end
|
25
25
|
|
26
26
|
class_methods do
|
27
|
+
def allow_only(*ary)
|
28
|
+
@allowed_actions = ary
|
29
|
+
end
|
30
|
+
|
31
|
+
def attr_permitted(*ary)
|
32
|
+
@resource_permitted_params = ary
|
33
|
+
end
|
34
|
+
|
27
35
|
def belongs_to(sym, opts = {})
|
28
36
|
@owner_class = sym.to_sym
|
29
37
|
define_method @owner_class do
|
@@ -55,7 +63,7 @@ module Rapido
|
|
55
63
|
end
|
56
64
|
|
57
65
|
def owner_lookup_param(*args)
|
58
|
-
return @owner_lookup_param =
|
66
|
+
return @owner_lookup_param = args[0].to_sym if args.count == 1
|
59
67
|
@owner_lookup_param = args.join('_').to_sym
|
60
68
|
end
|
61
69
|
|
@@ -71,14 +79,6 @@ module Rapido
|
|
71
79
|
@resource_lookup_param = str.to_sym
|
72
80
|
end
|
73
81
|
|
74
|
-
def attr_permitted(*ary)
|
75
|
-
@resource_permitted_params = ary
|
76
|
-
end
|
77
|
-
|
78
|
-
def permit_all_params!
|
79
|
-
@permit_all_params = true
|
80
|
-
end
|
81
|
-
|
82
82
|
def resource_class_from_controller
|
83
83
|
self.name.split('::')[-1].remove('Controller').singularize.underscore
|
84
84
|
end
|
@@ -95,13 +95,17 @@ module Rapido
|
|
95
95
|
|
96
96
|
private
|
97
97
|
|
98
|
+
def allowed_actions
|
99
|
+
setting(:allowed_actions)
|
100
|
+
end
|
101
|
+
|
98
102
|
def build_resource(params = {})
|
99
|
-
return owner.send(
|
103
|
+
return owner.send('build_' + resource_class_name, params) if setting(:has_one)
|
100
104
|
return owner.send(resource_class_name.pluralize).build(params) if owner && owner.respond_to?(resource_class_name.pluralize)
|
101
105
|
begin
|
102
106
|
send(:build)
|
103
107
|
rescue NoMethodError
|
104
|
-
raise
|
108
|
+
raise 'Rapido::Controller must belong to something that responds to build or define a build method'
|
105
109
|
end
|
106
110
|
end
|
107
111
|
|
@@ -174,9 +178,9 @@ module Rapido
|
|
174
178
|
.find_by!(resource_lookup_param => params[resource_lookup_param])
|
175
179
|
else
|
176
180
|
begin NoMethodError
|
177
|
-
|
181
|
+
send(:find)
|
178
182
|
rescue
|
179
|
-
raise
|
183
|
+
raise 'Rapido::Controller must belong to something that has many or has one of resource, or define a find method'
|
180
184
|
end
|
181
185
|
end
|
182
186
|
rescue ActiveRecord::RecordNotFound
|
@@ -216,11 +220,7 @@ module Rapido
|
|
216
220
|
def resource_params
|
217
221
|
return {} if setting(:permit_no_params)
|
218
222
|
base = params.require(resource_class_name)
|
219
|
-
|
220
|
-
base.permit!
|
221
|
-
else
|
222
|
-
base.permit(resource_permitted_params)
|
223
|
-
end
|
223
|
+
base.permit(resource_permitted_params)
|
224
224
|
end
|
225
225
|
|
226
226
|
def setting(var)
|
data/lib/rapido/version.rb
CHANGED
data/rapido.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
f.match(%r{^(test|spec|features)/})
|
19
19
|
end
|
20
20
|
spec.bindir = 'bin'
|
21
|
-
spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
spec.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
22
22
|
spec.require_paths = ['lib']
|
23
23
|
|
24
24
|
spec.add_development_dependency 'bundler', '> 1.15'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-rapido
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Kirst
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-06-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -47,8 +47,10 @@ executables:
|
|
47
47
|
extensions: []
|
48
48
|
extra_rdoc_files: []
|
49
49
|
files:
|
50
|
+
- ".codeclimate.yml"
|
50
51
|
- ".gitignore"
|
51
52
|
- ".rubocop.yml"
|
53
|
+
- ".ruby-version"
|
52
54
|
- CODE_OF_CONDUCT.md
|
53
55
|
- Gemfile
|
54
56
|
- LICENSE
|
@@ -89,8 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
91
|
- !ruby/object:Gem::Version
|
90
92
|
version: '0'
|
91
93
|
requirements: []
|
92
|
-
|
93
|
-
rubygems_version: 2.7.6
|
94
|
+
rubygems_version: 3.0.3
|
94
95
|
signing_key:
|
95
96
|
specification_version: 4
|
96
97
|
summary: Rails API Dryer-o
|