akkounts/app/models/user_preferences.rb
Râu Cao 4e2e13108c
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Update release notes draft
Refactor user preferences, add defaults from file
* Turn prefs into a flat hash structure, since nesting is not worth the
trouble
* Add a custom serializer class for prefs
* Add a config file for defaults and merge set prefs with unset ones
* Use booleans for "true" and "false", and integers where appropriate
2023-04-05 17:02:35 +02:00

30 lines
666 B
Ruby

DEFAULT_PREFS = YAML.load_file("#{Rails.root}/config/default_preferences.yml")
class UserPreferences
def self.dump(value)
process(value).to_yaml
end
def self.load(string)
stored_prefs = YAML.load(string || "{}")
DEFAULT_PREFS.merge(stored_prefs).with_indifferent_access
end
def self.is_integer?(value)
value.to_i.to_s == value
end
def self.process(hash)
hash.each do |key, value|
if value == "true"
hash[key] = true
elsif value == "false"
hash[key] = false
elsif value.is_a?(String) && is_integer?(value)
hash[key] = value.to_i
end
end
hash.stringify_keys!.to_h
end
end