-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
application_controller.rb
291 lines (247 loc) · 8.22 KB
/
application_controller.rb
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
module Administrate
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def index
authorize_resource(resource_class)
search_term = params[:search].to_s.strip
resources = filter_resources(scoped_resource, search_term: search_term)
resources = apply_collection_includes(resources)
resources = order.apply(resources)
resources = paginate_resources(resources)
@resources = resources
page = Administrate::Page::Collection.new(dashboard, order: order)
render locals: {
resources: resources,
search_term: search_term,
page: page,
show_search_bar: show_search_bar?,
}
end
def show
render locals: {
page: Administrate::Page::Show.new(dashboard, requested_resource),
}
end
def new
@new_resource = resource = new_resource
authorize_resource(resource)
render locals: {
page: Administrate::Page::Form.new(dashboard, resource),
}
end
def edit
render locals: {
page: Administrate::Page::Form.new(dashboard, requested_resource),
}
end
def create
@new_resource = resource = new_resource(resource_params)
authorize_resource(resource)
if resource.save
redirect_to(
after_resource_created_path(resource),
notice: translate_with_resource("create.success"),
)
else
render :new, locals: {
page: Administrate::Page::Form.new(dashboard, resource),
}, status: :unprocessable_entity
end
end
def update
if requested_resource.update(resource_params)
redirect_to(
after_resource_updated_path(requested_resource),
notice: translate_with_resource("update.success"),
)
else
render :edit, locals: {
page: Administrate::Page::Form.new(dashboard, requested_resource),
}, status: :unprocessable_entity
end
end
def destroy
if requested_resource.destroy
flash[:notice] = translate_with_resource("destroy.success")
else
flash[:error] = requested_resource.errors.full_messages.join("<br/>")
end
redirect_to after_resource_destroyed_path(requested_resource)
end
private
def filter_resources(resources, search_term:)
Administrate::Search.new(
resources,
dashboard,
search_term,
).run
end
def after_resource_destroyed_path(_requested_resource)
{ action: :index }
end
def after_resource_created_path(requested_resource)
[namespace, requested_resource]
end
def after_resource_updated_path(requested_resource)
[namespace, requested_resource]
end
helper_method :nav_link_state
def nav_link_state(resource)
underscore_resource = resource.to_s.split("/").join("__")
resource_name.to_s.pluralize == underscore_resource ? :active : :inactive
end
# Whether the named action route exists for the resource class.
#
# @param resource [Class, String, Symbol] A class of resources, or the name
# of a class of resources.
# @param action_name [String, Symbol] The name of an action that might be
# possible to perform on a resource or resource class.
# @return [Boolean] `true` if a route exists for the resource class and the
# action. `false` otherwise.
def existing_action?(resource, action_name)
routes.include?([resource.to_s.underscore.pluralize, action_name.to_s])
end
helper_method :existing_action?
# @deprecated Use {#existing_action} instead. Note that, in
# {#existing_action}, the order of parameters is reversed and
# there is no default value for the `resource` parameter.
def valid_action?(action_name, resource = resource_class)
Administrate.warn_of_deprecated_authorization_method(__method__)
existing_action?(resource, action_name)
end
helper_method :valid_action?
def routes
@routes ||= Namespace.new(namespace).routes.to_set
end
def records_per_page
params[:per_page] || 20
end
def order
@order ||= Administrate::Order.new(
sorting_attribute,
sorting_direction,
association_attribute: order_by_field(
dashboard_attribute(sorting_attribute),
),
)
end
def order_by_field(dashboard)
return unless dashboard.try(:options)
dashboard.options.fetch(:order, nil)
end
def dashboard_attribute(attribute)
dashboard.attribute_types[attribute.to_sym] if attribute
end
def sorting_attribute
sorting_params.fetch(:order) { default_sorting_attribute }
end
def default_sorting_attribute
nil
end
def sorting_direction
sorting_params.fetch(:direction) { default_sorting_direction }
end
def default_sorting_direction
nil
end
def sorting_params
Hash.try_convert(request.query_parameters[resource_name]) || {}
end
def dashboard
@dashboard ||= dashboard_class.new
end
def requested_resource
@requested_resource ||= find_resource(params[:id]).tap do |resource|
authorize_resource(resource)
end
end
def find_resource(param)
scoped_resource.find(param)
end
def scoped_resource
resource_class.default_scoped
end
def apply_collection_includes(relation)
resource_includes = dashboard.collection_includes
return relation if resource_includes.empty?
relation.includes(*resource_includes)
end
def resource_params
params.require(resource_class.model_name.param_key).
permit(dashboard.permitted_attributes(action_name)).
transform_values { |v| read_param_value(v) }
end
def read_param_value(data)
if data.is_a?(ActionController::Parameters) && data[:type]
if data[:type] == Administrate::Field::Polymorphic.to_s
GlobalID::Locator.locate(data[:value])
else
raise "Unrecognised param data: #{data.inspect}"
end
elsif data.is_a?(ActionController::Parameters)
data.transform_values { |v| read_param_value(v) }
elsif data.is_a?(String) && data.blank?
nil
else
data
end
end
delegate :dashboard_class, :resource_class, :resource_name, :namespace,
to: :resource_resolver
helper_method :namespace
helper_method :resource_name
helper_method :resource_class
def resource_resolver
@resource_resolver ||=
Administrate::ResourceResolver.new(controller_path)
end
def translate_with_resource(key)
t(
"administrate.controller.#{key}",
resource: resource_resolver.resource_title,
)
end
def show_search_bar?
dashboard.attribute_types_for(
dashboard.all_attributes,
).any? { |_name, attribute| attribute.searchable? }
end
# Whether the current user is authorized to perform the named action on the
# resource.
#
# @param _resource [ActiveRecord::Base, Class, String, Symbol] The
# temptative target of the action, or the name of its class.
# @param _action_name [String, Symbol] The name of an action that might be
# possible to perform on a resource or resource class.
# @return [Boolean] `true` if the current user is authorized to perform the
# action on the resource. `false` otherwise.
def authorized_action?(_resource, _action_name)
true
end
helper_method :authorized_action?
# @deprecated Use {#authorized_action} instead. Note that the order of
# parameters is reversed in {#authorized_action}.
def show_action?(action, resource)
Administrate.warn_of_deprecated_authorization_method(__method__)
authorized_action?(resource, action)
end
helper_method :show_action?
def new_resource(params = {})
resource_class.new(params)
end
helper_method :new_resource
def authorize_resource(resource)
if authorized_action?(resource, action_name)
resource
else
raise Administrate::NotAuthorizedError.new(
action: action_name,
resource: resource,
)
end
end
def paginate_resources(resources)
resources.page(params[:_page]).per(records_per_page)
end
end
end