2020-08-07 01:14:14 +02:00
|
|
|
class MigrateUserSettings < ActiveRecord::Migration[4.2]
|
2013-06-06 03:40:15 +02:00
|
|
|
def up
|
2013-09-02 10:03:12 +02:00
|
|
|
say_with_time 'Save old user settings in new RailsSettings module' do
|
2013-09-02 10:42:05 +02:00
|
|
|
# Allow setting default locale via env parameter
|
|
|
|
# This is used, when setting users language settings
|
|
|
|
default_locale = I18n.default_locale
|
|
|
|
tmp_locale = ENV['DEFAULT_LOCALE'].present? ? ENV['DEFAULT_LOCALE'].to_sym : default_locale
|
|
|
|
I18n.default_locale = tmp_locale
|
|
|
|
|
2013-09-02 10:03:12 +02:00
|
|
|
old_settings = ConfigurableSetting.all
|
|
|
|
|
|
|
|
old_settings.each do |old_setting|
|
|
|
|
# get target (user)
|
|
|
|
type = old_setting.configurable_type
|
|
|
|
id = old_setting.configurable_id
|
|
|
|
begin
|
2021-03-01 15:27:26 +01:00
|
|
|
user = type.constantize.find(id)
|
2013-09-02 10:03:12 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
Rails.logger.debug "Can't find configurable object with type: #{type.inspect}, id: #{id.inspect}"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
# get the data (settings)
|
|
|
|
name = old_setting.name
|
|
|
|
namespace = name.split('.')[0]
|
|
|
|
key = name.split('.')[1].underscore # Camelcase to underscore
|
|
|
|
|
|
|
|
# prepare value
|
|
|
|
value = YAML.load(old_setting.value)
|
|
|
|
value = value.nil? ? false : value
|
|
|
|
|
|
|
|
# set the settings_attributes (thanks to settings.merge! we can set them one by one)
|
|
|
|
user.settings_attributes = {
|
2021-03-01 15:27:26 +01:00
|
|
|
"#{namespace}" => {
|
|
|
|
"#{key}" => value
|
|
|
|
}
|
2013-06-06 03:40:15 +02:00
|
|
|
}
|
2013-09-02 10:03:12 +02:00
|
|
|
|
|
|
|
# save the user to apply after_save callback
|
|
|
|
user.save
|
|
|
|
end
|
2013-09-02 10:42:05 +02:00
|
|
|
|
|
|
|
I18n.default_locale = default_locale
|
2013-06-06 03:40:15 +02:00
|
|
|
end
|
2013-09-02 10:03:12 +02:00
|
|
|
|
|
|
|
drop_table :configurable_settings
|
2013-06-06 03:40:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
end
|
|
|
|
end
|
2013-09-02 10:03:12 +02:00
|
|
|
|
|
|
|
# this is the base class of all configurable settings
|
2021-03-01 15:27:26 +01:00
|
|
|
class ConfigurableSetting < ActiveRecord::Base; end
|