-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcontroller.rb.erb
More file actions
98 lines (89 loc) · 2.86 KB
/
controller.rb.erb
File metadata and controls
98 lines (89 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<% module_namespacing do -%>
class <%= model_klass.name.pluralize %>Controller < ApplicationController
<%- unless omit_comments? -%>
# Mark this as a JSONAPI controller, associating with the given resource
<%- end -%>
jsonapi resource: <%= model_klass %>Resource
<%- if actions?('create', 'update') -%>
<%- unless omit_comments? -%>
# Reference a strong resource payload defined in
# config/initializers/strong_resources.rb
<%- end -%>
strong_resource :<%= singular_table_name %>
<%- end -%>
<%- if actions?('create', 'update') -%>
<%- unless omit_comments? -%>
# Run strong parameter validation for these actions.
# Invalid keys will be dropped.
# Invalid value types will log or raise based on the configuration
# ActionController::Parameters.action_on_invalid_parameters
<%- end -%>
before_action :apply_strong_params, only: [:create, :update]
<%- end -%>
<%- if actions?('index') -%>
<%- unless omit_comments? -%>
# Start with a base scope and pass to render_jsonapi
<%- end -%>
def index
<%= file_name.pluralize %> = <%= model_klass %>.all
render_jsonapi(<%= file_name.pluralize %>)
end
<%- end -%>
<%- if actions?('show') -%>
<%- unless omit_comments? -%>
# Call jsonapi_scope directly here so we can get behavior like
# sparse fieldsets and statistics.
<%- end -%>
def show
scope = jsonapi_scope(<%= model_klass %>.where(id: params[:id]))
instance = scope.resolve.first
raise JsonapiCompliable::Errors::RecordNotFound unless instance
render_jsonapi(instance, scope: false)
end
<%- end -%>
<%- if actions?('create') -%>
<%- unless omit_comments? -%>
# jsonapi_create will use the configured Resource (and adapter) to persist.
# This will handle nested relationships as well.
# On validation errors, render correct error JSON.
<%- end -%>
def create
<%= file_name %>, success = jsonapi_create.to_a
if success
render_jsonapi(<%= file_name %>, scope: false)
else
render_errors_for(<%= file_name %>)
end
end
<%- end -%>
<%- if actions?('update') -%>
<%- unless omit_comments? -%>
# jsonapi_update will use the configured Resource (and adapter) to persist.
# This will handle nested relationships as well.
# On validation errors, render correct error JSON.
<%- end -%>
def update
<%= file_name %>, success = jsonapi_update.to_a
if success
render_jsonapi(<%= file_name %>, scope: false)
else
render_errors_for(<%= file_name %>)
end
end
<%- end -%>
<%- if actions?('destroy') -%>
<%- unless omit_comments? -%>
# Renders 200 OK with empty meta
# http://jsonapi.org/format/#crud-deleting-responses-200
<%- end -%>
def destroy
<%= file_name %>, success = jsonapi_destroy.to_a
if success
render json: { meta: {} }
else
render_errors_for(<%= file_name %>)
end
end
<%- end -%>
end
<% end -%>