Skip to content

Commit b784ae4

Browse files
committed
patch active admin comments page
1 parent 78beef3 commit b784ae4

File tree

1 file changed

+79
-3
lines changed

1 file changed

+79
-3
lines changed

lib/active_admin/nested_namespace.rb

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ def name_paths
118118
end
119119

120120
end
121-
122-
# TODO: patch active_admin/orm/active_record/comments.rb
123-
121+
124122
ActiveAdmin::Comment.class_eval do
125123
def self.find_for_resource_in_namespace(resource, name)
126124
where(
@@ -265,6 +263,84 @@ def add_classes_to_body
265263
@body.add_class(active_admin_namespace.name_path.map(&:to_s).join('_') + '_namespace')
266264
end
267265
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+
268344
end
269345
end
270346
end

0 commit comments

Comments
 (0)