diff --git a/lib/canonical-rails.rb b/lib/canonical-rails.rb index 15e20c8..cd6526a 100644 --- a/lib/canonical-rails.rb +++ b/lib/canonical-rails.rb @@ -1,4 +1,5 @@ require "canonical-rails/engine" +require "canonical-rails/deprecation" module CanonicalRails @@ -23,6 +24,10 @@ def self.setup mattr_accessor :collection_actions @@collection_actions = [:index] + # @deprecated: use config.allowed_parameters instead + mattr_accessor :whitelisted_parameters + @@whitelisted_parameters = [] + mattr_accessor :allowed_parameters @@allowed_parameters = [] @@ -34,6 +39,11 @@ def self.sym_collection_actions end def self.sym_allowed_parameters - @@sym_allowed_parameters ||= self.allowed_parameters.map(&:to_sym) + @@sym_allowed_parameters ||= if self.whitelisted_parameters.empty? + self.allowed_parameters.map(&:to_sym) + else + CanonicalRails::Deprecation.warn('config.whitelisted_parameters is deprecated, please use config.allowed_parameters instead.') + self.whitelisted_parameters.map(&:to_sym) + end end end diff --git a/lib/canonical-rails/deprecation.rb b/lib/canonical-rails/deprecation.rb new file mode 100644 index 0000000..29e28c8 --- /dev/null +++ b/lib/canonical-rails/deprecation.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'active_support/deprecation' + +module CanonicalRails + Deprecation = ActiveSupport::Deprecation.new('1.0', 'CanonicalRails') +end diff --git a/spec/helpers/canonical_rails/tag_helper_spec.rb b/spec/helpers/canonical_rails/tag_helper_spec.rb index 6666d99..81cdab3 100644 --- a/spec/helpers/canonical_rails/tag_helper_spec.rb +++ b/spec/helpers/canonical_rails/tag_helper_spec.rb @@ -210,6 +210,26 @@ end end end + + describe 'with the old config.whitelisted_parameters' do + before do + CanonicalRails.whitelisted_parameters = ['page'] + allow_any_instance_of(controller.class) + .to receive(:params) + .and_return(ActionController::Parameters.new('i-will' => 'kill-your-seo', 'page' => '5')) + controller.request.path_parameters = { controller: 'our_resources', action: 'index' } + end + + after do + CanonicalRails.class_variable_set(:@@sym_whitelisted_parameters, nil) + end + + it 'emits a deprecation warning and keeps working' do + expect(CanonicalRails::Deprecation).to receive(:warn).once + expect(helper.allowed_params['page']).to eq(5) + expect(helper.allowed_params['i-will']).to eq(5) + end + end end describe 'when host is specified' do