Skip to content

Commit 59ee746

Browse files
authored
Merge pull request #3101 from projectblacklight/rails_7.1_backport_bl_release_7.x
Backport of Rails 7.1 support to release-7.x
2 parents a1d2841 + af8b821 commit 59ee746

File tree

9 files changed

+93
-12
lines changed

9 files changed

+93
-12
lines changed

app/controllers/concerns/blacklight/bookmarks.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ def index
4242
@bookmarks = token_or_current_or_guest_user.bookmarks
4343
bookmark_ids = @bookmarks.collect { |b| b.document_id.to_s }
4444
@response, deprecated_document_list = search_service.fetch(bookmark_ids)
45-
@document_list = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(deprecated_document_list, "The @document_list instance variable is now deprecated and will be removed in Blacklight 8.0")
45+
@document_list = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(
46+
deprecated_document_list,
47+
"The @document_list instance variable is now deprecated",
48+
ActiveSupport::Deprecation.new("8.0", "blacklight")
49+
)
4650

4751
respond_to do |format|
4852
format.html {}

app/controllers/concerns/blacklight/catalog.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ module Blacklight::Catalog
3434
def index
3535
(@response, deprecated_document_list) = search_service.search_results
3636

37-
@document_list = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(deprecated_document_list, 'The @document_list instance variable is deprecated; use @response.documents instead.')
37+
@document_list = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(
38+
deprecated_document_list,
39+
'The @document_list instance variable is deprecated; use @response.documents instead.',
40+
ActiveSupport::Deprecation.new("8.0", "blacklight")
41+
)
3842

3943
respond_to do |format|
4044
format.html { store_preferred_view }
@@ -53,7 +57,11 @@ def index
5357
# to add responses for formats other than html or json see _Blacklight::Document::Export_
5458
def show
5559
deprecated_response, @document = search_service.fetch(params[:id])
56-
@response = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(deprecated_response, 'The @response instance variable is deprecated; use @document.response instead.')
60+
@response = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(
61+
deprecated_response,
62+
'The @response instance variable is deprecated; use @document.response instead.',
63+
ActiveSupport::Deprecation.new("8.0", "blacklight")
64+
)
5765

5866
respond_to do |format|
5967
format.html { @search_context = setup_next_and_previous_documents }

app/models/concerns/blacklight/document/active_model_shim.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ def repository
2828
def find id
2929
repository.find(id).documents.first
3030
end
31+
32+
# In Rails 7.1+, needs this method
33+
def composite_primary_key?
34+
false
35+
end
36+
37+
# In Rails 7.1+, needs this method
38+
def has_query_constraints?
39+
false
40+
end
3141
end
3242

3343
##

app/models/search.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ class Search < ApplicationRecord
44
belongs_to :user, optional: true
55

66
# use a backwards-compatible serializer until the Rails API stabilizes and we can evaluate for major-revision compatibility
7-
serialize :query_params, Blacklight::SearchParamsYamlCoder
7+
if ::Rails.version.to_f >= 7.1
8+
# non-deprecated coder: keyword arg for Rails 7.1+
9+
serialize :query_params, coder: Blacklight::SearchParamsYamlCoder
10+
else
11+
serialize :query_params, Blacklight::SearchParamsYamlCoder
12+
end
813

914
# A Search instance is considered a saved search if it has a user_id.
1015
def saved?

blacklight.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Gem::Specification.new do |s|
2525

2626
s.required_ruby_version = '>= 2.5'
2727

28-
s.add_dependency "rails", '>= 5.1', '< 7.1'
28+
s.add_dependency "rails", '>= 5.1', '< 7.2'
2929
s.add_dependency "globalid"
3030
s.add_dependency "jbuilder", '~> 2.7'
3131
s.add_dependency "kaminari", ">= 0.15" # the pagination (page 1,2,3, etc..) of our search results
@@ -36,7 +36,7 @@ Gem::Specification.new do |s|
3636
s.add_dependency 'hashdiff'
3737

3838
s.add_development_dependency "rsolr", ">= 1.0.6", "< 3" # Library for interacting with rSolr.
39-
s.add_development_dependency "rspec-rails", "~> 5.0"
39+
s.add_development_dependency "rspec-rails", ">= 5.0" # some versions tested need >= 6.0
4040
s.add_development_dependency "rspec-collection_matchers", ">= 1.0"
4141
s.add_development_dependency 'axe-core-rspec'
4242
s.add_development_dependency "capybara", '~> 3'

