@@ -118,9 +118,7 @@ def name_paths
118
118
end
119
119
120
120
end
121
-
122
- # TODO: patch active_admin/orm/active_record/comments.rb
123
-
121
+
124
122
ActiveAdmin ::Comment . class_eval do
125
123
def self . find_for_resource_in_namespace ( resource , name )
126
124
where (
@@ -265,6 +263,84 @@ def add_classes_to_body
265
263
@body . add_class ( active_admin_namespace . name_path . map ( &:to_s ) . join ( '_' ) + '_namespace' )
266
264
end
267
265
end
266
+
267
+ # patch - active_admin/orm/active_record/comments.rb
268
+ ActiveAdmin . after_load do |app |
269
+ app . namespaces . each do |namespace |
270
+
271
+ # Un-register default comment pages
272
+ namespace . resources . instance_variable_get ( :@collection ) . delete_if { |k , v | v . instance_variable_get ( :@resource_class_name ) == '::ActiveAdmin::Comment' }
273
+
274
+ # Re-register comments pages with nested namespaces
275
+ namespace . register ActiveAdmin ::Comment , as : namespace . comments_registration_name do
276
+ actions :index , :show , :create , :destroy
277
+
278
+ menu namespace . comments ? namespace . comments_menu : false
279
+
280
+ config . comments = false # Don't allow comments on comments
281
+ config . batch_actions = false # The default destroy batch action isn't showing up anyway...
282
+
283
+ scope :all , show_count : false
284
+ # Register a scope for every namespace that exists.
285
+ # The current namespace will be the default scope.
286
+ app . namespaces . map ( &:name_path ) . sort { |x , y | x [ 0 ] <=> y [ 0 ] } . each do |name_path |
287
+ scope "/#{ name_path . join ( '/' ) } " , default : namespace . name_path == name_path do |scope |
288
+ scope . where namespace : name_path . to_s
289
+ end
290
+ end
291
+
292
+ # Store the author and namespace
293
+ before_save do |comment |
294
+ comment . namespace = active_admin_config . namespace . name_path
295
+ comment . author = current_active_admin_user
296
+ end
297
+
298
+ controller do
299
+ # Prevent N+1 queries
300
+ def scoped_collection
301
+ super . includes ( :author , :resource )
302
+ end
303
+
304
+ # Redirect to the resource show page after comment creation
305
+ def create
306
+ create! do |success , failure |
307
+ success . html do
308
+ ActiveAdmin ::Dependency . rails . redirect_back self , active_admin_root
309
+ end
310
+ failure . html do
311
+ flash [ :error ] = I18n . t 'active_admin.comments.errors.empty_text'
312
+ ActiveAdmin ::Dependency . rails . redirect_back self , active_admin_root
313
+ end
314
+ end
315
+
316
+ def destroy
317
+ destroy! do |success , failure |
318
+ success . html do
319
+ ActiveAdmin ::Dependency . rails . redirect_back self , active_admin_root
320
+ end
321
+ failure . html do
322
+ ActiveAdmin ::Dependency . rails . redirect_back self , active_admin_root
323
+ end
324
+ end
325
+ end
326
+ end
327
+ end
328
+
329
+ permit_params :body , :namespace , :resource_id , :resource_type
330
+
331
+ index do
332
+ column I18n . t ( 'active_admin.comments.resource_type' ) , :resource_type
333
+ column I18n . t ( 'active_admin.comments.author_type' ) , :author_type
334
+ column I18n . t ( 'active_admin.comments.resource' ) , :resource
335
+ column I18n . t ( 'active_admin.comments.author' ) , :author
336
+ column I18n . t ( 'active_admin.comments.body' ) , :body
337
+ column I18n . t ( 'active_admin.comments.created_at' ) , :created_at
338
+ actions
339
+ end
340
+ end
341
+ end
342
+ end
343
+
268
344
end
269
345
end
270
346
end
0 commit comments