File: disable_sti_shared_examples.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (30 lines) | stat: -rw-r--r-- 1,214 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

# Checks whether STI is disabled in +models+.
#
# Parameter:
# - models: List of model classes
RSpec.shared_examples 'Model disables STI' do
  skip_sti_check = Gitlab::Utils.to_boolean(ENV['SKIP_STI_CHECK'], default: false)

  it 'does not allow STI', :aggregate_failures, unless: skip_sti_check do
    models.each do |model|
      next unless model
      next unless model < ApplicationRecord
      next unless model.name # skip unnamed/anonymous models
      next if model.table_name&.start_with?('_test') # skip test models that define the tables in specs
      next if model == model.base_class
      next if model.allow_legacy_sti_class

      expect(model).not_to have_attribute(model.inheritance_column),
        "Do not use Single Table Inheritance (`#{model.name}` inherits `#{model.base_class.name}`). " \
        "See https://docs.gitlab.com/ee/development/database/single_table_inheritance.html"
    end
  end
end

RSpec.shared_examples 'STI disabled', type: :model do # rubocop:disable RSpec/SharedGroupsMetadata -- Shared example is run within every spec tagged `type: :model`
  include_examples 'Model disables STI' do
    let(:models) { [described_class] }
  end
end