2

我的Trailblazer-Rails应用程序遇到了加载问题。我可以使用 解决加载问题require,但是,根据自述文件,我似乎不需要使用 `require trailblazer-loader.

我的应用程序具有这样的文件结构

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

我的理解是app > concepts > email_address > schema > create.rb应该在之前加载app > concepts > email_address > person > operation > test.rb,基于嵌套级别和operations最后加载的事实。

到目前为止,email_address > schema > create.rb从方法中引用email_address > person > operation > test.rb并没有引起任何问题。

然而,我决定干掉我的代码,并添加一个step Macro哪些引用email_address > schema > create.rb,这会导致自动加载问题(特别是,app/concepts/email_address/schema/create.rb:2:in '<class:EmailAddress>': Schema is not a class (TypeError))。

我可以通过使用require指定加载或移动email_address > person > operation > test.rb内部来修复此错误email_address > schema > new_folder > test.rb(在这种情况下加载正常工作而无需要求)。

关于为什么我使用 astep Macro会改变需要加载东西的顺序的任何想法?我试图了解发生了什么。

以下是相关代码:

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

上面的代码会导致问题。下面,我EmailAddress::Schema::Create在其中Test而不是在 a中引用step Macro,没有任何加载问题:

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

我不知道为什么 astep Macro会更改我的应用程序所需的加载顺序。任何建议都非常感谢!

4

0 回答 0