[HelpPages] Fix json & move modaction logging (#700)

* Fix #492 (json error when no help pages exist)
* Move modactions into class
This commit is contained in:
Donovan Daniels 2024-11-04 09:36:52 -06:00 committed by GitHub
parent 4cf67e9231
commit ad567bfc9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 16 deletions

View File

@ -3,8 +3,7 @@
class HelpController < ApplicationController
respond_to :html, :json
helper :wiki_pages
before_action :admin_only, only: [:new, :create, :edit,
:update, :destroy]
before_action :admin_only, except: %i[index show]
def show
if params[:id] =~ /\A\d+\Z/
@ -18,16 +17,14 @@ class HelpController < ApplicationController
redirect_to help_pages_path
end
end
format.json do
raise ActiveRecord::RecordNotFound if @help.blank?
render json: @help
end
end
end
def index
@help_pages = HelpPage.help_index
respond_with(@help_pages)
respond_with(@help_pages) do |format|
format.json { render json: @help_pages.to_json }
end
end
def new
@ -42,27 +39,20 @@ class HelpController < ApplicationController
def create
@help = HelpPage.create(help_params)
if @help.valid?
flash[:notice] = 'Help page created'
ModAction.log(:help_create, {name: @help.name, wiki_page: @help.wiki_page})
end
flash[:notice] = "Help page created" if @help.valid?
respond_with(@help)
end
def update
@help = HelpPage.find(params[:id])
@help.update(help_params)
if @help.valid?
flash[:notice] = "Help entry updated"
ModAction.log(:help_update,{name: @help.name, wiki_page: @help.wiki_page})
end
flash[:notice] = "Help entry updated" if @help.valid?
respond_with(@help)
end
def destroy
@help = HelpPage.find(params[:id])
@help.destroy
ModAction.log(:help_delete, {name: @help.name, wiki_page: @help.wiki_page})
respond_with(@help)
end

View File

@ -5,7 +5,10 @@ class HelpPage < ApplicationRecord
validates :wiki_page, :name, presence: true
normalizes :name, with: ->(name) { name.downcase.strip.tr(" ", "_") }
validate :wiki_page_exists
after_create :log_create
after_update :log_update
after_destroy :invalidate_cache
after_destroy :log_destroy
after_save :invalidate_cache
belongs_to :wiki, class_name: "WikiPage", foreign_key: "wiki_page", primary_key: "title"
@ -37,4 +40,20 @@ class HelpPage < ApplicationRecord
def self.help_index
Cache.fetch("help_index", expires_in: 12.hours) { HelpPage.all.sort_by(&:pretty_title) }
end
module LogMethods
def log_create
ModAction.log(:help_create, { name: name, wiki_page: wiki_page })
end
def log_update
ModAction.log(:help_update, { name: name, wiki_page: wiki_page })
end
def log_destroy
ModAction.log(:help_delete, { name: name, wiki_page: wiki_page })
end
end
include LogMethods
end