Skip to content

Commit

Permalink
deprecate Blacklight::Engine.config access without top-level key
Browse files Browse the repository at this point in the history
- #2607
- move facet_missing_param to second-level key #2608
  • Loading branch information
barmintor committed Jan 19, 2022
1 parent a16544b commit 4553b1b
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/controllers/concerns/blacklight/bookmarks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Blacklight::Bookmarks
before_action :verify_user

blacklight_config.track_search_session = false
blacklight_config.http_method = Blacklight::Engine.config.bookmarks_http_method
blacklight_config.http_method = Blacklight::Engine.config.blacklight.bookmarks_http_method
blacklight_config.add_results_collection_tool(:clear_bookmarks_widget)

blacklight_config.show.document_actions[:bookmark].if = false if blacklight_config.show.document_actions[:bookmark]
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/concerns/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,13 @@ def validate_sms_params
end

def sms_mappings
Blacklight::Engine.config.sms_mappings
Blacklight::Engine.config.blacklight.sms_mappings
end

def validate_email_params
if params[:to].blank?
flash[:error] = I18n.t('blacklight.email.errors.to.blank')
elsif !params[:to].match(Blacklight::Engine.config.email_regexp)
elsif !params[:to].match(Blacklight::Engine.config.blacklight.email_regexp)
flash[:error] = I18n.t('blacklight.email.errors.to.invalid', to: params[:to])
end

Expand Down
66 changes: 66 additions & 0 deletions lib/blacklight/deprecations/engine_configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

module Blacklight
module Deprecations
module EngineConfiguration
# rubocop:disable Style/RedundantSelf, Style/HashSyntax
# @deprecated
def bookmarks_http_method
self.blacklight.bookmarks_http_method
end
deprecation_deprecate bookmarks_http_method: 'Moved to `blacklight.bookmarks_http_method`'

# @deprecated
def bookmarks_http_method=(val)
self.blacklight.bookmarks_http_method = val
end
deprecation_deprecate :'bookmarks_http_method=' => 'Moved to `blacklight.bookmarks_http_method=`'

# @deprecated
def email_regexp
self.blacklight.email_regexp
end
deprecation_deprecate email_regexp: 'Moved to `blacklight.email_regexp`'

# @deprecated
def email_regexp=(val)
self.blacklight.email_regexp = val
end
deprecation_deprecate :'email_regexp=' => 'Moved to `blacklight.email_regexp=`'

# @deprecated
def facet_missing_param
self.blacklight.facet_missing_param
end
deprecation_deprecate facet_missing_param: 'Moved to `blacklight.facet_missing_param`'

# @deprecated
def facet_missing_param=(val)
self.blacklight.facet_missing_param = val
end
deprecation_deprecate :'facet_missing_param=' => 'Moved to `blacklight.facet_missing_param=`'

# @deprecated
def sms_mappings
self.blacklight.sms_mappings
end
deprecation_deprecate sms_mappings: 'Moved to `blacklight.sms_mappings`'

# @deprecated
def sms_mappings=(val)
self.blacklight.sms_mappings = val
end
deprecation_deprecate :'sms_mappings=' => 'Moved to `blacklight.sms_mappings=`'
# rubocop:enable Style/RedundantSelf, Style/HashSyntax

def self.deprecate_in(object)
class << object
extend Deprecation
self.deprecation_horizon = 'blacklight 8.0'

include Blacklight::Deprecations::EngineConfiguration
end
end
end
end
end
21 changes: 15 additions & 6 deletions lib/blacklight/engine.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
require 'blacklight/deprecations/engine_configuration'
require 'view_component/engine'

module Blacklight
Expand Down Expand Up @@ -29,7 +30,9 @@ class Engine < Rails::Engine
end
end

