Skip to content

Commit

Permalink
Reintroduce and deprecate whitelisted_params config
Browse files Browse the repository at this point in the history
This allows people to switch to the new config without
breaking their applications or requiring a new release
for libraries using this gem.
  • Loading branch information
kennyadsl committed Dec 14, 2020
1 parent ac1592f commit 6b5f71e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/canonical-rails.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "canonical-rails/engine"
require "canonical-rails/deprecation"

module CanonicalRails

Expand All @@ -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 = []

Expand All @@ -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
7 changes: 7 additions & 0 deletions lib/canonical-rails/deprecation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require 'active_support/deprecation'

module CanonicalRails
Deprecation = ActiveSupport::Deprecation.new('1.0', 'CanonicalRails')
end
20 changes: 20 additions & 0 deletions spec/helpers/canonical_rails/tag_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6b5f71e

Please sign in to comment.