Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove runtime dependency of trailblazer-operation #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/trailblazer/macro.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require "forwardable"
require "trailblazer/activity/dsl/linear"
require "trailblazer/operation" # TODO: remove this dependency

require "trailblazer/macro/strategy"
require "trailblazer/macro/model"
Expand Down
2 changes: 1 addition & 1 deletion lib/trailblazer/macro/guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.build(callable)
option = Trailblazer::Option(callable)

->((ctx, *), **circuit_args) do
Trailblazer::Operation::Result.new(!!option.call(ctx, keyword_arguments: ctx.to_hash, **circuit_args), {})
Policy::Result.new(result: !!option.call(ctx, keyword_arguments: ctx.to_hash, **circuit_args))
end
end
end
Expand Down
26 changes: 25 additions & 1 deletion lib/trailblazer/macro/policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,36 @@ def call((ctx, flow_options), **circuit_options)
ctx[:"result.policy.#{@name}"] = result

# flow control
signal = result.success? ? Trailblazer::Activity::Right : Trailblazer::Activity::Left
signal = result[:result] ? Trailblazer::Activity::Right : Trailblazer::Activity::Left

return signal, [ctx, flow_options]
end
end

class Result < Hash
def initialize(result:, data: nil)
self[:result] = result

data.each { |k, v| self[k] = v } if data
end

def success?
Trailblazer::Activity::Deprecate.warn caller_locations[0],
"The `success?` method is deprecated and will be removed in 3.0.0. " \
"Use `ctx[\"result.policy.\#{name}\"][:result]` instead."

self[:result]
end

def failure?
Trailblazer::Activity::Deprecate.warn caller_locations[0],
"The `failure?` method is deprecated and will be removed in 3.0.0. " \
"Use `!ctx[\"result.policy.\#{name}\"][:result]` instead."

!self[:result]
end
end

# Adds the `yield` result to the Railway and treats it like a
# policy-compatible object at runtime.
def self.step(condition, name: nil, &block)
Expand Down
2 changes: 1 addition & 1 deletion lib/trailblazer/macro/pundit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def result!(success, policy)
data = { policy: policy }
data[:message] = "Breach" if !success # TODO: how to allow messages here?

Trailblazer::Operation::Result.new(success, data)
Policy::Result.new(result: success, data: data)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/trailblazer/macro/rescue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def self.Rescue(*exceptions, handler: NoopHandler, &block)
# DISCUSS: should we deprecate this signature and rather apply the Task API here?
handler.call(exception, ctx, **circuit_options) # FIXME: when there's an error here, it shows the wrong exception!

[Operation::Railway.fail!, [ctx, flow_options]]
[Activity::Left, [ctx, flow_options]]
end
end

Expand Down
10 changes: 5 additions & 5 deletions test/docs/guard_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def process(options, **)
it { Create.(pass: true)[:x].must_equal true }

#- result object, guard
it { Create.(pass: true)[:"result.policy.default"].success?.must_equal true }
it { Create.(pass: false)[:"result.policy.default"].success?.must_equal false }
it { Create.(pass: true)[:"result.policy.default"][:result].must_equal true }
it { Create.(pass: false)[:"result.policy.default"][:result].must_equal false }

#---
#- Guard inheritance
Expand Down Expand Up @@ -97,13 +97,13 @@ class Create < Trailblazer::Operation
end
#:name end

it { Create.(:current_user => nil )[:"result.policy.user"].success?.must_equal false }
it { Create.(:current_user => Module)[:"result.policy.user"].success?.must_equal true }
it { Create.(:current_user => nil )[:"result.policy.user"][:result].must_equal false }
it { Create.(:current_user => Module)[:"result.policy.user"][:result].must_equal true }

it {
#:name-result
result = Create.(:current_user => true)
result[:"result.policy.user"].success? #=> true
result[:"result.policy.user"][:result] #=> true
#:name-result end
}
end
Expand Down
33 changes: 29 additions & 4 deletions test/docs/pundit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class Create < Trailblazer::Operation
it do
#:pundit-result
result = Create.(params: {}, current_user: Module)
result[:"result.policy.default"].success? #=> true
result[:"result.policy.default"][:result] #=> true
result[:"result.policy.default"][:policy] #=> #<MyPolicy ...>
#:pundit-result end
result[:"result.policy.default"].success?.must_equal true
result[:"result.policy.default"][:result].must_equal true
result[:"result.policy.default"][:policy].is_a?(MyPolicy).must_equal true
end

