Skip to content

Commit 2f3feff

Browse files
authored
Merge pull request #3545 from projectblacklight/safe-nav
Use the safe navigation operator
2 parents abdddb5 + b377745 commit 2f3feff

File tree

8 files changed

+15
-31
lines changed

8 files changed

+15
-31
lines changed

.rubocop_todo.yml

-14
Original file line numberDiff line numberDiff line change
@@ -532,20 +532,6 @@ Style/RescueStandardError:
532532
- 'lib/blacklight/configuration/fields.rb'
533533
- 'lib/railties/blacklight.rake'
534534

535-
# Offense count: 11
536-
# This cop supports unsafe autocorrection (--autocorrect-all).
537-
# Configuration parameters: ConvertCodeThatCanStartToReturnNil, AllowedMethods, MaxChainLength.
538-
# AllowedMethods: present?, blank?, presence, try, try!
539-
Style/SafeNavigation:
540-
Exclude:
541-
- 'app/controllers/concerns/blacklight/bookmarks.rb'
542-
- 'app/controllers/concerns/blacklight/controller.rb'
543-
- 'app/services/blacklight/field_retriever.rb'
544-
- 'lib/blacklight.rb'
545-
- 'lib/blacklight/configuration/facet_field.rb'
546-
- 'lib/blacklight/configuration/search_field.rb'
547-
- 'lib/blacklight/solr/search_builder_behavior.rb'
548-
549535
# Offense count: 6
550536
# This cop supports safe autocorrection (--autocorrect).
551537
# Configuration parameters: AllowAsExpressionSeparator.

app/controllers/concerns/blacklight/bookmarks.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def destroy
110110

111111
success = @bookmarks.all? do |bookmark|
112112
bookmark = current_or_guest_user.bookmarks.find_by(bookmark)
113-
bookmark && bookmark.delete && bookmark.destroyed?
113+
bookmark&.delete && bookmark.destroyed?
114114
end
115115

116116
if success

app/controllers/concerns/blacklight/controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def access_denied
130130
# send the user home if the access was previously denied by the same
131131
# request to avoid sending the user back to the login page
132132
# (e.g. protected page -> logout -> returned to protected page -> home)
133-
redirect_to(root_url) && flash.discard && return if request.referer && request.referer.ends_with?(request.fullpath)
133+
redirect_to(root_url) && flash.discard && return if request.referer&.ends_with?(request.fullpath)
134134

135135
redirect_to(root_url) && return unless has_user_authentication_provider?
136136

app/services/blacklight/field_retriever.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def fetch
4141

4242
def retrieve_simple
4343
# regular document field
44-
if field_config.default && field_config.default.is_a?(Proc)
44+
if field_config.default.is_a?(Proc)
4545
document.fetch(field_config.field, &field_config.default)
4646
else
4747
document.fetch(field_config.field, field_config.default)

lib/blacklight.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def self.blacklight_yml?
9999
end
100100

101101
def self.logger
102-
@logger ||= (::Rails.logger if defined? Rails && Rails.respond_to?(:logger))
102+
@logger ||= ::Rails.logger
103103
end
104104

105105
def self.logger= logger

lib/blacklight/configuration/facet_field.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Configuration::FacetField < Blacklight::Configuration::Field
6767

6868
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
6969
def normalize! blacklight_config = nil
70-
query.stringify_keys! if query
70+
query&.stringify_keys!
7171

7272
normalize_pivot_config! if pivot
7373
self.collapse = true if collapse.nil?

lib/blacklight/configuration/search_field.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def normalize! blacklight_config = nil
1414
self.if = include_in_simple_select if self.if.nil?
1515

1616
super
17-
self.qt ||= blacklight_config.default_solr_params[:qt] if blacklight_config && blacklight_config.default_solr_params
17+
self.qt ||= blacklight_config.default_solr_params[:qt] if blacklight_config&.default_solr_params
1818

1919
self
2020
end

lib/blacklight/solr/search_builder_behavior.rb

+9-11
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,8 @@ def add_facetting_to_solr(solr_parameters)
190190
solr_parameters[:"f.#{facet.field}.facet.sort"] = facet.sort
191191
end
192192

193-
if facet.solr_params
194-
facet.solr_params.each do |k, v|
195-
solr_parameters[:"f.#{facet.field}.#{k}"] = v
196-
end
193+
facet.solr_params&.each do |k, v|
194+
solr_parameters[:"f.#{facet.field}.#{k}"] = v
197195
end
198196

199197
limit = facet_limit_with_pagination(field_name)
@@ -203,9 +201,9 @@ def add_facetting_to_solr(solr_parameters)
203201

204202
def add_solr_fields_to_query solr_parameters
205203
blacklight_config.show_fields.select(&method(:should_add_field_to_request?)).each_value do |field|
206-
field.solr_params.each do |k, v|
204+
field.solr_params&.each do |k, v|
207205
solr_parameters[:"f.#{field.field}.#{k}"] = v
208-
end if field.solr_params
206+
end
209207
end
210208

211209
blacklight_config.index_fields.select(&method(:should_add_field_to_request?)).each_value do |field|
@@ -214,9 +212,9 @@ def add_solr_fields_to_query solr_parameters
214212
solr_parameters.append_highlight_field field.field
215213
end
216214

217-
field.solr_params.each do |k, v|
215+
field.solr_params&.each do |k, v|
218216
solr_parameters[:"f.#{field.field}.#{k}"] = v
219-
end if field.solr_params
217+
end
220218
end
221219
end
222220

@@ -350,9 +348,9 @@ def facet_value_to_fq_string(facet_field, value, use_local_params: true)
350348
solr_field ||= facet_field
351349

352350
local_params = []
353-
local_params << "tag=#{facet_config.tag}" if use_local_params && facet_config && facet_config.tag
351+
local_params << "tag=#{facet_config.tag}" if use_local_params && facet_config&.tag
354352

355-
if facet_config && facet_config.query
353+
if facet_config&.query
356354
if facet_config.query[value]
357355
facet_config.query[value][:fq]
358356
else
@@ -380,7 +378,7 @@ def facet_inclusive_value_to_fq_string(facet_field, values)
380378
facet_config = blacklight_config.facet_fields[facet_field]
381379

382380
local_params = []
383-
local_params << "tag=#{facet_config.tag}" if facet_config && facet_config.tag
381+
local_params << "tag=#{facet_config.tag}" if facet_config&.tag
384382

385383
solr_filters = values.each_with_object({}).with_index do |(v, h), index|
386384
h["f_inclusive.#{facet_field}.#{index}"] = facet_value_to_fq_string(facet_field, v, use_local_params: false)

0 commit comments

Comments
 (0)