From a17cb6a51506f3008e624813a693128975dbede9 Mon Sep 17 00:00:00 2001 From: jorg-vr Date: Thu, 26 Oct 2023 15:54:40 +0200 Subject: [PATCH] Move validation to policy --- app/models/activity.rb | 1 - app/policies/activity_policy.rb | 4 +++- test/policies/exercise_policy_test.rb | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/activity.rb b/app/models/activity.rb index 0aa8c2de07..97b92ec465 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -57,7 +57,6 @@ class Activity < ApplicationRecord has_many :labels, through: :activity_labels validates :path, uniqueness: { scope: :repository_id, case_sensitive: false }, allow_nil: true - validates :draft, inclusion: { in: [false] }, unless: :draft_was token_generator :repository_token, length: 64 token_generator :access_token diff --git a/app/policies/activity_policy.rb b/app/policies/activity_policy.rb index cbc4b0f7a4..ea813b34a5 100644 --- a/app/policies/activity_policy.rb +++ b/app/policies/activity_policy.rb @@ -75,7 +75,9 @@ def read? def permitted_attributes if update? - %i[access name_nl name_en draft] + permitted_attributes = %i[access name_nl name_en] + permitted_attributes << :draft if record.draft? + permitted_attributes else [] end diff --git a/test/policies/exercise_policy_test.rb b/test/policies/exercise_policy_test.rb index 3a6ebe1d46..185bb15333 100644 --- a/test/policies/exercise_policy_test.rb +++ b/test/policies/exercise_policy_test.rb @@ -27,6 +27,6 @@ def test_destroy; end policy = ExercisePolicy.new(create(:temporary_user), create(:exercise, :valid)) User.any_instance.stubs(:repository_admin?).returns(true) - assert_equal %i[access name_nl name_en draft], policy.permitted_attributes + assert_equal %i[access name_nl name_en], policy.permitted_attributes end end