Skip to content

Commit

Permalink
Merge pull request #3099 from projectblacklight/rails_7.1_jrochkind
Browse files Browse the repository at this point in the history
Build and support on Rails 7.1
  • Loading branch information
tpendragon authored Nov 8, 2023
2 parents e208774 + e45b762 commit 92a0da7
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:
additional_engine_cart_rails_options: ['']
additional_name: ['']
include:
- ruby: '3.2.2'
rails_version: '7.1.1'
- ruby: '3.2.0'
rails_version: '7.0.4'
- ruby: '3.2.0'
Expand Down
10 changes: 10 additions & 0 deletions app/models/concerns/blacklight/document/active_model_shim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ def repository
def find id
repository.find(id).documents.first
end

# In Rails 7.1+, needs this method
def composite_primary_key?
false
end

# In Rails 7.1+, needs this method
def has_query_constraints?
false
end
end

##
Expand Down
7 changes: 6 additions & 1 deletion app/models/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
class Search < ApplicationRecord
belongs_to :user, optional: true

serialize :query_params, Blacklight::SearchParamsYamlCoder
if ::Rails.version.to_f >= 7.1
# non-deprecated coder: keyword arg for Rails 7.1+
serialize :query_params, coder: Blacklight::SearchParamsYamlCoder
else
serialize :query_params, Blacklight::SearchParamsYamlCoder
end

# A Search instance is considered a saved search if it has a user_id.
def saved?
Expand Down
12 changes: 12 additions & 0 deletions lib/blacklight/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ module Blacklight
class Engine < Rails::Engine
engine_name "blacklight"

config.before_configuration do
# see https://github.com/fxn/zeitwerk#for_gem
# Blacklight puts a generator into LOCAL APP lib/generators, so tell
# zeitwerk to ignore the whole directory? If we're using a recent
# enough version of Rails to have zeitwerk config
#
# See: https://github.com/cbeer/engine_cart/issues/117
if Rails.try(:autoloaders).try(:main).respond_to?(:ignore)
Rails.autoloaders.main.ignore(Rails.root.join('lib/generators'))
end
end

config.after_initialize do
Blacklight::Configuration.initialize_default_configuration
end
Expand Down
12 changes: 11 additions & 1 deletion spec/components/blacklight/facet_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,18 @@ def call
Blacklight::Configuration::FacetField.new(key: 'field', partial: 'catalog/facet_partial').normalize!
end

# Not sure why we need to re-implement rspec's stub_template, but
# we already were, and need a Rails 7.1+ safe alternate too
# https://github.com/rspec/rspec-rails/commit/4d65bea0619955acb15023b9c3f57a3a53183da8
# https://github.com/rspec/rspec-rails/issues/2696
before do
controller.view_context.view_paths.unshift(RSpec::Rails::ViewExampleGroup::StubResolverCache.resolver_for('catalog/_facet_partial.html.erb' => 'facet partial'))
replace_hash = { 'catalog/_facet_partial.html.erb' => 'facet partial' }

if ::Rails.version.to_f >= 7.1
controller.prepend_view_path(RSpec::Rails::ViewExampleGroup::StubResolverCache.resolver_for(replace_hash))
else
controller.view_context.view_paths.unshift(RSpec::Rails::ViewExampleGroup::StubResolverCache.resolver_for(replace_hash))
end
end

it 'renders the partial' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@
let(:view_context) { controller.view_context }

before do
view_context.view_paths.unshift(RSpec::Rails::ViewExampleGroup::StubResolverCache.resolver_for('application/_start_over.html.erb' => 'start over'))
# Not sure why we need to re-implement rspec's stub_template, but
# we already were, and need a Rails 7.1+ safe alternate too
# https://github.com/rspec/rspec-rails/commit/4d65bea0619955acb15023b9c3f57a3a53183da8
# https://github.com/rspec/rspec-rails/issues/2696
replace_hash = { 'application/_start_over.html.erb' => 'start over' }
if ::Rails.version.to_f >= 7.1
controller.prepend_view_path(RSpec::Rails::ViewExampleGroup::StubResolverCache.resolver_for(replace_hash))
else
view_context.view_paths.unshift(RSpec::Rails::ViewExampleGroup::StubResolverCache.resolver_for(replace_hash))
end

allow(view_context).to receive(:current_search_session).and_return current_search_session
allow(view_context).to receive(:link_back_to_catalog).with(any_args)
end
Expand Down
15 changes: 10 additions & 5 deletions spec/helpers/blacklight_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,17 @@
blacklight_config.view.gallery(template: '/my/partial')
end

def stub_template(hash)
view.view_paths.unshift(ActionView::FixtureResolver.new(hash))
end

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

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

Expand Down
28 changes: 28 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,31 @@
# as the one that triggered the failure.
Kernel.srand config.seed
end

# RSpec's stub_template method needs a differnet implementation for Rails 7.1, that
# isn't yet in an rspec-rails release.
#
# First rspec-rails tried this:
# https://github.com/rspec/rspec-rails/commit/4d65bea0619955acb15023b9c3f57a3a53183da8
#
# But it was subject to this problem:
# https://github.com/rspec/rspec-rails/issues/2696
#
# Below implementation appears to work for our purposes here, so we will patch it in
# if we are on Rails 7.1+, and not yet rspec-rails 6.1 which we expect to have it.

if ::Rails.version.to_f >= 7.1 && Gem.loaded_specs["rspec-rails"].version.release < Gem::Version.new('6.1')

module RSpec
module Rails
module ViewExampleGroup
module ExampleMethods
def stub_template(hash)
controller.prepend_view_path(StubResolverCache.resolver_for(hash))
end
end
end
end
end

end

0 comments on commit 92a0da7

Please sign in to comment.