-
Notifications
You must be signed in to change notification settings - Fork 897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to reset settings to default value and delete newly added keys #17482
Changes from 3 commits
9888390
45beae8
5d1861e
b960144
76b928f
c68a4fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
module Vmdb | ||
class Settings | ||
extend Vmdb::SettingsWalker::ClassMethods | ||
include Vmdb::Logging | ||
|
||
class ConfigurationInvalid < StandardError | ||
attr_accessor :errors | ||
|
@@ -21,6 +22,11 @@ def initialize(errors) | |
PASSWORD_FIELDS = Vmdb::SettingsWalker::PASSWORD_FIELDS | ||
DUMP_LOG_FILE = Rails.root.join("log/last_settings.txt").freeze | ||
|
||
# RESET_COMMAND remove record for selected key and specific resource | ||
# RESET_ALL_COMMAND remove all records for selected key and all resources | ||
MAGIC_VALUES = [DELETE_COMMAND = "<<reset>>".freeze, | ||
DELETE_ALL_COMMAND = "<<reset_all>>".freeze].freeze | ||
|
||
cattr_accessor :last_loaded | ||
|
||
def self.init | ||
|
@@ -62,8 +68,9 @@ def self.valid? | |
|
||
def self.save!(resource, hash) | ||
new_settings = build_without_local(resource).load!.merge!(hash).to_hash | ||
settings_to_validate = remove_magic_values(new_settings.deep_clone) | ||
|
||
valid, errors = validate(new_settings) | ||
valid, errors = validate(settings_to_validate) | ||
raise ConfigurationInvalid.new(errors) unless valid # rubocop:disable Style/RaiseArgs | ||
|
||
hash_for_parent = parent_settings_without_local(resource).load!.to_hash | ||
|
@@ -188,6 +195,7 @@ def self.apply_settings_changes(resource, deltas) | |
|
||
deltas.each do |delta| | ||
record = index.delete(delta[:key]) | ||
next if process_magic_value(resource, delta) | ||
if record | ||
record.update_attributes!(delta) | ||
else | ||
|
@@ -198,5 +206,34 @@ def self.apply_settings_changes(resource, deltas) | |
end | ||
end | ||
private_class_method :apply_settings_changes | ||
|
||
def self.process_magic_value(resource, delta) | ||
return false unless MAGIC_VALUES.include?(delta[:value]) | ||
case delta[:value] | ||
when DELETE_COMMAND | ||
_log.info("removing custom settings #{delta[:key]} on #{resource.class} id:#{resource.id}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should read There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
resource.settings_changes.where("key LIKE ?", "#{delta[:key]}%").destroy_all | ||
when DELETE_ALL_COMMAND | ||
_log.info("resetting #{delta[:key]} to defaul for all resorces") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo |
||
SettingsChange.where("key LIKE ?", "#{delta[:key]}%").destroy_all | ||
end | ||
true | ||
end | ||
private_class_method :process_magic_value | ||
|
||
def self.walk_hash(hash, path = [], &block) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand why we are keeping track of this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are right, we do not need it here if we are ignoring Arrays @Fryguy should we accommodate resetting nested Hash as an element of Array in |
||
hash.each do |key, value| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer using the existing walk method, which already handles this. Even so, I don't think Iike this manual thing at all, A reset is really just taking the parent's value at that level and replacing it. Then, let the differ handle the rest. |
||
key_path = path.dup << key | ||
yield key, value, key_path, hash | ||
walk_hash(value, key_path, &block) if value&.class == hash.class | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @carbonin it is always |
||
end | ||
end | ||
private_class_method :walk_hash | ||
|
||
def self.remove_magic_values(hash) | ||
walk_hash(hash) { |key, value, _path, owner| owner.delete(key) if MAGIC_VALUES.include?(value) } | ||
hash | ||
end | ||
private_class_method :remove_magic_values | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these should match the name of the constant, right?
DELETE_COMMAND
andDELETE_ALL_COMMAND
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