Sha256: 710433febf3092335e42f4fd30d0886a18cc32accb3e56804e4b1ea29635d2a3

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 KB

Contents

require "test_helper"

class PipedreamTest < Minitest::Spec
  Song = Struct.new(:title)

  class Create < Trailblazer::Operation
    class MyContract < Reform::Form
      property :title
    end

    class Auth
      def initialize(user, model); @user, @model = user, model end
      def user_and_model?; @user == Module && @model.class == Song end
    end

    # design principles:
    # * include as less code as possible into the op class.
    # * make the flow super explicit without making it cryptic (only 3 new operators)
    # * avoid including DSL modules in favor of passing those configurations directly to the "step".



    self.|         Model[ Song, :new ]      # model!)
    self.| Policy::Guard[ ->(options){ options["current_user"] == ::Module } ]
    self.|      Contract[ MyContract]
    self.|        Policy[ Auth, :user_and_model?]
    self.<      Contract[ MyContract]

    # self.| :model
    # self.| :guard
    # self.| :contract


    # ok Model[Song, :new]      # model!)
    # ok Policy::Guard[ ->(options){ options["current_user"] == ::Module } ]
    # ok Contract[MyContract]
    # fail Contract[MyContract]
    # self.|> "contract"

    # | :bla
    # | ->

  end

  # TODO: test with contract constant (done).
  #       test with inline contract.
  #       test with override contract!.

  it do
    puts Create["pipetree"].inspect(style: :rows)
    result = Create.({}, { "current_user" => Module })

    result["model"].inspect.must_equal %{#<struct PipedreamTest::Song title=nil>}
    result["result.policy"].success?.must_equal true
    result["contract"].class.superclass.must_equal Reform::Form


  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
trailblazer-2.0.0 test/operation/pipedream_test.rb
trailblazer-2.0.0.rc1 test/operation/pipedream_test.rb
trailblazer-2.0.0.beta3 test/operation/pipedream_test.rb
trailblazer-2.0.0.beta2 test/operation/pipedream_test.rb
trailblazer-2.0.0.beta1 test/operation/pipedream_test.rb