diff --git a/lib/alchemy/config.rb b/lib/alchemy/config.rb index 3daaa40e42..e54b421c63 100644 --- a/lib/alchemy/config.rb +++ b/lib/alchemy/config.rb @@ -8,8 +8,10 @@ class << self # @param name [String] # def get(name) + check_deprecation(name) show[name.to_s] end + alias_method :parameter, :get # Returns a merged configuration of the following files @@ -25,6 +27,15 @@ def show @config ||= merge_configs!(alchemy_config, main_app_config, env_specific_config) end + # A list of deprecated configurations + # a value of nil means there is no new default + # any not nil value is the new default + def deprecated_configs + { + url_nesting: true, + } + end + private # Alchemy default configuration @@ -60,6 +71,20 @@ def merge_configs!(*config_files) config_files.each { |h| config.merge!(h.stringify_keys!) } config end + + def check_deprecation(name) + if deprecated_configs.key?(name.to_sym) + config = deprecated_configs[name.to_sym] + if config.nil? + Alchemy::Deprecation.warn("#{name} configuration is deprecated and will be removed from Alchemy 5.0") + else + value = show[name.to_s] + if value != config + Alchemy::Deprecation.warn("Setting #{name} configuration to #{value} is deprecated and will be always #{config} in Alchemy 5.0") + end + end + end + end end end end diff --git a/lib/tasks/alchemy/convert.rake b/lib/tasks/alchemy/convert.rake index 2ae48e28e6..adb4a3152a 100644 --- a/lib/tasks/alchemy/convert.rake +++ b/lib/tasks/alchemy/convert.rake @@ -4,6 +4,7 @@ namespace :alchemy do namespace :urlnames do desc "Converts the urlname of all pages to nested url paths." task to_nested: [:environment] do + Alchemy::Deprecation.warn('alchemy:convert:urlnames:to_nested task is deprecated and will be removed from Alchemy 5.0') unless Alchemy::Config.get(:url_nesting) raise "\nURL nesting is disabled! Please enable url_nesting in `config/alchemy/config.yml` first.\n\n" end @@ -20,6 +21,7 @@ namespace :alchemy do desc "Converts the urlname of all pages to contain the slug only." task to_slug: [:environment] do + Alchemy::Deprecation.warn('alchemy:convert:urlnames:to_slug task is deprecated and will be removed from Alchemy 5.0') if Alchemy::Config.get(:url_nesting) raise "\nURL nesting is enabled! Please disable url_nesting in `config/alchemy/config.yml` first.\n\n" end diff --git a/spec/libraries/config_spec.rb b/spec/libraries/config_spec.rb index f5ac55e063..9283846e9b 100644 --- a/spec/libraries/config_spec.rb +++ b/spec/libraries/config_spec.rb @@ -11,8 +11,39 @@ module Alchemy end it "should return the requested part of the config" do - expect(Config).to receive(:show).and_return({"mailer" => {"setting" => "true"}}) - expect(Config.get(:mailer)).to eq({"setting" => "true"}) + expect(Config).to receive(:show).and_return({ "mailer" => { "setting" => "true" } }) + expect(Config.get(:mailer)).to eq({ "setting" => "true" }) + end + + context "if config is deprecated" do + context "without a new default" do + before do + expect(described_class).to receive(:deprecated_configs).at_least(:once) do + { url_nesting: nil } + end + end + + it "warns" do + expect(Alchemy::Deprecation).to receive(:warn) + Config.get(:url_nesting) + end + end + + context "with a new default" do + context "and current value is not the default" do + before do + expect(described_class).to receive(:show).at_least(:once) do + { "url_nesting" => false } + end + end + + it "warns about new default" do + expect(Alchemy::Deprecation).to \ + receive(:warn).with("Setting url_nesting configuration to false is deprecated and will be always true in Alchemy 5.0") + Config.get(:url_nesting) + end + end + end end end @@ -20,8 +51,8 @@ module Alchemy let(:main_app_config_path) { "#{Rails.root}/config/alchemy/config.yml" } it "should call and return .read_file with the correct config path" do - expect(Config).to receive(:read_file).with(main_app_config_path).once.and_return({setting: "true"}) - expect(Config.send(:main_app_config)).to eq({setting: "true"}) + expect(Config).to receive(:read_file).with(main_app_config_path).once.and_return({ setting: "true" }) + expect(Config.send(:main_app_config)).to eq({ setting: "true" }) end end @@ -29,8 +60,8 @@ module Alchemy let(:env_specific_config_path) { "#{Rails.root}/config/alchemy/#{Rails.env}.config.yml" } it "should call and return .read_file with the correct config path" do - expect(Config).to receive(:read_file).with(env_specific_config_path).once.and_return({setting: "true"}) - expect(Config.send(:env_specific_config)).to eq({setting: "true"}) + expect(Config).to receive(:read_file).with(env_specific_config_path).once.and_return({ setting: "true" }) + expect(Config.send(:env_specific_config)).to eq({ setting: "true" }) end end @@ -39,17 +70,17 @@ module Alchemy before { Config.instance_variable_set("@config", nil) } it "should call and return .merge_configs!" do - expect(Config).to receive(:merge_configs!).once.and_return({setting: "true"}) - expect(Config.show).to eq({setting: "true"}) + expect(Config).to receive(:merge_configs!).once.and_return({ setting: "true" }) + expect(Config.show).to eq({ setting: "true" }) end end context "when ivar @config was already set" do - before { Config.instance_variable_set("@config", {setting: "true"}) } + before { Config.instance_variable_set("@config", { setting: "true" }) } after { Config.instance_variable_set("@config", nil) } it "should have memoized the return value of .merge_configs!" do - expect(Config.send(:show)).to eq({setting: "true"}) + expect(Config.send(:show)).to eq({ setting: "true" }) end end end @@ -77,11 +108,11 @@ module Alchemy describe ".merge_configs!" do let(:config_1) do - {setting_1: "same", other_setting: "something"} + { setting_1: "same", other_setting: "something" } end let(:config_2) do - {setting_1: "same", setting_2: "anything"} + { setting_1: "same", setting_2: "anything" } end it "should stringify the keys" do @@ -97,7 +128,9 @@ module Alchemy context "when configs containing same keys" do it "should merge them together" do expect(Config.send(:merge_configs!, config_1, config_2)).to eq( - {"setting_1" => "same", "other_setting" => "something", "setting_2" => "anything"}, + "setting_1" => "same", + "other_setting" => "something", + "setting_2" => "anything", ) end end