lib/blacklight/engine.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ module Blacklight
66
class Engine < Rails::Engine
77
engine_name "blacklight"
88

9+
config.before_configuration do
10+
# see https://github.com/fxn/zeitwerk#for_gem
11+
# Blacklight puts a generator into LOCAL APP lib/generators, so tell
12+
# zeitwerk to ignore the whole directory? If we're using zeitwerk
13+
#
14+
# https://github.com/cbeer/engine_cart/issues/117
15+
if Rails.try(:autoloaders).try(:main).respond_to?(:ignore)
16+
Rails.autoloaders.main.ignore(Rails.root.join('lib/generators'))
17+
end
18+
end
19+
920
# BlacklightHelper is needed by all helpers, so we inject it
1021
# into action view base here.
1122
initializer 'blacklight.helpers' do

spec/components/blacklight/facet_component_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,17 @@ def call
5151
end
5252

5353
before do
54-
controller.view_context.view_paths.unshift(RSpec::Rails::ViewExampleGroup::StubResolverCache.resolver_for('catalog/_facet_partial.html.erb' => 'facet partial'))
54+
# Not sure why we need to re-implement rspec's stub_template, but
55+
# we already were, and need a Rails 7.1+ safe alternate too
56+
# https://github.com/rspec/rspec-rails/commit/4d65bea0619955acb15023b9c3f57a3a53183da8
57+
# https://github.com/rspec/rspec-rails/issues/2696
58+
59+
replace_hash = { 'catalog/_facet_partial.html.erb' => 'facet partial' }
60+
if ::Rails.version.to_f >= 7.1
61+
controller.prepend_view_path(RSpec::Rails::ViewExampleGroup::StubResolverCache.resolver_for(replace_hash))
62+
else
63+
controller.view_context.view_paths.unshift(RSpec::Rails::ViewExampleGroup::StubResolverCache.resolver_for(replace_hash))
64+
end
5565
end
5666

5767
it 'renders the partial' do

spec/helpers/blacklight_helper_spec.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,17 @@
331331
blacklight_config.view.gallery(template: '/my/partial')
332332
end
333333

334-
def stub_template(hash)
335-
view.view_paths.unshift(ActionView::FixtureResolver.new(hash))
336-
end
337-
338334
it 'renders that template' do
339-
stub_template 'my/_partial.html.erb' => 'some content'
335+
# Not sure why we need to re-implement rspec's stub_template, but
336+
# we already were, and need a Rails 7.1+ safe alternate too
337+
# https://github.com/rspec/rspec-rails/commit/4d65bea0619955acb15023b9c3f57a3a53183da8
338+
# https://github.com/rspec/rspec-rails/issues/2696
339+
replace_hash = { 'my/_partial.html.erb' => 'some content' }
340+
if ::Rails.version.to_f >= 7.1
341+
controller.prepend_view_path(RSpec::Rails::ViewExampleGroup::StubResolverCache.resolver_for(replace_hash))
342+
else
343+
view.view_paths.unshift(ActionView::FixtureResolver.new(replace_hash))
344+
end
340345

341346
response = helper.render_document_index_with_view :gallery, [obj1, obj1]
342347

spec/spec_helper.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,31 @@
117117
# as the one that triggered the failure.
118118
Kernel.srand config.seed
119119
end
120+
121+
# RSpec's stub_template method needs a differnet implementation for Rails 7.1, that
122+
# isn't yet in an rspec-rails release.
123+
#
124+
# First rspec-rails tried this:
125+
# https://github.com/rspec/rspec-rails/commit/4d65bea0619955acb15023b9c3f57a3a53183da8
126+
#
127+
# But it was subject to this problem:
128+
# https://github.com/rspec/rspec-rails/issues/2696
129+
#
130+
# Below implementation appears to work for our purposes here, so we will patch it in
131+
# if we are on Rails 7.1+, and not yet rspec-rails 6.1 which we expect to have it.
132+
133+
if ::Rails.version.to_f >= 7.1 && Gem.loaded_specs["rspec-rails"].version.release < Gem::Version.new('6.1')
134+
135+
module RSpec
136+
module Rails
137+
module ViewExampleGroup
138+
module ExampleMethods
139+
def stub_template(hash)
140+
controller.prepend_view_path(StubResolverCache.resolver_for(hash))
141+
end
142+
end
143+
end
144+
end
145+
end
146+
147+
end

0 commit comments

Comments
 (0)