Sha256: bf61ae296d8dcd55c11b68bf530c722f0d0dd05a076fc5612ba341b983925677

Contents?: true

Size: 548 Bytes

Versions: 2

Compression:

Stored size: 548 Bytes

Contents

require "dry-initializer"
require "rails"

# Define a dispatcher for `:model` and `:find_by` options
rails_dispatcher = lambda do |model: nil, find_by: :id, **options|
  return options unless model

  model = eval(model) if model.is_a? String
  klass = model.is_a?(ActiveRecord::Relation) ? model.klass : model

  coercer = lambda do |value|
    return value if value.nil? || value.is_a?(klass)
    model.find_by! find_by => value
  end

  options.merge(type: coercer)
end

# Register a dispatcher
Dry::Initializer::Dispatchers << rails_dispatcher

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-initializer-rails-3.1.1 lib/dry/initializer/rails.rb
dry-initializer-rails-3.1.0 lib/dry/initializer/rails.rb