crudable-rails 1.4.0 → 1.5.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/README.md +161 -1
- data/lib/crudable/rails/base.rb +9 -1
- data/lib/crudable/rails/engine.rb +4 -0
- data/lib/crudable/rails/generators/scaffold_controller_extension.rb +55 -0
- data/lib/crudable/rails/generators/scaffold_extension.rb +29 -0
- data/lib/crudable/rails/generators/turbo_forms_generator_option.rb +16 -0
- data/lib/crudable/rails/generators/turbo_forms_scaffold_extension.rb +44 -0
- data/lib/crudable/rails/installer.rb +35 -0
- data/lib/crudable/rails/version.rb +1 -1
- data/lib/crudable-rails.rb +5 -0
- data/lib/generators/crudable/controller/USAGE +23 -0
- data/lib/generators/crudable/controller/controller_generator.rb +19 -0
- data/lib/generators/crudable/controller_helpers.rb +58 -0
- data/lib/generators/crudable/install/USAGE +8 -0
- data/lib/generators/crudable/install/install_generator.rb +19 -0
- data/lib/generators/crudable/scaffold/USAGE +14 -7
- data/lib/generators/crudable/scaffold/scaffold_generator.rb +8 -24
- data/lib/generators/crudable/templates/crudable_controller.rb.tt +257 -0
- data/lib/generators/crudable/templates/turbo_forms/_form.html.erb.tt +37 -0
- data/lib/generators/crudable/templates/turbo_forms/edit.html.erb.tt +14 -0
- data/lib/generators/crudable/templates/turbo_forms/edit.turbo_stream.erb.tt +3 -0
- data/lib/generators/crudable/templates/turbo_forms/new.html.erb.tt +13 -0
- data/lib/generators/crudable/templates/turbo_forms/new.turbo_stream.erb.tt +3 -0
- data/lib/tasks/crudable_tasks.rake +12 -4
- metadata +21 -9
- data/lib/generators/crudable/scaffold/templates/crudable_controller.rb.tt +0 -164
- data/lib/generators/scaffold_controller/USAGE +0 -13
- data/lib/generators/scaffold_controller/scaffold_controller_generator.rb +0 -36
- data/lib/generators/scaffold_controller/templates/crudable_controller.rb.tt +0 -164
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
<% module_namespacing do -%>
|
|
2
|
+
class <%= controller_class_name %>Controller < ApplicationController
|
|
3
|
+
crudable
|
|
4
|
+
|
|
5
|
+
private
|
|
6
|
+
|
|
7
|
+
# ---------------------------------------------------------------------------
|
|
8
|
+
# Optional overrides (self-documenting hooks)
|
|
9
|
+
#
|
|
10
|
+
# `crudable` mixes in `Crudable::Rails::Base` (which includes `Resourceable`
|
|
11
|
+
# and `Nestable`), which provides the REST actions and calls these hooks.
|
|
12
|
+
# Uncomment and tailor any of the following as needed.
|
|
13
|
+
# ---------------------------------------------------------------------------
|
|
14
|
+
#
|
|
15
|
+
# # Override the model class when it cannot be inferred from the controller name.
|
|
16
|
+
# def resource_class
|
|
17
|
+
# <%= class_name %>
|
|
18
|
+
# end
|
|
19
|
+
#
|
|
20
|
+
# # Customize the record passed to Pundit (e.g. namespaced authorization).
|
|
21
|
+
# def authorizable_resource
|
|
22
|
+
# @<%= singular_table_name %>
|
|
23
|
+
# end
|
|
24
|
+
#
|
|
25
|
+
# # If this resource is singular (e.g. SettingsController), return true.
|
|
26
|
+
# def singleton?
|
|
27
|
+
# false
|
|
28
|
+
# end
|
|
29
|
+
#
|
|
30
|
+
# # Which param key to use when finding a member resource.
|
|
31
|
+
# def finder_param
|
|
32
|
+
# :id
|
|
33
|
+
# end
|
|
34
|
+
#
|
|
35
|
+
# # Enable FriendlyId finders/redirects.
|
|
36
|
+
# def friendly_finders?
|
|
37
|
+
# return false unless defined?(FriendlyId) && resource_class.respond_to?(:friendly)
|
|
38
|
+
#
|
|
39
|
+
# true
|
|
40
|
+
# end
|
|
41
|
+
#
|
|
42
|
+
# # Nested parent lookup: use FriendlyId for the parent when available.
|
|
43
|
+
# def parent_friendly_finders?
|
|
44
|
+
# return false unless defined?(FriendlyId)
|
|
45
|
+
# return false unless parent_class.respond_to?(:friendly)
|
|
46
|
+
#
|
|
47
|
+
# friendly_finders?
|
|
48
|
+
# end
|
|
49
|
+
#
|
|
50
|
+
# # Enable pagination (requires Kaminari).
|
|
51
|
+
# def paginate_resource?
|
|
52
|
+
# return false unless defined?(Kaminari)
|
|
53
|
+
#
|
|
54
|
+
# true
|
|
55
|
+
# end
|
|
56
|
+
#
|
|
57
|
+
# # Customize the base scope for index (before HasScope and Pundit).
|
|
58
|
+
# def find_scope
|
|
59
|
+
# if parent_resource_association.present?
|
|
60
|
+
# instance_variable_get("@#{parent_var_name}").send(parent_resource_association)
|
|
61
|
+
# else
|
|
62
|
+
# <%= class_name %>
|
|
63
|
+
# end
|
|
64
|
+
# end
|
|
65
|
+
#
|
|
66
|
+
# # Customize the scope used for index. Primary hook for filtering.
|
|
67
|
+
# # (HasScope is applied automatically if present.)
|
|
68
|
+
# def authorizable_scope
|
|
69
|
+
# scope = find_scope
|
|
70
|
+
# return scope.all unless authorize_with_pundit?
|
|
71
|
+
# return scope.all unless pundit_authorization_available?
|
|
72
|
+
#
|
|
73
|
+
# policy_scope(resource_namespace + [scope])
|
|
74
|
+
# end
|
|
75
|
+
#
|
|
76
|
+
# # Full scope used for index (after HasScope, Pundit, etc).
|
|
77
|
+
# def resource_scope
|
|
78
|
+
# return apply_scopes(authorizable_scope) if defined?(HasScope)
|
|
79
|
+
#
|
|
80
|
+
# authorizable_scope
|
|
81
|
+
# end
|
|
82
|
+
#
|
|
83
|
+
# # Skip initializing a new instance in create (useful for custom create flows).
|
|
84
|
+
# def skip_initialize_create?
|
|
85
|
+
# false
|
|
86
|
+
# end
|
|
87
|
+
#
|
|
88
|
+
# # Discard/soft-delete support (requires Discard). When true, destroy will
|
|
89
|
+
# # choose discard vs destroy based on record state and params.
|
|
90
|
+
# def discard?
|
|
91
|
+
# false
|
|
92
|
+
# end
|
|
93
|
+
#
|
|
94
|
+
# # Control whether Pundit authorization should be performed.
|
|
95
|
+
# def authorize_with_pundit?
|
|
96
|
+
# defined?(Pundit)
|
|
97
|
+
# end
|
|
98
|
+
#
|
|
99
|
+
# # Authorization lifecycle hooks (called inside create/update).
|
|
100
|
+
# def before_authorize_create; end
|
|
101
|
+
# def after_authorize_create; end
|
|
102
|
+
# def before_authorize_update; end
|
|
103
|
+
# def after_authorize_update; end
|
|
104
|
+
#
|
|
105
|
+
# # Post-success hooks (side-effects, instrumentation, etc).
|
|
106
|
+
# def on_successful_create
|
|
107
|
+
# flash[:success] = after_create_notice
|
|
108
|
+
# end
|
|
109
|
+
#
|
|
110
|
+
# def on_successful_update
|
|
111
|
+
# flash[:success] = after_update_notice
|
|
112
|
+
# end
|
|
113
|
+
#
|
|
114
|
+
# def on_successful_destroy
|
|
115
|
+
# flash[:success] = after_destroy_notice
|
|
116
|
+
# end
|
|
117
|
+
#
|
|
118
|
+
# # Failure setup hooks (e.g., rebuild form collections).
|
|
119
|
+
# def on_failed_create_setup; end
|
|
120
|
+
# def on_failed_update_setup; end
|
|
121
|
+
#
|
|
122
|
+
# def on_failed_destroy
|
|
123
|
+
# flash[:alert] = after_failed_destroy_alert
|
|
124
|
+
# end
|
|
125
|
+
#
|
|
126
|
+
# # Customize success redirects/flash messages.
|
|
127
|
+
# def after_create_redirect_path
|
|
128
|
+
# if respond_to?(:parent_present?) && parent_present? && instance_variable_get("@#{parent_var_name}")
|
|
129
|
+
# parent = instance_variable_get("@#{parent_var_name}")
|
|
130
|
+
# resource_namespace + [parent, :<%= plural_table_name %>]
|
|
131
|
+
# else
|
|
132
|
+
# resource_namespace + [:<%= plural_table_name %>]
|
|
133
|
+
# end
|
|
134
|
+
# rescue NoMethodError
|
|
135
|
+
# resource_namespace + [:<%= plural_table_name %>]
|
|
136
|
+
# end
|
|
137
|
+
#
|
|
138
|
+
# def after_create_notice
|
|
139
|
+
# t('crudable.created', model_name: <%= class_name %>.model_name.human)
|
|
140
|
+
# end
|
|
141
|
+
#
|
|
142
|
+
# def after_update_redirect_path
|
|
143
|
+
# resource_namespace + [@<%= singular_table_name %>]
|
|
144
|
+
# end
|
|
145
|
+
#
|
|
146
|
+
# def after_update_notice
|
|
147
|
+
# t('crudable.updated', model_name: <%= class_name %>.model_name.human)
|
|
148
|
+
# end
|
|
149
|
+
#
|
|
150
|
+
# def after_destroy_redirect_path
|
|
151
|
+
# resource_namespace + [:<%= plural_table_name %>]
|
|
152
|
+
# end
|
|
153
|
+
#
|
|
154
|
+
# def after_failed_destroy_redirect_path
|
|
155
|
+
# after_destroy_redirect_path
|
|
156
|
+
# end
|
|
157
|
+
#
|
|
158
|
+
# def after_destroy_notice
|
|
159
|
+
# t('crudable.destroyed', model_name: <%= class_name %>.model_name.human)
|
|
160
|
+
# end
|
|
161
|
+
#
|
|
162
|
+
# def after_failed_destroy_alert
|
|
163
|
+
# t('crudable.not_destroyed', model_name: <%= class_name %>.model_name.human)
|
|
164
|
+
# end
|
|
165
|
+
#
|
|
166
|
+
# # Customize rendering for success/failure outcomes (Turbo/HTML).
|
|
167
|
+
# def on_successful_create_render
|
|
168
|
+
# redirect_to after_create_redirect_path
|
|
169
|
+
# end
|
|
170
|
+
#
|
|
171
|
+
# def on_failed_create_render
|
|
172
|
+
# resource = @<%= singular_table_name %>
|
|
173
|
+
# @<%= singular_table_name %> = new_instance if resource.nil?
|
|
174
|
+
# respond_to do |format|
|
|
175
|
+
# format.turbo_stream { render_action(:new) }
|
|
176
|
+
# format.html { render :new, status: :unprocessable_content }
|
|
177
|
+
# end
|
|
178
|
+
# end
|
|
179
|
+
#
|
|
180
|
+
# def on_successful_update_render
|
|
181
|
+
# redirect_to after_update_redirect_path
|
|
182
|
+
# end
|
|
183
|
+
#
|
|
184
|
+
# def on_failed_update_render
|
|
185
|
+
# respond_to do |format|
|
|
186
|
+
# format.turbo_stream { render_action(:edit) }
|
|
187
|
+
# format.html { render :edit, status: :unprocessable_content }
|
|
188
|
+
# end
|
|
189
|
+
# end
|
|
190
|
+
#
|
|
191
|
+
# def on_successful_destroy_render
|
|
192
|
+
# redirect_to after_destroy_redirect_path
|
|
193
|
+
# end
|
|
194
|
+
#
|
|
195
|
+
# def on_failed_destroy_render
|
|
196
|
+
# redirect_to after_failed_destroy_redirect_path
|
|
197
|
+
# end
|
|
198
|
+
#
|
|
199
|
+
# # Customize pagination when enabled.
|
|
200
|
+
# def paginate_resource
|
|
201
|
+
# return unless paginate_resource?
|
|
202
|
+
#
|
|
203
|
+
# @<%= plural_table_name %> = @<%= plural_table_name %>.page(params[:page]).per(params[:per])
|
|
204
|
+
# end
|
|
205
|
+
#
|
|
206
|
+
# # Customize 404 responses.
|
|
207
|
+
# def render_not_found
|
|
208
|
+
# head :not_found
|
|
209
|
+
# end
|
|
210
|
+
#
|
|
211
|
+
# # Nested resources: parent scoping is automatic when a `*_id` param is present.
|
|
212
|
+
# def use_parent_as_scope?
|
|
213
|
+
# true
|
|
214
|
+
# end
|
|
215
|
+
#
|
|
216
|
+
# # Nested resources: customize which `*_id` param is treated as the parent id.
|
|
217
|
+
# def parent_id_param
|
|
218
|
+
# return nil unless use_parent_as_scope?
|
|
219
|
+
#
|
|
220
|
+
# @parent_id_param ||= begin
|
|
221
|
+
# keys = parent_id_param_keys
|
|
222
|
+
# keys.map(&:to_s).grep(/(.+)_id$/).last
|
|
223
|
+
# end
|
|
224
|
+
# end
|
|
225
|
+
#
|
|
226
|
+
# # Override when create and update need different permitted attributes.
|
|
227
|
+
# def create_params
|
|
228
|
+
# resource_params
|
|
229
|
+
# end
|
|
230
|
+
#
|
|
231
|
+
# def update_params
|
|
232
|
+
# resource_params
|
|
233
|
+
# end
|
|
234
|
+
#
|
|
235
|
+
# # Strong params API used by `resource_params`.
|
|
236
|
+
# def param_method
|
|
237
|
+
# @param_method ||= ActionController::Parameters.method_defined?(:expect) ? :expect : :require
|
|
238
|
+
# end
|
|
239
|
+
#
|
|
240
|
+
# # def resource_params
|
|
241
|
+
# # if param_method == :expect
|
|
242
|
+
# # params.expect(<%= singular_table_name %>: permitted_params)
|
|
243
|
+
# # else
|
|
244
|
+
# # params.require(:<%= singular_table_name %>).permit(*permitted_params)
|
|
245
|
+
# # end
|
|
246
|
+
# # end
|
|
247
|
+
#
|
|
248
|
+
# Required: return the attributes permitted for create/update.
|
|
249
|
+
def permitted_params
|
|
250
|
+
<%- if attributes_names.empty? -%>
|
|
251
|
+
[]
|
|
252
|
+
<%- else -%>
|
|
253
|
+
[<%= permitted_params_list %>]
|
|
254
|
+
<%- end -%>
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
<% end -%>
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
<%%= form_with model: <%= form_with_model %> do |f| %>
|
|
2
|
+
<%% if f.object.errors.any? %>
|
|
3
|
+
<div style="color: red">
|
|
4
|
+
<h2><%%= pluralize(f.object.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2>
|
|
5
|
+
|
|
6
|
+
<ul>
|
|
7
|
+
<%% f.object.errors.each do |error| %>
|
|
8
|
+
<li><%%= error.full_message %></li>
|
|
9
|
+
<%% end %>
|
|
10
|
+
</ul>
|
|
11
|
+
</div>
|
|
12
|
+
<%% end %>
|
|
13
|
+
|
|
14
|
+
<% form_attributes.each do |attribute| -%>
|
|
15
|
+
<div>
|
|
16
|
+
<% if attribute.password_digest? -%>
|
|
17
|
+
<%%= f.label :password, style: "display: block" %>
|
|
18
|
+
<%%= f.password_field :password %>
|
|
19
|
+
</div>
|
|
20
|
+
|
|
21
|
+
<div>
|
|
22
|
+
<%%= f.label :password_confirmation, style: "display: block" %>
|
|
23
|
+
<%%= f.password_field :password_confirmation %>
|
|
24
|
+
<% elsif attribute.attachments? -%>
|
|
25
|
+
<%%= f.label :<%= attribute.column_name %>, style: "display: block" %>
|
|
26
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.column_name %>, multiple: true %>
|
|
27
|
+
<% else -%>
|
|
28
|
+
<%%= f.label :<%= attribute.column_name %>, style: "display: block" %>
|
|
29
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.column_name %> %>
|
|
30
|
+
<% end -%>
|
|
31
|
+
</div>
|
|
32
|
+
|
|
33
|
+
<% end -%>
|
|
34
|
+
<div>
|
|
35
|
+
<%%= f.submit %>
|
|
36
|
+
</div>
|
|
37
|
+
<%% end %>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<%% content_for :title, "Editing <%= human_name.downcase %>" %>
|
|
2
|
+
|
|
3
|
+
<h1>Editing <%= human_name.downcase %></h1>
|
|
4
|
+
|
|
5
|
+
<%%= tag.div id: dom_id(@<%= singular_table_name %>, :form) do %>
|
|
6
|
+
<%%= render "form", <%= singular_table_name %>: @<%= singular_table_name %> %>
|
|
7
|
+
<%% end %>
|
|
8
|
+
|
|
9
|
+
<br>
|
|
10
|
+
|
|
11
|
+
<div>
|
|
12
|
+
<%%= link_to "Show this <%= human_name.downcase %>", <%= model_resource_name(prefix: "@") %> %> |
|
|
13
|
+
<%%= link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper(type: :path) %> %>
|
|
14
|
+
</div>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<%% content_for :title, "New <%= human_name.downcase %>" %>
|
|
2
|
+
|
|
3
|
+
<h1>New <%= human_name.downcase %></h1>
|
|
4
|
+
|
|
5
|
+
<%%= tag.div id: dom_id(@<%= singular_table_name %>, :form) do %>
|
|
6
|
+
<%%= render "form", <%= singular_table_name %>: @<%= singular_table_name %> %>
|
|
7
|
+
<%% end %>
|
|
8
|
+
|
|
9
|
+
<br>
|
|
10
|
+
|
|
11
|
+
<div>
|
|
12
|
+
<%%= link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper(type: :path) %> %>
|
|
13
|
+
</div>
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
namespace :crudable do
|
|
4
|
+
desc 'Install crudable-rails configuration'
|
|
5
|
+
task install: :environment do
|
|
6
|
+
updated = Crudable::Rails.postinstall
|
|
7
|
+
|
|
8
|
+
if updated.any?
|
|
9
|
+
puts "Set raise_on_missing_callback_actions to true in: #{updated.join(', ')}"
|
|
10
|
+
else
|
|
11
|
+
puts 'No environment files with raise_on_missing_callback_actions = false were found.'
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: crudable-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tomislav Simnett
|
|
@@ -15,7 +15,7 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: 7.
|
|
18
|
+
version: 7.2.0
|
|
19
19
|
- - "<"
|
|
20
20
|
- !ruby/object:Gem::Version
|
|
21
21
|
version: 9.0.0
|
|
@@ -25,7 +25,7 @@ dependencies:
|
|
|
25
25
|
requirements:
|
|
26
26
|
- - ">="
|
|
27
27
|
- !ruby/object:Gem::Version
|
|
28
|
-
version: 7.
|
|
28
|
+
version: 7.2.0
|
|
29
29
|
- - "<"
|
|
30
30
|
- !ruby/object:Gem::Version
|
|
31
31
|
version: 9.0.0
|
|
@@ -102,15 +102,27 @@ files:
|
|
|
102
102
|
- lib/crudable/rails/base.rb
|
|
103
103
|
- lib/crudable/rails/controller.rb
|
|
104
104
|
- lib/crudable/rails/engine.rb
|
|
105
|
+
- lib/crudable/rails/generators/scaffold_controller_extension.rb
|
|
106
|
+
- lib/crudable/rails/generators/scaffold_extension.rb
|
|
107
|
+
- lib/crudable/rails/generators/turbo_forms_generator_option.rb
|
|
108
|
+
- lib/crudable/rails/generators/turbo_forms_scaffold_extension.rb
|
|
109
|
+
- lib/crudable/rails/installer.rb
|
|
105
110
|
- lib/crudable/rails/nestable.rb
|
|
106
111
|
- lib/crudable/rails/resourceable.rb
|
|
107
112
|
- lib/crudable/rails/version.rb
|
|
113
|
+
- lib/generators/crudable/controller/USAGE
|
|
114
|
+
- lib/generators/crudable/controller/controller_generator.rb
|
|
115
|
+
- lib/generators/crudable/controller_helpers.rb
|
|
116
|
+
- lib/generators/crudable/install/USAGE
|
|
117
|
+
- lib/generators/crudable/install/install_generator.rb
|
|
108
118
|
- lib/generators/crudable/scaffold/USAGE
|
|
109
119
|
- lib/generators/crudable/scaffold/scaffold_generator.rb
|
|
110
|
-
- lib/generators/crudable/
|
|
111
|
-
- lib/generators/
|
|
112
|
-
- lib/generators/
|
|
113
|
-
- lib/generators/
|
|
120
|
+
- lib/generators/crudable/templates/crudable_controller.rb.tt
|
|
121
|
+
- lib/generators/crudable/templates/turbo_forms/_form.html.erb.tt
|
|
122
|
+
- lib/generators/crudable/templates/turbo_forms/edit.html.erb.tt
|
|
123
|
+
- lib/generators/crudable/templates/turbo_forms/edit.turbo_stream.erb.tt
|
|
124
|
+
- lib/generators/crudable/templates/turbo_forms/new.html.erb.tt
|
|
125
|
+
- lib/generators/crudable/templates/turbo_forms/new.turbo_stream.erb.tt
|
|
114
126
|
- lib/tasks/crudable_tasks.rake
|
|
115
127
|
homepage: https://gitlab.com/initforthe/crudable-rails
|
|
116
128
|
licenses:
|
|
@@ -124,14 +136,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
124
136
|
requirements:
|
|
125
137
|
- - ">="
|
|
126
138
|
- !ruby/object:Gem::Version
|
|
127
|
-
version: 3.
|
|
139
|
+
version: 3.2.0
|
|
128
140
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
129
141
|
requirements:
|
|
130
142
|
- - ">="
|
|
131
143
|
- !ruby/object:Gem::Version
|
|
132
144
|
version: '0'
|
|
133
145
|
requirements: []
|
|
134
|
-
rubygems_version: 4.0.
|
|
146
|
+
rubygems_version: 4.0.10
|
|
135
147
|
specification_version: 4
|
|
136
148
|
summary: CRUD operations for Rails controllers
|
|
137
149
|
test_files: []
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
<% module_namespacing do -%>
|
|
2
|
-
class <%= controller_class_name %>Controller < ApplicationController
|
|
3
|
-
crudable
|
|
4
|
-
|
|
5
|
-
private
|
|
6
|
-
|
|
7
|
-
# ---------------------------------------------------------------------------
|
|
8
|
-
# Optional overrides (self-documenting hooks)
|
|
9
|
-
#
|
|
10
|
-
# `crudable` mixes in `Crudable::Rails::Base` (which includes `Nestable`),
|
|
11
|
-
# which provides the REST actions and calls these hooks. Uncomment and tailor
|
|
12
|
-
# any of the following as needed.
|
|
13
|
-
# ---------------------------------------------------------------------------
|
|
14
|
-
#
|
|
15
|
-
# # If this resource is singular (e.g. SettingsController), return true.
|
|
16
|
-
# def singleton?
|
|
17
|
-
# false
|
|
18
|
-
# end
|
|
19
|
-
#
|
|
20
|
-
# # Which param key to use when finding a member resource (default :id).
|
|
21
|
-
# # Common examples: :slug, :uuid
|
|
22
|
-
# def finder_param
|
|
23
|
-
# :id
|
|
24
|
-
# end
|
|
25
|
-
#
|
|
26
|
-
# # Enable FriendlyId finders/redirects (default false).
|
|
27
|
-
# def friendly_finders?
|
|
28
|
-
# false
|
|
29
|
-
# end
|
|
30
|
-
#
|
|
31
|
-
# # Nested parent lookup: use FriendlyId for the parent (defaults to friendly_finders?).
|
|
32
|
-
# # This is only applied when FriendlyId is available and the parent model responds to `.friendly`.
|
|
33
|
-
# def parent_friendly_finders?
|
|
34
|
-
# friendly_finders?
|
|
35
|
-
# end
|
|
36
|
-
#
|
|
37
|
-
# # Enable pagination (requires Kaminari) (default true when Kaminari is defined).
|
|
38
|
-
# def paginate_resource?
|
|
39
|
-
# super
|
|
40
|
-
# end
|
|
41
|
-
#
|
|
42
|
-
# # Customize the scope used for index. This is the primary hook for filtering.
|
|
43
|
-
# # (HasScope is applied automatically if present.)
|
|
44
|
-
# def authorizable_scope
|
|
45
|
-
# super
|
|
46
|
-
# end
|
|
47
|
-
#
|
|
48
|
-
# # Full scope used for index (after HasScope, Pundit, etc).
|
|
49
|
-
# def resource_scope
|
|
50
|
-
# super
|
|
51
|
-
# end
|
|
52
|
-
#
|
|
53
|
-
# # Skip initializing a new instance in create (useful for custom create flows).
|
|
54
|
-
# def skip_initialize_create?
|
|
55
|
-
# false
|
|
56
|
-
# end
|
|
57
|
-
#
|
|
58
|
-
# # Discard/soft-delete support (requires Discard). When true, destroy will
|
|
59
|
-
# # choose discard vs destroy based on record state and params.
|
|
60
|
-
# def discard?
|
|
61
|
-
# false
|
|
62
|
-
# end
|
|
63
|
-
#
|
|
64
|
-
# # Control whether Pundit authorization should be performed.
|
|
65
|
-
# # Override this to customize authorization behavior (e.g., feature flags, user roles).
|
|
66
|
-
# # Default: returns true when Pundit is defined.
|
|
67
|
-
# def authorize_with_pundit?
|
|
68
|
-
# super
|
|
69
|
-
# end
|
|
70
|
-
#
|
|
71
|
-
# # Authorization lifecycle hooks (called inside create/update).
|
|
72
|
-
# def before_authorize_create; end
|
|
73
|
-
# def after_authorize_create; end
|
|
74
|
-
# def before_authorize_update; end
|
|
75
|
-
# def after_authorize_update; end
|
|
76
|
-
#
|
|
77
|
-
# # Post-success hooks (side-effects, instrumentation, etc).
|
|
78
|
-
# def on_successful_create; end
|
|
79
|
-
# def on_successful_update; end
|
|
80
|
-
#
|
|
81
|
-
# # Failure setup hooks (e.g., rebuild form collections).
|
|
82
|
-
# def on_failed_create_setup; end
|
|
83
|
-
# def on_failed_update_setup; end
|
|
84
|
-
#
|
|
85
|
-
# # Customize success redirects/flash messages.
|
|
86
|
-
# def after_create_redirect_path
|
|
87
|
-
# super
|
|
88
|
-
# end
|
|
89
|
-
#
|
|
90
|
-
# def after_create_notice
|
|
91
|
-
# super
|
|
92
|
-
# end
|
|
93
|
-
#
|
|
94
|
-
# def after_update_redirect_path
|
|
95
|
-
# super
|
|
96
|
-
# end
|
|
97
|
-
#
|
|
98
|
-
# def after_update_notice
|
|
99
|
-
# super
|
|
100
|
-
# end
|
|
101
|
-
#
|
|
102
|
-
# def after_destroy_redirect_path
|
|
103
|
-
# super
|
|
104
|
-
# end
|
|
105
|
-
#
|
|
106
|
-
# def after_failed_destroy_redirect_path
|
|
107
|
-
# super
|
|
108
|
-
# end
|
|
109
|
-
#
|
|
110
|
-
# def after_destroy_notice
|
|
111
|
-
# super
|
|
112
|
-
# end
|
|
113
|
-
#
|
|
114
|
-
# def after_failed_destroy_alert
|
|
115
|
-
# super
|
|
116
|
-
# end
|
|
117
|
-
#
|
|
118
|
-
# # Customize rendering for failed creates/updates (Turbo/HTML).
|
|
119
|
-
# def on_failed_create_render
|
|
120
|
-
# super
|
|
121
|
-
# end
|
|
122
|
-
#
|
|
123
|
-
# def on_failed_update_render
|
|
124
|
-
# super
|
|
125
|
-
# end
|
|
126
|
-
#
|
|
127
|
-
# # Customize rendering for destroy outcomes.
|
|
128
|
-
# def on_successful_destroy_render
|
|
129
|
-
# super
|
|
130
|
-
# end
|
|
131
|
-
#
|
|
132
|
-
# def on_failed_destroy_render
|
|
133
|
-
# super
|
|
134
|
-
# end
|
|
135
|
-
#
|
|
136
|
-
# # Nested resources: parent scoping is automatic when a `*_id` param is present.
|
|
137
|
-
# # Override `use_parent_as_scope?` to disable parent scoping.
|
|
138
|
-
# def find_parent
|
|
139
|
-
# super
|
|
140
|
-
# end
|
|
141
|
-
#
|
|
142
|
-
# def use_parent_as_scope?
|
|
143
|
-
# true
|
|
144
|
-
# end
|
|
145
|
-
#
|
|
146
|
-
# # Nested resources: customize which `*_id` param is treated as the parent id.
|
|
147
|
-
# # Useful for multi-level nesting or non-standard param names.
|
|
148
|
-
# def parent_id_param
|
|
149
|
-
# super
|
|
150
|
-
# end
|
|
151
|
-
#
|
|
152
|
-
# Strong params used by `create` (and by default `update` via `update_params`).
|
|
153
|
-
# Adjust the permitted attributes to match your model and nested params shape.
|
|
154
|
-
def create_params
|
|
155
|
-
<%- if attributes_names.empty? -%>
|
|
156
|
-
params.fetch(:<%= singular_table_name %>, {})
|
|
157
|
-
<%- else -%>
|
|
158
|
-
params.expect(<%= singular_table_name %>: [ <%= permitted_params %> ])
|
|
159
|
-
<%- end -%>
|
|
160
|
-
end
|
|
161
|
-
# By default, updates permit the same attributes as creates.
|
|
162
|
-
alias update_params create_params
|
|
163
|
-
end
|
|
164
|
-
<% end -%>
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
Description:
|
|
2
|
-
Extends the Rails scaffold controller generator with an option to generate a Crudable controller.
|
|
3
|
-
|
|
4
|
-
Example:
|
|
5
|
-
bin/rails generate scaffold Thing
|
|
6
|
-
|
|
7
|
-
This will create:
|
|
8
|
-
It will invoke the rails scaffold controller with an optional Crudable controller (use `--crudable`).
|
|
9
|
-
|
|
10
|
-
Notes:
|
|
11
|
-
Nested routing is automatically supported: if a parent `*_id` param is present and an association
|
|
12
|
-
exists between the parent and resource, collections and resources will be scoped through the parent.
|
|
13
|
-
Override `use_parent_as_scope?` in your controller to disable parent scoping.
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class ScaffoldControllerGenerator < Rails::Generators::NamedBase
|
|
4
|
-
include Rails::Generators::ResourceHelpers
|
|
5
|
-
|
|
6
|
-
source_root File.expand_path('templates', __dir__)
|
|
7
|
-
|
|
8
|
-
class_exclusive do
|
|
9
|
-
class_option :crudable, type: :boolean, desc: 'Generate with crudable controller'
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
argument :attributes, type: :array, default: [], banner: 'field:type field:type'
|
|
13
|
-
|
|
14
|
-
hook_for :scaffold_controller, in: :rails
|
|
15
|
-
|
|
16
|
-
def crudable
|
|
17
|
-
return unless options.crudable?
|
|
18
|
-
|
|
19
|
-
template 'crudable_controller.rb',
|
|
20
|
-
File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb"), force: true
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
private
|
|
24
|
-
|
|
25
|
-
def permitted_params
|
|
26
|
-
attachments, others = attributes_names.partition { |name| attachments?(name) }
|
|
27
|
-
params = others.map { |name| ":#{name}" }
|
|
28
|
-
params += attachments.map { |name| "#{name}: []" }
|
|
29
|
-
params.join(', ')
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def attachments?(name)
|
|
33
|
-
attribute = attributes.find { |attr| attr.name == name }
|
|
34
|
-
attribute&.attachments?
|
|
35
|
-
end
|
|
36
|
-
end
|