Blacklight::Engine.config.sms_mappings = {
bl_global_config = OpenStructWithHashAccess.new

bl_global_config.sms_mappings = {
'Virgin' => 'vmobl.com',
'AT&T' => 'txt.att.net',
'Verizon' => 'vtext.com',
Expand All @@ -41,14 +44,20 @@ class Engine < Rails::Engine
'Google Fi' => 'msg.fi.google.com'
}

config.bookmarks_http_method = :post
bl_global_config.bookmarks_http_method = :post

config.email_regexp = defined?(Devise) ? Devise.email_regexp : /\A[^@\s]+@[^@\s]+\z/
bl_global_config.email_regexp = defined?(Devise) ? Devise.email_regexp : /\A[^@\s]+@[^@\s]+\z/

config.action_dispatch.rescue_responses["Blacklight::Exceptions::RecordNotFound"] = :not_found
bl_global_config.facet_missing_param = '[* TO *]'

config.enable_search_bar_autofocus = false
# Anything that goes into Blacklight::Engine.config is stored as a class
# variable on Railtie::Configuration. we're going to encapsulate all the
# Blacklight specific stuff in this single struct:
Blacklight::Engine.config.blacklight = bl_global_config

config.facet_missing_param = '[* TO *]'
# Deprecate top-level access to legacy engine configuration
Blacklight::Deprecations::EngineConfiguration.deprecate_in(Blacklight::Engine.config)

config.action_dispatch.rescue_responses["Blacklight::Exceptions::RecordNotFound"] = :not_found
end
end
8 changes: 4 additions & 4 deletions lib/blacklight/search_state/filter_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def add(item)

if value == Blacklight::SearchState::FilterField::MISSING
url_key = "-#{key}"
value = Blacklight::Engine.config.facet_missing_param
value = Blacklight::Engine.config.blacklight.facet_missing_param
end

param = :f_inclusive if value.is_a?(Array)
Expand Down Expand Up @@ -82,7 +82,7 @@ def remove(item)

if value == Blacklight::SearchState::FilterField::MISSING
url_key = "-#{key}"
value = Blacklight::Engine.config.facet_missing_param
value = Blacklight::Engine.config.blacklight.facet_missing_param
end

param = :f_inclusive if value.is_a?(Array)
Expand Down Expand Up @@ -114,7 +114,7 @@ def values
params = search_state.params
f = Array(params.dig(:f, key))
f_inclusive = [params.dig(:f_inclusive, key)] if params.dig(:f_inclusive, key).present?
f_missing = [Blacklight::SearchState::FilterField::MISSING] if params.dig(:f, "-#{key}")&.any? { |v| v == Blacklight::Engine.config.facet_missing_param }
f_missing = [Blacklight::SearchState::FilterField::MISSING] if params.dig(:f, "-#{key}")&.any? { |v| v == Blacklight::Engine.config.blacklight.facet_missing_param }

f + (f_inclusive || []) + (f_missing || [])
end
Expand All @@ -133,7 +133,7 @@ def include?(item)
if value.is_a?(Array)
(params.dig(:f_inclusive, key) || []).to_set == value.to_set
elsif value == Blacklight::SearchState::FilterField::MISSING
(params.dig(:f, "-#{key}") || []).include?(Blacklight::Engine.config.facet_missing_param)
(params.dig(:f, "-#{key}") || []).include?(Blacklight::Engine.config.blacklight.facet_missing_param)
else
(params.dig(:f, key) || []).include?(value)
end
Expand Down
41 changes: 41 additions & 0 deletions spec/lib/blacklight/engine_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

RSpec.describe Blacklight::Engine do
[:bookmarks_http_method, :email_regexp, :facet_missing_param, :sms_mappings].each do |dep_key|
describe "config.#{dep_key}" do
subject { described_class.config }

let(:unlikely_value) { 'unlikely value' }

it 'is deprecated' do
allow(Deprecation).to receive(:warn)
subject.send(dep_key)
expect(Deprecation).to have_received(:warn)
end

it 'delegates to config.blacklight' do
allow(subject.blacklight).to receive(dep_key).and_return(unlikely_value)
expect(subject.send(dep_key)).to eql(unlikely_value)
end
end

describe "config.#{dep_key}=" do
subject { described_class.config }

let(:unlikely_value) { 'unlikely value' }

it 'is deprecated' do
allow(Deprecation).to receive(:warn)
allow(subject.blacklight).to receive(:"#{dep_key}=").with(unlikely_value)
subject.send(:"#{dep_key}=", unlikely_value)
expect(Deprecation).to have_received(:warn)
end

it 'delegates to config.blacklight' do
allow(subject.blacklight).to receive(:"#{dep_key}=").with(unlikely_value)
subject.send(:"#{dep_key}=", unlikely_value)
expect(subject.blacklight).to have_received(:"#{dep_key}=").with(unlikely_value)
end
end
end
end

0 comments on commit 4553b1b

Please sign in to comment.