Allow hiding and un-hiding of screenshot by moderators

This commit is contained in:
Christoph Haas 2021-03-10 01:04:28 +01:00
parent f31502c333
commit 5d0e4a520e
6 changed files with 62 additions and 10 deletions

View file

@ -32,18 +32,19 @@ class Ability
# See the wiki for details:
# https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
# Everybody can see public screenshots
can :view, Screenshot, approved: true, hidden:false
if user.present? # Logged-in users
# Allow to view all public/approved screenshots
can :view, Screenshot, approved: true
# Allow to view any own uploads (even not-yet-approved)
# Allow to view own uploads (even not-yet-approved)
can :view, Screenshot, user_id: user.id
can :destroy, Screenshot, user_id: user.id
if user.admin_role?
can :approve, Screenshot
can :destroy, Screenshot
can :hide, Screenshot
can :unhide, Screenshot
can :hide, Screenshot, hidden: false
can :unhide, Screenshot, hidden: true
can :destroy, User
can :view, Screenshot
can :destroy, Package
@ -51,11 +52,10 @@ class Ability
end
if user.moderator_role?
can :approve, Screenshot
can :hide, Screenshot
can :hide, Screenshot, hidden: false
can :unhide, Screenshot, hidden: true
can :view, Screenshot
end
else # Nobody logged in
can :view, Screenshot, approved: true
end
end
end

View file

@ -5,7 +5,7 @@ class Screenshot < ApplicationRecord
# Default sorting:
# - unapproved first (to see them at the top when moderating)
# - otherwise show newest first
default_scope { order(approved: :asc, created_at: :desc) }
default_scope { order(approved: :asc, hidden: :asc, created_at: :desc) }
# Shrine
include ImageUploader::Attachment(:simage) # adds an `simage` virtual attribute
@ -63,6 +63,16 @@ class Screenshot < ApplicationRecord
self.save!
end
# Hide a screenshot from the public. Moderator level can do this.
def hide!
self.hidden = true
self.save!
end
def unhide!
self.hidden = false
self.save!
end
# Get the newest screenshots regardless of the package it belongs to
def self.newest
self.order(created_at: :desc)