From f5913399aa70c0d5dc3038da85d0997e0d2f3dbc Mon Sep 17 00:00:00 2001 From: Andy Zhao Date: Tue, 20 Mar 2018 16:25:09 -0400 Subject: [PATCH] Allow changing user for comments in admin panel (#113) * Allow changing user for comments in admin panel * Add successful flash msg * Use user_id instead of name for comment admin --- app/controllers/admin/comments_controller.rb | 28 +++++++++++--------- app/controllers/admin/users_controller.rb | 1 + app/dashboards/comment_dashboard.rb | 1 + 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index c74e48319..1a82ae1d9 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -1,19 +1,21 @@ module Admin class CommentsController < Admin::ApplicationController - # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: - # - # def index - # super - # @resources = Comment.all.paginate(10, params[:page]) - # end + def update + comment = Comment.find(params[:id]) + if comment.update(comment_params) + flash[:notice] = "Comment successfully updated" + redirect_to "/admin/comments/#{comment.id}" + else + flash.now[:error] = comment.errors.full_messages + render :new, locals: { page: Administrate::Page::Form.new(dashboard, comment) } + end + end - # Define a custom finder by overriding the `find_resource` method: - # def find_resource(param) - # Comment.find_by!(slug: param) - # end + private - # See https://administrate-docs.herokuapp.com/customizing_controller_actions - # for more information + def comment_params + accessible = %i[user_id body_markdown deleted] + params.require(:comment).permit(accessible) + end end end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index bb3bba9de..4ca157c2c 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -4,6 +4,7 @@ module Admin user = User.find(params[:id]) UserRoleService.new(user).check_for_roles(params[:user]) if user.errors.messages.blank? && user.update(user_params) + flash[:notice] = "User successfully updated" redirect_to "/admin/users/#{params[:id]}" else render_with_errors(user) diff --git a/app/dashboards/comment_dashboard.rb b/app/dashboards/comment_dashboard.rb index 29a990df1..a2f6ec35a 100644 --- a/app/dashboards/comment_dashboard.rb +++ b/app/dashboards/comment_dashboard.rb @@ -61,6 +61,7 @@ class CommentDashboard < Administrate::BaseDashboard # an array of attributes that will be displayed # on the model's form (`new` and `edit`) pages. FORM_ATTRIBUTES = [ + :user_id, :body_markdown, :deleted, ]