@@ -161,9 +161,9 @@ defmodule ExAdmin do
161
161
import ExAdmin.Utils , only: [ titleize: 1 , humanize: 1 , admin_resource_path: 2 ]
162
162
require ExAdmin.Register
163
163
164
- Code . ensure_compiled ExAdmin.Register
164
+ Code . ensure_compiled ( ExAdmin.Register )
165
165
166
- Module . register_attribute __MODULE__ , :registered , accumulate: true , persist: true
166
+ Module . register_attribute ( __MODULE__ , :registered , accumulate: true , persist: true )
167
167
168
168
@ default_theme ExAdmin.Theme.AdminLte2
169
169
@@ -177,24 +177,29 @@ defmodule ExAdmin do
177
177
# check for old xain.after_callback format and issue a compile time
178
178
# exception if not configured correctly.
179
179
180
- case Application . get_env :xain , :after_callback do
181
- nil -> nil
182
- { _ , _ } -> nil
180
+ case Application . get_env ( :xain , :after_callback ) do
181
+ nil ->
182
+ nil
183
+
184
+ { _ , _ } ->
185
+ nil
186
+
183
187
_ ->
184
- raise ExAdmin.CompileError , message: "Invalid xain_callback in config. Use {Phoenix.HTML, :raw}"
188
+ raise ExAdmin.CompileError ,
189
+ message: "Invalid xain_callback in config. Use {Phoenix.HTML, :raw}"
185
190
end
186
191
187
192
@ doc false
188
- def registered , do: Application . get_env ( :ex_admin , :modules , [ ] ) |> Enum . reverse
193
+ def registered , do: Application . get_env ( :ex_admin , :modules , [ ] ) |> Enum . reverse ( )
189
194
190
195
@ doc false
191
196
def put_data ( key , value ) do
192
- Agent . update __MODULE__ , & ( Map . put ( & 1 , key , value ) )
197
+ Agent . update ( __MODULE__ , & Map . put ( & 1 , key , value ) )
193
198
end
194
199
195
200
@ doc false
196
201
def get_data ( key ) do
197
- Agent . get __MODULE__ , & ( Map . get ( & 1 , key ) )
202
+ Agent . get ( __MODULE__ , & Map . get ( & 1 , key ) )
198
203
end
199
204
200
205
@ doc false
@@ -203,6 +208,7 @@ defmodule ExAdmin do
203
208
case get_registered_resource ( reg ) do
204
209
% { resource_model: rm } = item ->
205
210
{ rm , item }
211
+
206
212
% { type: :page } = item ->
207
213
{ nil , item }
208
214
end
@@ -220,33 +226,45 @@ defmodule ExAdmin do
220
226
get_registered_resource ( reg )
221
227
end
222
228
end
229
+
223
230
@ doc false
224
231
def get_registered ( resource_model ) do
225
232
get_all_registered ( )
226
233
|> Keyword . get ( resource_model )
227
234
end
228
235
229
-
230
236
def get_registered_by_association ( resource , assoc_name ) do
231
237
resource_model = resource . __struct__
232
- assoc_model = case resource_model . __schema__ ( :association , assoc_name ) do
233
- % { through: [ link1 , link2 ] } ->
234
- resource |> Ecto . build_assoc ( link1 ) |> Ecto . build_assoc ( link2 ) |> Map . get ( :__struct__ )
235
- % { queryable: assoc_model } ->
236
- assoc_model
237
- nil ->
238
- raise ArgumentError . exception ( "Association #{ assoc_name } is not found.\n #{ inspect ( resource_model ) } .__schema__(:association, #{ inspect ( assoc_name ) } ) returns nil" )
239
- _ ->
240
- raise ArgumentError . exception ( "Association type of #{ assoc_name } is not supported. Please, fill an issue." )
241
- end
242
- Enum . find get_registered ( ) , % { } , & ( Map . get ( & 1 , :resource_model ) == assoc_model )
243
- end
244
238
239
+ assoc_model =
240
+ case resource_model . __schema__ ( :association , assoc_name ) do
241
+ % { through: [ link1 , link2 ] } ->
242
+ resource |> Ecto . build_assoc ( link1 ) |> Ecto . build_assoc ( link2 ) |> Map . get ( :__struct__ )
243
+
244
+ % { queryable: assoc_model } ->
245
+ assoc_model
246
+
247
+ nil ->
248
+ raise ArgumentError . exception (
249
+ "Association #{ assoc_name } is not found.\n #{ inspect ( resource_model ) } .__schema__(:association, #{
250
+ inspect ( assoc_name )
251
+ } ) returns nil"
252
+ )
253
+
254
+ _ ->
255
+ raise ArgumentError . exception (
256
+ "Association type of #{ assoc_name } is not supported. Please, fill an issue."
257
+ )
258
+ end
259
+
260
+ Enum . find ( get_registered ( ) , % { } , & ( Map . get ( & 1 , :resource_model ) == assoc_model ) )
261
+ end
245
262
246
263
@ doc false
247
264
def get_controller_path ( % { } = resource ) do
248
- get_controller_path Map . get ( resource , :__struct__ )
265
+ get_controller_path ( Map . get ( resource , :__struct__ ) )
249
266
end
267
+
250
268
@ doc false
251
269
def get_controller_path ( resource_model ) when is_atom ( resource_model ) do
252
270
get_all_registered ( )
@@ -266,6 +284,7 @@ defmodule ExAdmin do
266
284
case get_registered ( name ) do
267
285
nil ->
268
286
& __MODULE__ . default_page_title_actions / 2
287
+
269
288
% { title_actions: actions } ->
270
289
actions
271
290
end
@@ -274,35 +293,42 @@ defmodule ExAdmin do
274
293
@ doc false
275
294
def get_name_from_controller ( controller ) when is_atom ( controller ) do
276
295
get_all_registered ( )
277
- |> Enum . find_value ( fn ( { name , % { controller: c_name } } ) ->
296
+ |> Enum . find_value ( fn { name , % { controller: c_name } } ->
278
297
if c_name == controller , do: name
279
298
end )
280
299
end
281
300
282
301
@ doc false
283
- def default_resource_title_actions ( % Plug.Conn { params: params } = conn , % { resource_model: resource_model } = defn ) do
302
+ def default_resource_title_actions (
303
+ % Plug.Conn { params: params } = conn ,
304
+ % { resource_model: resource_model } = defn
305
+ ) do
284
306
singular = ExAdmin.Utils . displayable_name_singular ( conn ) |> titleize
285
307
actions = defn . actions
308
+
286
309
case Utils . action_name ( conn ) do
287
310
:show ->
288
311
id = Map . get ( params , "id" )
289
- Enum . reduce ( [ :edit , :new , :delete ] , [ ] , fn ( action , acc ) ->
312
+
313
+ Enum . reduce ( [ :edit , :new , :delete ] , [ ] , fn action , acc ->
290
314
if Utils . authorized_action? ( conn , action , resource_model ) do
291
- [ { action , action_button ( conn , defn , singular , :show , action , actions , id ) } | acc ]
315
+ [ { action , action_button ( conn , defn , singular , :show , action , actions , id ) } | acc ]
292
316
else
293
317
acc
294
318
end
295
319
end )
296
320
|> add_custom_actions ( :show , actions , id )
297
- |> Enum . reverse
321
+ |> Enum . reverse ( )
322
+
298
323
action when action in [ :index , :edit ] ->
299
324
if Utils . authorized_action? ( conn , action , resource_model ) do
300
325
[ { :new , action_button ( conn , defn , singular , action , :new , actions ) } ]
301
326
else
302
327
[ ]
303
328
end
304
329
|> add_custom_actions ( action , actions )
305
- |> Enum . reverse
330
+ |> Enum . reverse ( )
331
+
306
332
_ ->
307
333
[ ]
308
334
end
@@ -340,46 +366,54 @@ defmodule ExAdmin do
340
366
341
367
defp add_custom_actions ( acc , action , actions , id \\ nil )
342
368
defp add_custom_actions ( acc , _action , [ ] , _id ) , do: acc
369
+
343
370
defp add_custom_actions ( acc , action , [ { action , button } | actions ] , id ) do
344
371
import ExAdmin.ViewHelpers
345
- endpoint ( ) # remove the compiler warning
346
- { fun , _ } = Code . eval_quoted button , [ id: id ] , __ENV__
372
+ # remove the compiler warning
373
+ endpoint ( )
374
+ { fun , _ } = Code . eval_quoted ( button , [ id: id ] , __ENV__ )
375
+
347
376
cond do
348
377
is_function ( fun , 1 ) -> [ fun . ( id ) | acc ]
349
378
is_function ( fun , 0 ) -> [ fun . ( ) | acc ]
350
- true -> acc
379
+ true -> acc
351
380
end
352
381
|> add_custom_actions ( action , actions , id )
353
382
end
354
- defp add_custom_actions ( acc , action , [ _ | actions ] , id ) do
383
+
384
+ defp add_custom_actions ( acc , action , [ _ | actions ] , id ) do
355
385
add_custom_actions ( acc , action , actions , id )
356
386
end
357
387
358
388
defp action_link ( conn , name , :delete , _id ) do
359
- { name ,
360
- [ href: admin_resource_path ( conn , :destroy ) ,
361
- "data-confirm": Utils . confirm_message ,
362
- "data-method": :delete , rel: :nofollow ] }
389
+ { name , [
390
+ href: admin_resource_path ( conn , :destroy ) ,
391
+ "data-confirm": Utils . confirm_message ( ) ,
392
+ "data-method": :delete ,
393
+ rel: :nofollow
394
+ ] }
363
395
end
396
+
364
397
defp action_link ( conn , name , action , _id ) do
365
398
{ name , [ href: admin_resource_path ( conn , action ) ] }
366
399
end
367
400
368
401
@ doc false
369
402
def has_action? ( conn , defn , action ) do
370
403
if ExAdmin.Utils . authorized_action? ( conn , action , defn ) ,
371
- do: _has_action? ( defn , action ) , else: false
404
+ do: _has_action? ( defn , action ) ,
405
+ else: false
372
406
end
373
407
374
408
defp _has_action? ( defn , action ) do
375
- except = Keyword . get defn . actions , :except , [ ]
376
- only = Keyword . get defn . actions , :only , [ ]
409
+ except = Keyword . get ( defn . actions , :except , [ ] )
410
+ only = Keyword . get ( defn . actions , :only , [ ] )
411
+
377
412
cond do
378
413
action in defn . actions -> true
379
414
action in only -> true
380
415
action in except -> false
381
416
true -> false
382
417
end
383
418
end
384
-
385
419
end
0 commit comments