-
Notifications
You must be signed in to change notification settings - Fork 987
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #24801 - Always seed default taxonomies
This is needed so we can enforce enabling taxonomies. If this seed is run on an existing system, all existing objects will be associated to the default taxonomy. For systems that already have taxonomies this seed will remain a noop.
- Loading branch information
Showing
6 changed files
with
31 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
# Create an initial organization if specified | ||
if SETTINGS[:organizations_enabled] && ENV['SEED_ORGANIZATION'] && !Organization.any? | ||
Organization.without_auditing do | ||
original_user, User.current = User.current, User.anonymous_admin | ||
@organization = Organization.where(:name => ENV['SEED_ORGANIZATION']).first_or_create | ||
User.current = original_user | ||
end | ||
end | ||
# This runs last since we want to associate all objects we can to the default taxonomies | ||
|
||
# Create an initial location if specified | ||
if SETTINGS[:locations_enabled] && ENV['SEED_LOCATION'] && !Location.any? | ||
Location.without_auditing do | ||
original_user, User.current = User.current, User.anonymous_admin | ||
@location = Location.where(:name => ENV['SEED_LOCATION']).first_or_create! | ||
User.current = original_user | ||
end | ||
end | ||
# some associations make no sense to set. Default templates are assigned during creation. | ||
skip_associations = [:associated_audits, :audits, :default_users, :hosts, | ||
:location_parameters, :organization_parameters, | ||
:taxable_taxonomies ] + Template.descendants.map {|type| type.to_s.tableize.to_sym} | ||
|
||
User.as_anonymous_admin do | ||
[Location, Organization].select(&:none?).each do |taxonomy| | ||
taxonomy.without_auditing do | ||
tax_name = ENV.fetch("SEED_#{taxonomy.to_s.upcase}", "Default #{taxonomy}") | ||
tax = taxonomy.create!(name: tax_name) | ||
associations = taxonomy.reflect_on_all_associations.reject {|assoc| skip_associations.include?(assoc.name)} | ||
associations.each do |association| | ||
tax.send("#{association.name}=", association.klass.all) | ||
end | ||
|
||
# Add the initial location to the initial organization to prevent mismatches | ||
# when a host is created that uses them | ||
if @organization && @location && @organization.locations.exclude?(@location) | ||
@organization.locations << @location | ||
# Mass update when we can | ||
tax_id = "#{taxonomy.to_s.parameterize}_id" | ||
Host::Managed.update_all(tax_id => tax.id) | ||
User.update_all("default_#{tax_id}": tax.id) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters