Commit 93a88ddb authored by Max Woolf's avatar Max Woolf

Prevent terms from being created if blank

https://gitlab.com/gitlab-org/gitlab/-/merge_requests/65007
introduced the ability to remove t&cs in the admin panel
but this had the unintended consequence of removing
the terms whenever any other application
setting was changed.

This commit reverts the changes to avoid this problem
but does reintroduce the problem that admins can no longer
remove terms and conditions.

Changelog: fixed
parent b67a72df
...@@ -3,13 +3,12 @@ ...@@ -3,13 +3,12 @@
class ApplicationSetting class ApplicationSetting
class Term < ApplicationRecord class Term < ApplicationRecord
include CacheMarkdownField include CacheMarkdownField
include NullifyIfBlank
has_many :term_agreements has_many :term_agreements
cache_markdown_field :terms cache_markdown_field :terms
nullify_if_blank :terms validates :terms, presence: true
def self.latest def self.latest
order(:id).last order(:id).last
......
...@@ -67,8 +67,10 @@ module ApplicationSettings ...@@ -67,8 +67,10 @@ module ApplicationSettings
end end
def update_terms(terms) def update_terms(terms)
return unless terms.present?
# Avoid creating a new terms record if the text is exactly the same. # Avoid creating a new terms record if the text is exactly the same.
terms = terms&.strip terms = terms.strip
return if terms == @application_setting.terms return if terms == @application_setting.terms
ApplicationSetting::Term.create(terms: terms) ApplicationSetting::Term.create(terms: terms)
......
# frozen_string_literal: true
class ChangeApplicationSettingTermsNotNull < ActiveRecord::Migration[6.1]
def up
execute("UPDATE application_setting_terms SET terms = '' WHERE terms IS NULL")
change_column_null :application_setting_terms, :terms, false
end
def down
change_column_null :application_setting_terms, :terms, true
end
end
6096780be4fae007485f150a019fc4555153e4b22b893d5fe29be36834d970a9
\ No newline at end of file
...@@ -9216,7 +9216,7 @@ ALTER SEQUENCE appearances_id_seq OWNED BY appearances.id; ...@@ -9216,7 +9216,7 @@ ALTER SEQUENCE appearances_id_seq OWNED BY appearances.id;
CREATE TABLE application_setting_terms ( CREATE TABLE application_setting_terms (
id integer NOT NULL, id integer NOT NULL,
cached_markdown_version integer, cached_markdown_version integer,
terms text, terms text NOT NULL,
terms_html text terms_html text
); );
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe ApplicationSetting::Term do RSpec.describe ApplicationSetting::Term do
it { is_expected.to nullify_if_blank(:terms) } it { is_expected.to validate_presence_of(:terms) }
describe '.latest' do describe '.latest' do
it 'finds the latest terms' do it 'finds the latest terms' do
......
...@@ -23,8 +23,8 @@ RSpec.describe ApplicationSettings::UpdateService do ...@@ -23,8 +23,8 @@ RSpec.describe ApplicationSettings::UpdateService do
context 'when the passed terms are blank' do context 'when the passed terms are blank' do
let(:params) { { terms: '' } } let(:params) { { terms: '' } }
it 'does create terms' do it 'does not create terms' do
expect { subject.execute }.to change { ApplicationSetting::Term.count }.by(1) expect { subject.execute }.not_to change { ApplicationSetting::Term.count }
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment