ocean-rails 8.1.1 → 8.2.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/generators/ocean_scaffold_dynamo/templates/controller.rb +9 -12
- data/lib/ocean/ocean_application_controller.rb +9 -0
- data/lib/ocean/ocean_resource_controller.rb +10 -7
- data/lib/ocean/version.rb +1 -1
- data/lib/templates/rails/scaffold_controller/controller.rb +7 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d9060f6156d98438b9ce3dd4deba2fb97461ce44ece562fe3dcea701755c52d
|
4
|
+
data.tar.gz: a1a06a85ea3ccada5b17e83062db49a927936fc2d51f2262f9403d05a040d2f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '080c8feb9696e384a95ff51c1e9e3f56649c252ed9ad478d90e27c982d108177d395e79aabea3a937eb1041fb83a32b7f419e5cec60571401b46fd95afbea2d5'
|
7
|
+
data.tar.gz: 694b220450f368176bd3a5df6125efacd4d166494b2c29d815516327e0d10728819ec8c32366c5b4d71e0e725fb84205efc9e180e7158bf731b9d0871b714d23
|
@@ -8,8 +8,9 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
8
8
|
ocean_resource_controller
|
9
9
|
|
10
10
|
before_action :find_<%= singular_table_name %>, :only => [:show, :update, :destroy]
|
11
|
-
|
12
|
-
|
11
|
+
before_action :verify_attributes, only: :update
|
12
|
+
|
13
|
+
|
13
14
|
# GET <%= route_url %>
|
14
15
|
def index
|
15
16
|
expires_in 0, 's-maxage' => DEFAULT_CACHE_TIME
|
@@ -17,9 +18,9 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
17
18
|
# Instead, we get all the instances and compute the ETag manually.
|
18
19
|
# You may wish to remove this action entirely. You may also wish to remove the caching,
|
19
20
|
# in which case you don't need to compute the Etag at all.
|
20
|
-
@<%= plural_table_name %> = <%= class_name %>.all
|
21
|
+
@<%= plural_table_name %> = <%= class_name %>.all
|
21
22
|
latest = @<%= plural_table_name %>.max_by(&:updated_at)
|
22
|
-
last_updated = latest && latest.updated_at
|
23
|
+
last_updated = latest && latest.updated_at
|
23
24
|
if stale?(etag: "<%= class_name %>:#{@<%= plural_table_name %>.length}:#{last_updated}")
|
24
25
|
api_render @<%= plural_table_name %>
|
25
26
|
end
|
@@ -50,10 +51,6 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
50
51
|
|
51
52
|
# PUT <%= route_url %>/a-b-c-d-e
|
52
53
|
def update
|
53
|
-
if missing_attributes?
|
54
|
-
render_api_error 422, "Missing resource attributes"
|
55
|
-
return
|
56
|
-
end
|
57
54
|
@<%= singular_table_name %>.name = params[:name] if params[:name]
|
58
55
|
@<%= singular_table_name %>.description = params[:description] if params[:description]
|
59
56
|
set_updater(@<%= singular_table_name %>)
|
@@ -67,10 +64,10 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
67
64
|
@<%= singular_table_name %>.destroy
|
68
65
|
render_head_204
|
69
66
|
end
|
70
|
-
|
71
|
-
|
67
|
+
|
68
|
+
|
72
69
|
private
|
73
|
-
|
70
|
+
|
74
71
|
def find_<%= singular_table_name %>
|
75
72
|
ActionController::Parameters.permit_all_parameters = true
|
76
73
|
@<%= singular_table_name %> = <%= class_name %>.find_by_key(params['id'], consistent: true)
|
@@ -78,6 +75,6 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
78
75
|
render_api_error 404, "<%= class_name %> not found"
|
79
76
|
false
|
80
77
|
end
|
81
|
-
|
78
|
+
|
82
79
|
end
|
83
80
|
<% end -%>
|
@@ -229,6 +229,15 @@ module OceanApplicationController
|
|
229
229
|
end
|
230
230
|
|
231
231
|
|
232
|
+
def verify_attributes
|
233
|
+
missing = missing_attributes?
|
234
|
+
if missing
|
235
|
+
render_api_error 422, "Missing resource attributes: #{missing.join(', ')}"
|
236
|
+
return false
|
237
|
+
end
|
238
|
+
return true
|
239
|
+
end
|
240
|
+
|
232
241
|
#
|
233
242
|
# Cache values for collections. Accepts a class, a scope, or an array.
|
234
243
|
# The cache value is based on three components:
|
@@ -14,12 +14,12 @@ module Ocean
|
|
14
14
|
# to be written in a very terse, clear and understandable manner.
|
15
15
|
#
|
16
16
|
module OceanResourceController
|
17
|
-
|
17
|
+
|
18
18
|
extend ActiveSupport::Concern
|
19
19
|
|
20
20
|
included do
|
21
21
|
if defined? ActiveRecord
|
22
|
-
rescue_from ActiveRecord::RecordNotUnique,
|
22
|
+
rescue_from ActiveRecord::RecordNotUnique,
|
23
23
|
ActiveRecord::StatementInvalid do |x|
|
24
24
|
render_api_error 422, "Resource not unique"
|
25
25
|
end
|
@@ -56,7 +56,7 @@ module Ocean
|
|
56
56
|
# three optional keyword parameters:
|
57
57
|
#
|
58
58
|
# +required_attributes+: a list of keywords naming model attributes which must be
|
59
|
-
# present. If an API consumer submits data where any of these attributes isn't present,
|
59
|
+
# present. If an API consumer submits data where any of these attributes isn't present,
|
60
60
|
# an API error will be generated.
|
61
61
|
#
|
62
62
|
# ocean_resource_controller required_attributes: [:lock_version, :title]
|
@@ -73,7 +73,7 @@ module Ocean
|
|
73
73
|
# ocean_resource_controller no_validation_errors_on: [:password_hash, :password_salt]
|
74
74
|
#
|
75
75
|
# +extra_actions+: a hash containing information about extra controller actions
|
76
|
-
# apart from the standard Rails ones of +index+, +show+, +create+, +update+, and
|
76
|
+
# apart from the standard Rails ones of +index+, +show+, +create+, +update+, and
|
77
77
|
# +destroy+. One entry per extra action is required in order to process authorisation
|
78
78
|
# requests. Here's an example:
|
79
79
|
#
|
@@ -113,13 +113,16 @@ module Ocean
|
|
113
113
|
|
114
114
|
|
115
115
|
#
|
116
|
-
#
|
117
|
-
# +ocean_resource_controller
|
116
|
+
# If the params hash lacks any required attributes declared using
|
117
|
+
# +ocean_resource_controller+, returns a list of those attributes.
|
118
|
+
# Otherwise return false.
|
118
119
|
#
|
119
120
|
def missing_attributes?
|
121
|
+
missing = []
|
120
122
|
self.class.ocean_resource_controller_required_attributes.each do |attr|
|
121
|
-
|
123
|
+
missing << attr unless params[attr]
|
122
124
|
end
|
125
|
+
return missing unless missing.blank?
|
123
126
|
return false
|
124
127
|
end
|
125
128
|
|
data/lib/ocean/version.rb
CHANGED
@@ -8,8 +8,9 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
8
8
|
ocean_resource_controller
|
9
9
|
|
10
10
|
before_action :find_<%= singular_table_name %>, :only => [:show, :update, :destroy]
|
11
|
-
|
12
|
-
|
11
|
+
before_action :verify_attributes, only: :update
|
12
|
+
|
13
|
+
|
13
14
|
# GET <%= route_url %>
|
14
15
|
def index
|
15
16
|
expires_in 0, 's-maxage' => DEFAULT_CACHE_TIME
|
@@ -39,10 +40,6 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
39
40
|
|
40
41
|
# PUT <%= route_url %>/1
|
41
42
|
def update
|
42
|
-
if missing_attributes?
|
43
|
-
render_api_error 422, "Missing resource attributes"
|
44
|
-
return
|
45
|
-
end
|
46
43
|
@<%= singular_table_name %>.assign_attributes(filtered_params <%= class_name %>)
|
47
44
|
set_updater(@<%= singular_table_name %>)
|
48
45
|
@<%= singular_table_name %>.save!
|
@@ -55,10 +52,10 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
55
52
|
@<%= orm_instance.destroy %>
|
56
53
|
render_head_204
|
57
54
|
end
|
58
|
-
|
59
|
-
|
55
|
+
|
56
|
+
|
60
57
|
private
|
61
|
-
|
58
|
+
|
62
59
|
def find_<%= singular_table_name %>
|
63
60
|
@<%= singular_table_name %> = <%= class_name %>.find_by_id params[:id]
|
64
61
|
# If your table has app and context columns and you have created Rights utilising them,
|
@@ -68,6 +65,6 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
68
65
|
render_api_error 404, "<%= class_name %> not found"
|
69
66
|
false
|
70
67
|
end
|
71
|
-
|
68
|
+
|
72
69
|
end
|
73
70
|
<% end -%>
|