Skip to content

Commit

Permalink
Config option to exclude models (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
slawosz authored Nov 7, 2024
1 parent f9bbbb2 commit a89f3bd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,10 @@ en:
description: |
Google generated cloud credentials file
default: ENV['GOOGLE_CLOUD_CREDENTIALS']
excluded_models_proc:
description: |
A proc which will be called during model initialization. It allows to disable models
which should not be used. Each model is passed to bloc and if bloc returns true for the model,
it wont be used by the application. Eg: proc { |x| x.to_s =~ /Namespace::/ } will exclude all
models namespaced with Namespace
default: proc { |_model| false }
3 changes: 3 additions & 0 deletions lib/dfe/analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def self.config
gcp_scope
google_cloud_credentials
excluded_paths
excluded_models_proc
]

@config ||= Struct.new(*configurables).new
Expand All @@ -82,6 +83,7 @@ def self.configure
config.bigquery_maintenance_window ||= ENV.fetch('BIGQUERY_MAINTENANCE_WINDOW', nil)
config.azure_federated_auth ||= false
config.excluded_paths ||= []
config.excluded_models_proc ||= proc { |_model| false }

return unless config.azure_federated_auth

Expand Down Expand Up @@ -240,6 +242,7 @@ def self.entity_model_mapping

ActiveRecord::Base.descendants
.reject(&:abstract_class?)
.reject(&DfE::Analytics.config.excluded_models_proc)
.group_by(&:table_name)
.except(*rails_tables)
end
Expand Down
29 changes: 29 additions & 0 deletions spec/dfe/analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,33 @@
end
end
end

describe 'excluding models via configuration' do
with_model :Candidate do
table do |t|
t.string :email_address
t.string :first_name
t.string :last_name
t.string :dob
end
end

with_model :Teacher do
table do |t|
t.string :email_address
t.string :first_name
t.string :last_name
t.string :dob
end
end

before do
DfE::Analytics.config.excluded_models_proc = proc { |x| x.to_s =~ /Teacher/ }
end

it 'does not use disabled model' do
expect(DfE::Analytics.all_entities_in_application.length).to eq(1)
expect(DfE::Analytics.all_entities_in_application.first.to_s).to match(/with_model_candidates/)
end
end
end

0 comments on commit a89f3bd

Please sign in to comment.