usa 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8275113ba717d4dcd16a6ab69e75c3d2a0ae317138d4da57d3b6ceb5e534408
4
- data.tar.gz: 4182c9028362c42327acd46f14cbad052c5074594820a7db581cf3aa9445561d
3
+ metadata.gz: 548ccad376f01003a206b1c4a9691fac0beba122f699b3215171aff74ad7bc6f
4
+ data.tar.gz: dc17540f0361f124ae91a6af8888c3af063cf290416e7abb9cc39e7a852548f2
5
5
  SHA512:
6
- metadata.gz: 14589ea7e3c01ec46a705f3777c38bc11a0670733b6fd8c2551499955a32e274516c37d93f8668c632ade9c2aca925ea556e08ef5ce9344126bc4b51e22a2768
7
- data.tar.gz: 6cecb88f8f456ad77f6a353ed1cafba16fb369ef46d4aa71b95af0e6e971dd9a1bc943042edab8d5e0e6496ec48c9d59274ad74cadff5d61773da31fba37e0ea
6
+ metadata.gz: fda77e35c1d90e41334f16203e7156ce1739b50a1fba3bab650ab894ce853c171d0054745736f77b1dea45afd6936b4886ed47ff33c7da5e0ad67e42adacf44f
7
+ data.tar.gz: 025b0c17a446ec5da6f19e6821e5c93e0ae3f9ecccc0d99bbc7198b6efd33df0e819bafb6a6e1f6f634e275cd7874ae7539c25b9a5660167d0ad04f2efd5719b
data/CHANGELOG.md CHANGED
@@ -7,6 +7,16 @@ For more information about changelogs, check [Keep a Changelog](http://keepachan
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## 0.4.2 - 2026-09-16
11
+
12
+ * [Fix] The check that refuses a host's own class where one of these models should be asks the
13
+ files rather than the constants, and loads nothing to answer. 0.4.1 moved it onto
14
+ `ActiveSupport.on_load(:active_record)`, which fires long before an app is ready: a host whose
15
+ own load hook declares `belongs_to :market, counter_cache: true` had that class name resolved
16
+ on the spot, which reached `ApplicationRecord` before the database was configured and stopped
17
+ the boot. Zeitwerk is asked which directory holds a `city.rb` instead, which answers the same
18
+ question without loading a model or Active Record behind it
19
+
10
20
  ## 0.4.1 - 2026-09-16
11
21
 
12
22
  * [Fix] The check that refuses a host's own class where one of these models should be is asked
data/README.md CHANGED
@@ -12,7 +12,7 @@ To install on your system, run
12
12
 
13
13
  To use inside a bundled Ruby project, add this line to the `Gemfile`:
14
14
 
15
- gem 'usa', '~> 0.4.1'
15
+ gem 'usa', '~> 0.4.2'
16
16
 
17
17
  Below 1.0 the pin stops at the next minor rather than the next major, because that is where a
18
18
  breaking change may still land. It becomes `~> 1.0` once the API is settled on purpose.
data/lib/usa/engine.rb CHANGED
@@ -4,11 +4,6 @@ require 'rails/engine'
4
4
  module USA
5
5
  # Teaches Rails where this gem's models live, and refuses a host that has taken their names.
6
6
  class Engine < ::Rails::Engine
7
- # Asked when Active Record loads rather than while the app boots: reaching for these
8
- # models pulls Active Record in behind them, and pulling it in before initialization is
9
- # over slows every boot and can reorder a host's own -- which is what Rails warns about.
10
- ActiveSupport.on_load(:active_record) do
11
- USA.verify_models ::State, ::County, ::City, ::CityCounty, ::ZIP
12
- end
7
+ config.to_prepare { USA.verify_models }
13
8
  end
14
9
  end
data/lib/usa/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module USA
2
2
  # The version of this gem, as RubyGems knows it.
3
- VERSION = '0.4.1'
3
+ VERSION = '0.4.2'
4
4
  end
data/lib/usa.rb CHANGED
@@ -34,18 +34,24 @@ module USA
34
34
  County.recount_zips
35
35
  end
36
36
 
37
+ # The file each of this gem's models is defined in, which is the name a host must not take.
38
+ MODELS = %w[city city_county county state zip]
39
+
37
40
  # Refuses a host's own class standing where one of this gem's models should be. Zeitwerk
38
41
  # gives an app's file precedence over an engine's, silently, so without this a host holding
39
42
  # `app/models/city.rb` would find every association of this gem's pointing at a class of its
40
- # own.
41
- # @param [Array<Class>] models the classes the names this gem takes answer to.
43
+ # own. Asked of the files rather than of the constants: loading a model to find out pulls
44
+ # Active Record in with it, long before an app is ready for either.
45
+ # @param [Array<String>] dirs the autoloaded directories to look through.
42
46
  # @return [void]
43
- def self.verify_models(*models)
44
- theirs = models.reject { |model| model < Record }
45
- return if theirs.empty?
46
-
47
- raise Error, "The usa gem defines #{theirs.to_sentence}, and a class of your own has " \
48
- 'taken the name. Rename yours: Rails gives an app’s file precedence over an engine’s, ' \
49
- 'silently, so the models this gem ships would point at a class it knows nothing about.'
47
+ def self.verify_models(dirs = Rails.autoloaders.main.dirs)
48
+ mine = Engine.root.join('app/models').to_s
49
+ taken = dirs.reject { |dir| dir == mine }.
50
+ flat_map { |dir| MODELS.select { |model| File.exist? File.join(dir, "#{model}.rb") } }
51
+ return if taken.empty?
52
+
53
+ raise Error, "The usa gem defines #{taken.uniq.map(&:camelize).to_sentence}, and a file " \
54
+ 'of your own takes the name first. Rename yours: Rails gives an app’s file precedence ' \
55
+ 'over an engine’s, silently, and the models this gem ships would point at your class.'
50
56
  end
51
57
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claudio Baccigalupo