rubytrailblazer

Trailblazer Operation undefined method `call' for nil:NilClass


I have a simple Trailblazer Operation as follow:

class User::Delete < Trailblazer::Operation
  extend Contract::DSL

  contract 'params' do
    property :token

    validates :token, presence: true
  end

  step    Contract::Validate(name: 'params'), before: 'operation.new'
  success :kill_zombie

  def kill_zombie(options, params:, **)
    options['killed'] = true
  end

end

When I run the operation under the rails console, that's the output:

irb(main):003:0> User::Delete.()
NoMethodError: undefined method `call' for nil:NilClass
    from (irb):3

If I remove the Contract line:

class User::Delete < Trailblazer::Operation
  extend Contract::DSL

  contract 'params' do
    property :token

    validates :token, presence: true
  end

  success :kill_zombie

  def kill_zombie(options, params:, **)
    options['killed'] = true
  end

end

It works as expected:

irb(main):005:0> User::Delete.()
=> <Result:true <Skill {"killed"=>true} {"params"=>{}} {"pipetree"=>[>operation.new,>kill_zombie], "contract.params.class"=>#<Class:0x0000000003e54e50>}> >
irb(main):006:0>

Any thoughts?


Solution

  • You need a Contract::Build before Contract::Validate when using a Reform contract. With a Dry schema, the build step isn't needed.