Class: Apartment::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/apartment/railtie.rb

Class Method Summary collapse

Class Method Details

.deactivate_pool_reaper_in_test_env!Object

In test environments the reaper is more liability than asset: suites are short, tenant counts low, memory pressure absent, and an eviction mid-example orphans transactional-fixture state. Stop the reaper that Apartment.configure started; a suite that genuinely needs eviction can call Apartment.pool_reaper.start explicitly. Emits :reaper_stopped so an upgrading adopter notices the behavior change without combing release notes.



174
175
176
177
178
179
180
# File 'lib/apartment/railtie.rb', line 174

def self.deactivate_pool_reaper_in_test_env!
  return unless Rails.env.test?
  return unless Apartment.pool_reaper

  Apartment.pool_reaper.stop
  Apartment::Instrumentation.instrument(:reaper_stopped, reason: :test_env)
end

.header_trust_warning?(elevator_class, opts) ⇒ Boolean

Whether the Header elevator trust warning should fire. Class method for testability.

Returns:

  • (Boolean)


158
159
160
161
162
163
164
165
# File 'lib/apartment/railtie.rb', line 158

def self.header_trust_warning?(elevator_class, opts)
  # Module#<= returns nil (not false) for unrelated classes, so a bare
  # `&&` would let unrelated elevators slip through as nil — coerce via
  # an explicit guard so non-Header elevators report false.
  return false unless elevator_class <= Apartment::Elevators::Header

  !opts[:trusted]
end

.insert_elevator_middleware(middleware_stack, elevator_class) ⇒ Object

Insert elevator middleware after ActionDispatch::Callbacks. In the full stack this places it just before Cookies/Session/Auth. In API mode (where Cookies is absent), Callbacks is still present. Class method for testability.



153
154
155
# File 'lib/apartment/railtie.rb', line 153

def self.insert_elevator_middleware(middleware_stack, elevator_class, **)
  middleware_stack.insert_after(ActionDispatch::Callbacks, elevator_class, **)
end

.resolve_elevator_class(elevator) ⇒ Object

Resolve an elevator symbol/string to its class. Class method for testability. Accepts a Class directly (pass-through) or a symbol/string for lookup.



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/apartment/railtie.rb', line 184

def self.resolve_elevator_class(elevator)
  return elevator if elevator.is_a?(Class)

  class_name = "Apartment::Elevators::#{elevator.to_s.camelize}"
  require("apartment/elevators/#{elevator}")
  class_name.constantize
rescue NameError, LoadError => e
  available = Dir[File.join(__dir__, 'elevators', '*.rb')]
    .filter_map { |f| File.basename(f, '.rb').then { |name| name unless name == 'generic' } }
  raise(Apartment::ConfigurationError,
        "Unknown elevator '#{elevator}': #{e.message}. " \
        "Available elevators: #{available.join(', ')}")
end