Skip to content
Snippets Groups Projects
Verified Commit 8cfaab7c authored by Tan Le's avatar Tan Le :two:
Browse files

Fix delegate predicate method offence

This fixes the delegate predicate method offence raised by Rubocop. We
would like to retain the false/true return value of predicate method
even when the delegated class is nil.
parent 439f314e
No related branches found
No related tags found
1 merge request!89820Fix delegate predicate method offence in integrations
......@@ -2,6 +2,5 @@
Gitlab/DelegatePredicateMethods:
Exclude:
- app/models/clusters/cluster.rb
- app/models/concerns/integrations/base_data_fields.rb
- app/models/project.rb
- ee/app/models/concerns/ee/ci/metadatable.rb
......@@ -14,8 +14,6 @@ module BaseDataFields
# https://gitlab.com/gitlab-org/gitlab/-/issues/331953
belongs_to :integration, inverse_of: self.table_name.to_sym, foreign_key: foreign_key_name
delegate :activated?, to: :integration, allow_nil: true
validates :integration, presence: true
end
......@@ -40,6 +38,10 @@ def foreign_key_name
end
end
def activated?
!!integration&.activated?
end
def to_database_hash
as_json(
only: self.class.column_names
......
......@@ -3,13 +3,47 @@
require 'spec_helper'
RSpec.shared_examples Integrations::BaseDataFields do
subject(:model) { described_class.new }
describe 'associations' do
it { is_expected.to belong_to :integration }
end
describe '#activated?' do
subject(:activated?) { model.activated? }
context 'with integration' do
let(:integration) { instance_spy(Integration, activated?: activated) }
before do
allow(model).to receive(:integration).and_return(integration)
end
context 'with value set to false' do
let(:activated) { false }
it { is_expected.to eq(false) }
end
context 'with value set to true' do
let(:activated) { true }
it { is_expected.to eq(true) }
end
end
context 'without integration' do
before do
allow(model).to receive(:integration).and_return(nil)
end
it { is_expected.to eq(false) }
end
end
describe '#to_database_hash' do
it 'does not include certain attributes' do
hash = described_class.new.to_database_hash
hash = model.to_database_hash
expect(hash.keys).not_to include('id', 'service_id', 'integration_id', 'created_at', 'updated_at')
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment