2

I'm running into a loading issue with my Trailblazer-Rails app. I can solve the loading issue using require, but, based on trailblazer-loader's readme, it doesn't seem like I should need to use `require.

My app has a file structure like so

app
-|concepts
---|email_address
-----|person
-------|operation
---------|test.rb
-----|schema
-------|create.rb

My understanding is that app > concepts > email_address > schema > create.rb should be loaded before app > concepts > email_address > person > operation > test.rb, based on the nesting level and the fact that operations are loaded last.

So far, referencing email_address > schema > create.rb from within a method of email_address > person > operation > test.rb hasn't caused any issues.

I've decided to DRY up my code however, and add a step Macro which references email_address > schema > create.rb and that is causing autoloading problems (specifically, app/concepts/email_address/schema/create.rb:2:in '<class:EmailAddress>': Schema is not a class (TypeError)).

I can fix this error by either using require to specify loading, or by moving email_address > person > operation > test.rb inside email_address > schema > new_folder > test.rb (in which case loading works normally without needing a require).

Any ideas on why my use of a step Macro is changing the order things need to be loaded in? I'm trying to understand what's going on.

Here's the relevant code:

module Macro
  def self.ValidateParams(schema:)
    step = ->(input, options) { 
      unless input[:validate] == false
        options['contract.default'] = schema.(input['params'])
        options[:params] = options['contract.default'].output
        options['contract.default'].success? && input['current_user']
      else
        options[:params] = params
      end
    }

    [ step, name: "validate_params.#{schema.name}" ]
  end
end

class ApplicationSchema < Dry::Validation::Schema
  configure do |config|
    config.messages_file = './config/dry_messages.yml'
    config.type_specs = true
  end

  def self.schema
    Dry::Validation.Form(self)
  end

  def self.call(params)
    self.schema.call(params)
  end
end

class EmailAddress
  class Schema
    class Create < ApplicationSchema
      define! do
        required(:email_address, Types::Email).value(:str?)
        optional(:email_label, Types::String).maybe(:str?)
        optional(:email_primary, Types::Bool).value(:bool?)
      end
    end
  end
end


class Test < Trailblazer::Operation

  step Macro::ValidateParams(schema: EmailAddress::Schema::Create)
  step :do_everything!

  def do_everything!(options, params:, **)
    p "Successfully validated params! Score!"
  end
end

The code above causes problems. Below, where I reference EmailAddress::Schema::Create within Test but not within a step Macro, there aren't any loading problems:

class Test < Trailblazer::Operation

  step :do_everything!

  def do_everything!(options, params:, **)
    EmailAddress::Person::Schema::Create.(params) # <-- works fine
    p "Successfully validated params! Score!"
  end
end

I don't know of any reason why a step Macro would change the required loading order of my app. Any suggestions are greatly appreciated!!

John
  • 9,249
  • 5
  • 44
  • 76

0 Answers0