Skip to content

Commit

Permalink
Fix nested model loading with Zeitwerk (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
jturkel authored Sep 26, 2022
1 parent e62f0b2 commit bb91294
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 46 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# avromatic changelog

## 4.1.1
- Fix eager loading of nested models when using the Zeitwerk classloader with Rails.

## 4.1.0
- Add support for specifying a subject for the avro schema when building an Avromatic model

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ correct order:
Avromatic.configure do |config|
config.eager_load_models = [
# reference any extended models that should be defined first
MyNestedModel
'MyNestedModel'
]
end
```
Expand Down
19 changes: 8 additions & 11 deletions lib/avromatic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class << self

def self.configure
yield self
eager_load_models!
end

def self.build_schema_registry
Expand Down Expand Up @@ -66,18 +65,16 @@ def self.build_messaging!

# This method is called as a Rails to_prepare hook after the application
# first initializes during boot-up and prior to each code reloading.
# For the first call during boot-up we do not want to clear the nested_models.
def self.prepare!(skip_clear: false)
unless skip_clear
nested_models.clear
if schema_store
if schema_store.respond_to?(:clear_schemas)
schema_store.clear_schemas
elsif schema_store.respond_to?(:clear)
schema_store.clear
end
def self.prepare!
nested_models.clear
if schema_store
if schema_store.respond_to?(:clear_schemas)
schema_store.clear_schemas
elsif schema_store.respond_to?(:clear)
schema_store.clear
end
end

eager_load_models!
end

Expand Down
13 changes: 1 addition & 12 deletions lib/avromatic/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,8 @@ class Railtie < Rails::Railtie
config.logger = Rails.logger
end

# Rails calls the to_prepare hook once during boot-up, after running
# initializers. After the to_prepare call during boot-up, no code will
# we reloaded, so we need to retain the contents of the nested_models
# registry.
#
# For subsequent calls to to_prepare (in development), the nested_models
# registry is cleared and repopulated by explicitly referencing the
# eager_loaded_models.
first_prepare = true

Rails.configuration.to_prepare do
Avromatic.prepare!(skip_clear: first_prepare)
first_prepare = false
Avromatic.prepare!
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/avromatic/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Avromatic
VERSION = '4.1.0'
VERSION = '4.1.1'
end
21 changes: 0 additions & 21 deletions spec/avromatic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@
described_class.nested_models.clear
end

context "at the end of configure" do
it "registers models" do
described_class.configure do |config|
config.eager_load_models = NestedRecord
end
expect(described_class.nested_models.registered?('test.nested_record')).to be(true)
end
end

describe "#prepare!" do
before do
stub_const('ValueModel', Avromatic::Model.model(schema_name: 'test.value'))
Expand All @@ -80,18 +71,6 @@
expect(Avromatic.schema_store).to have_received(:clear)
end

context "when skip_clear is true" do
it "does not clear the registry" do
described_class.prepare!(skip_clear: true)
expect(described_class.nested_models.registered?('test.value')).to be(true)
end

it "does not clear the schema store" do
described_class.prepare!(skip_clear: true)
expect(Avromatic.schema_store).not_to have_received(:clear)
end
end

it "registers models" do
described_class.eager_load_models = ['NestedRecord']
described_class.prepare!
Expand Down

0 comments on commit bb91294

Please sign in to comment.