Expand Down Expand Up @@ -104,9 +104,9 @@ class Create < Trailblazer::Operation
it {
#:name-call
result = Create.(params: {}, current_user: Module)
result[:"result.policy.after_model"].success? #=> true
result[:"result.policy.after_model"][:result] #=> true
#:name-call end
result[:"result.policy.after_model"].success?.must_equal true }
result[:"result.policy.after_model"][:result].must_equal true }
end

#---
Expand All @@ -131,3 +131,28 @@ class Create < Trailblazer::Operation

# TODO:
#policy.default

class PunditWithDepreciationsTest < Minitest::Spec
Song = Struct.new(:id)

class Create < Trailblazer::Operation
step Model( Song, :new )
step Policy::Pundit( MyPolicy, :create? )
end

it "warns about the `success?` deprecation" do
warning = /The `success\?` method is deprecated and will be removed in 3.0.0. Use `ctx\["result.policy.\#{name}"\]\[:result\]` instead./

assert_output("", warning) do
Create.(params: {}, current_user: Module)[:"result.policy.default"].success?
end
end

it "warns about the `failure?` deprecation" do
warning = /The `failure\?` method is deprecated and will be removed in 3.0.0. Use `!ctx\["result.policy.\#{name}"\]\[:result\]` instead./

assert_output("", warning) do
Create.(params: {}, current_user: Module)[:"result.policy.default"].failure?
end
end
end
8 changes: 4 additions & 4 deletions test/operation/pundit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def process(options, **)
result = Create.(params: {}, current_user: Module)
result[:process].must_equal true
#- result object, policy
result[:"result.policy.default"].success?.must_equal true
result[:"result.policy.default"][:result].must_equal true
result[:"result.policy.default"][:message].must_be_nil
# result[:valid].must_be_nil
result[:"policy.default"].inspect.must_equal %{<Auth: user:Module, model:nil>}
Expand All @@ -39,7 +39,7 @@ def process(options, **)
result = Create.(params: {}, current_user: nil)
result[:process].must_be_nil
#- result object, policy
result[:"result.policy.default"].success?.must_equal false
result[:"result.policy.default"][:result].must_equal false
result[:"result.policy.default"][:message].must_equal "Breach"
end
# inject different policy.Condition it { Create.(params: {}, current_user: Object, "policy.default.eval" => Trailblazer::Operation::Policy::Pundit::Condition.new(Auth, :user_object?))["process"].must_equal true }
Expand Down Expand Up @@ -88,7 +88,7 @@ def process(options, **)
result = Edit.(params: { id: 1 }, current_user: Module)
result[:process].must_equal true
result[:model].inspect.must_equal %{#<struct PolicyTest::Song id=1>}
result[:"result.policy.default"].success?.must_equal true
result[:"result.policy.default"][:result].must_equal true
result[:"result.policy.default"][:message].must_be_nil
# result[:valid].must_be_nil
result[:"policy.default"].inspect.must_equal %{<Auth: user:Module, model:#<struct PolicyTest::Song id=1>>}
Expand All @@ -99,7 +99,7 @@ def process(options, **)
result = Edit.(params: { id: 4 }, current_user: nil)
result[:model].inspect.must_equal %{#<struct PolicyTest::Song id=4>}
result[:process].must_be_nil
result[:"result.policy.default"].success?.must_equal false
result[:"result.policy.default"][:result].must_equal false
result[:"result.policy.default"][:message].must_equal "Breach"
end
end
3 changes: 2 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require "minitest/autorun"

require "trailblazer/macro"
require "trailblazer/developer"
require "trailblazer/operation"
require "trailblazer/activity/testing"
require "trailblazer/macro"

T = Trailblazer::Activity::Testing

Expand Down
2 changes: 1 addition & 1 deletion trailblazer-macro.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "minitest"
spec.add_development_dependency "rake"
spec.add_development_dependency "trailblazer-developer"
spec.add_dependency "trailblazer-operation", ">= 0.10.0" # TODO: this dependency will be removed. currently needed for tests and for Guard::Result
spec.add_development_dependency "trailblazer-operation"

spec.add_dependency "trailblazer-activity-dsl-linear", ">= 1.2.0", "< 1.3.0"

Expand Down