Module: Errgonomic::Rails

Defined in:
lib/errgonomic/rails.rb,
lib/errgonomic/rails/active_record_optional.rb,
lib/errgonomic/rails/active_record_delegate_optional.rb

Overview

Slightly more convenient access to the setup functions: Errgonomic::Rails.setup_before and Errgonomic::Rails.setup_after

Defined Under Namespace

Modules: ActiveRecordDelegateOptional, ActiveRecordOptional

Class Method Summary collapse

Class Method Details

.setup_afterObject

Wrapping optional associations requires that we include the module after the class is first evaluated, so that it can define its associations for later reflection.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/errgonomic/rails.rb', line 19

def self.setup_after
  Zeitwerk::Loader.eager_load_all
  setupable = defined?(ApplicationRecord) ? ApplicationRecord : ActiveRecord::Base
  setupable.descendants.each do |model|
    next unless begin
      model.table_exists?
    rescue StandardError
      false
    end

    model.include Errgonomic::Rails::ActiveRecordOptional
  end
end

.setup_beforeObject

We provide helper class methods, like ‘delegate_optional`, which need to be included into ActiveRecord::Base before any models are evaluated.



12
13
14
# File 'lib/errgonomic/rails.rb', line 12

def self.setup_before
  ActiveRecord::Base.include(Errgonomic::Rails::ActiveRecordDelegateOptional)
end