Commit 742787df authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '336452-fix-terms-deletion' into 'master'

Prevent terms from being created if blank

See merge request gitlab-org/gitlab!66437
parents fe91c0a6 93a88ddb
...@@ -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