pundit: convert artist commentaries to pundit.

This commit is contained in:
evazion
2020-03-19 14:12:52 -05:00
parent 3d83c3154e
commit ce1133dd69
2 changed files with 28 additions and 17 deletions

View File

@@ -1,9 +1,8 @@
class ArtistCommentariesController < ApplicationController class ArtistCommentariesController < ApplicationController
respond_to :html, :xml, :json, :js respond_to :html, :xml, :json, :js
before_action :member_only, only: [:create_or_update, :revert]
def index def index
@commentaries = ArtistCommentary.paginated_search(params) @commentaries = authorize ArtistCommentary.paginated_search(params)
@commentaries = @commentaries.includes(post: :uploader) if request.format.html? @commentaries = @commentaries.includes(post: :uploader) if request.format.html?
respond_with(@commentaries) respond_with(@commentaries)
@@ -14,9 +13,9 @@ class ArtistCommentariesController < ApplicationController
def show def show
if params[:id] if params[:id]
@commentary = ArtistCommentary.find(params[:id]) @commentary = authorize ArtistCommentary.find(params[:id])
else else
@commentary = ArtistCommentary.find_by_post_id!(params[:post_id]) @commentary = authorize ArtistCommentary.find_by_post_id!(params[:post_id])
end end
respond_with(@commentary) do |format| respond_with(@commentary) do |format|
@@ -25,24 +24,16 @@ class ArtistCommentariesController < ApplicationController
end end
def create_or_update def create_or_update
@artist_commentary = ArtistCommentary.find_or_initialize_by(post_id: params.dig(:artist_commentary, :post_id)) post_id = params[:artist_commentary].delete(:post_id)
@artist_commentary.update(commentary_params) @artist_commentary = authorize ArtistCommentary.find_or_initialize_by(post_id: post_id)
@artist_commentary.update(permitted_attributes(@artist_commentary))
respond_with(@artist_commentary) respond_with(@artist_commentary)
end end
def revert def revert
@artist_commentary = ArtistCommentary.find_by_post_id!(params[:id]) @artist_commentary = authorize ArtistCommentary.find_by_post_id!(params[:id])
@version = @artist_commentary.versions.find(params[:version_id]) @version = @artist_commentary.versions.find(params[:version_id])
@artist_commentary.revert_to!(@version) @artist_commentary.revert_to!(@version)
end respond_with(@artist_commentary)
private
def commentary_params
params.fetch(:artist_commentary, {}).except(:post_id).permit(%i[
original_description original_title translated_description translated_title
remove_commentary_tag remove_commentary_request_tag remove_commentary_check_tag remove_partial_commentary_tag
add_commentary_tag add_commentary_request_tag add_commentary_check_tag add_partial_commentary_tag
])
end end
end end

View File

@@ -0,0 +1,20 @@
class ArtistCommentaryPolicy < ApplicationPolicy
def create_or_update?
unbanned?
end
def revert?
unbanned?
end
def permitted_attributes
%i[
original_description original_title
translated_description translated_title
remove_commentary_tag remove_commentary_request_tag
remove_commentary_check_tag remove_partial_commentary_tag
add_commentary_tag add_commentary_request_tag
add_commentary_check_tag add_partial_commentary_tag
]
end
end