Add rake task to rename existing setting keys

This commit is contained in:
Julian Herrero
2019-03-13 12:52:28 +01:00
parent 4df9a1a1b4
commit e04fd74cf1
2 changed files with 95 additions and 0 deletions

View File

@@ -55,4 +55,24 @@ namespace :settings do
end end
end end
desc "Rename existing settings"
task rename_setting_keys: :environment do
Setting.rename_key from: "map_latitude", to: "map.latitude"
Setting.rename_key from: "map_longitude", to: "map.longitude"
Setting.rename_key from: "map_zoom", to: "map.zoom"
Setting.rename_key from: "feature.debates", to: "process.debates"
Setting.rename_key from: "feature.proposals", to: "process.proposals"
Setting.rename_key from: "feature.polls", to: "process.polls"
Setting.rename_key from: "feature.budgets", to: "process.budgets"
Setting.rename_key from: "feature.legislation", to: "process.legislation"
Setting.rename_key from: "per_page_code_head", to: "html.per_page_code_head"
Setting.rename_key from: "per_page_code_body", to: "html.per_page_code_body"
Setting.rename_key from: "feature.homepage.widgets.feeds.proposals", to: "homepage.widgets.feeds.proposals"
Setting.rename_key from: "feature.homepage.widgets.feeds.debates", to: "homepage.widgets.feeds.debates"
Setting.rename_key from: "feature.homepage.widgets.feeds.processes", to: "homepage.widgets.feeds.processes"
end
end end

View File

@@ -100,4 +100,79 @@ describe Setting do
end end
describe "#rename_setting_keys" do
let :run_rake_task do
Rake::Task["settings:rename_setting_keys"].reenable
Rake.application.invoke_task "settings:rename_setting_keys"
end
let :old_keys do
%w[map_latitude map_longitude map_zoom feature.debates feature.proposals feature.polls
feature.budgets feature.legislation per_page_code_head per_page_code_body
feature.homepage.widgets.feeds.proposals feature.homepage.widgets.feeds.debates
feature.homepage.widgets.feeds.processes]
end
let :new_keys do
%w[map.latitude map.longitude map.zoom process.debates process.proposals process.polls
process.budgets process.legislation html.per_page_code_head html.per_page_code_body
homepage.widgets.feeds.proposals homepage.widgets.feeds.debates
homepage.widgets.feeds.processes]
end
context "with existing old settings" do
it "rename all settings keys keeping the same value" do
Setting.destroy_all
old_keys.each { |old_key| Setting[old_key] = "old value" }
run_rake_task
new_keys.each do |new_key|
expect(Setting[new_key]).to eq "old value"
end
old_keys.each do |old_key|
expect(Setting.where(key: old_key)).not_to exist
end
end
end
context "without existing old settings" do
it "initializes all settings with null value" do
Setting.destroy_all
run_rake_task
new_keys.each do |new_key|
expect(Setting[new_key]).to eq nil
end
old_keys.each do |old_key|
expect(Setting.where(key: old_key)).not_to exist
end
end
end
context "with already existing new settings" do
it "does not change the value of the new settings even if the old setting exist" do
Setting.destroy_all
old_keys.each { |old_key| Setting[old_key] = "old value" }
new_keys.each { |new_key| Setting[new_key] = "new value" }
run_rake_task
new_keys.each do |new_key|
expect(Setting[new_key]).to eq "new value"
end
old_keys.each do |old_key|
expect(Setting.where(key: old_key)).not_to exist
end
end
end
end
end end