hubbado-trailblazer 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 73c78de9e6399b2943e694031b7cfd30b6c13776944b37221fbe89a47518aeb6
4
- data.tar.gz: 4fd46589064ce601305b990873d981554a50ee97b634b50ee97ca19478bf96f3
3
+ metadata.gz: f0d94f46fd55fd7b149ec18fb82e6923b8d828b4d9fbf4e23465bbfbd448688e
4
+ data.tar.gz: 5a63ac1ee253726b488acd45acbdaad37eb9af1fea81d9716f2089727aeae80f
5
5
  SHA512:
6
- metadata.gz: d728feb3ee223119e4f32ef9efb908c5965e89d712b85d3f8984234d4abde74a280cd2638da7e539f65f566955ee991a38f786ceae1b855b9093a560e0ae1dc1
7
- data.tar.gz: d89402e82251ce04726a280ebfbca988c295608ac48aba5125fd390d3bcc45dc6ef995119ea0756dcd580e7e3f987cf07f4196a250a88b831d13e89e6540e0e4
6
+ metadata.gz: 7fb80170ecf81108173c6fcca1cf4271eb7e927f9bbc179cf2e66dc7e6f1e57b9681ecd99658af9184494330b46044d2c1cb60ab866e2c5c6efc8b08badc471f
7
+ data.tar.gz: afd335df6d8698d1fc89e722ba7fac1415e06b46f1722cb3796e6546d444005c98df54fb5cdbb9881ac5bd8703c8dc01d65898d7e834a68382b5be9188458bc2
data/CHANGELOG.md CHANGED
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.4.0] - 2025-12-09
9
+
10
+ ### Added
11
+
12
+ - `policy_unauthorized_message` template method to customize the policy unauthorized exception message
13
+
14
+ ## [1.3.0] - 2025-11-26
15
+
16
+ ### Added
17
+
18
+ - Support for `result.not_found { ... }` block to handle operations with `:not_found` terminus.
19
+
8
20
  ## [1.2.0] - 2025-07-19
9
21
 
10
22
  ### Added
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "hubbado-trailblazer"
3
- s.version = "1.2.0"
3
+ s.version = "1.4.0"
4
4
  s.summary = "Enhanced Trailblazer operation utilities for Ruby applications with improved error handling, operation execution patterns, and ActiveRecord integration."
5
5
 
6
6
  s.authors = ["Hubbado Devs"]
@@ -16,6 +16,10 @@ module Hubbado
16
16
  # result.raise_policy_failed
17
17
  # end
18
18
  #
19
+ # result.not_found do |ctx|
20
+ # ...
21
+ # end
22
+ #
19
23
  # result.validation_failed do |ctx|
20
24
  # ...
21
25
  # end
@@ -34,6 +38,9 @@ module Hubbado
34
38
  # If there is a policy failure and you have not implemented
35
39
  # `result.policy_failed` then an exception will be raised.
36
40
  #
41
+ # If the operation has not_found terminus and you have not
42
+ # implemented `result.not_found` then ActiveRecord::RecordNotFound will be raised.
43
+ #
37
44
  # If the operation fails (due to non-policy error) and you have not
38
45
  # implemented `result.otherwise` then an exception will be raised.
39
46
  #
@@ -67,6 +74,10 @@ module Hubbado
67
74
  ctx
68
75
  end
69
76
 
77
+ template_method :policy_unauthorized_message do |ctx, operation_name:|
78
+ "User #{ctx[:current_user]&.id} not allowed to run #{operation_name}"
79
+ end
80
+
70
81
  def _run_runtime_options(ctx = {}, *dependencies)
71
82
  [_run_options(ctx), *dependencies]
72
83
  end
@@ -81,13 +92,23 @@ module Hubbado
81
92
 
82
93
  ctx = operation.send(operation_method, operation_arguments)
83
94
 
84
- result = Result.new(operation, ctx, trace_operation)
95
+ result = Result.new(
96
+ operation,
97
+ ctx,
98
+ trace_operation: trace_operation,
99
+ policy_unauthorized_message: policy_unauthorized_message(
100
+ ctx,
101
+ operation_name: operation.name
102
+ )
103
+ )
85
104
 
86
105
  yield(result) if block_given?
87
106
 
88
107
  if ctx['result.policy.default']&.failure?
89
108
  result.raise_policy_failed unless result.policy_failed_executed?
90
- elsif ctx.failure? && !result.validation_failed_executed? && !result.otherwise_executed?
109
+ elsif !result.not_found_executed? && ctx.terminus.to_h[:semantic] == :not_found
110
+ result.raise_not_found
111
+ elsif ctx.failure? && !result.validation_failed_executed? && !result.not_found_executed? && !result.otherwise_executed?
91
112
  result.raise_operation_failed
92
113
  end
93
114
 
@@ -99,11 +120,13 @@ module Hubbado
99
120
 
100
121
  attr_reader :returned
101
122
  attr_reader :trace_operation
123
+ attr_reader :policy_unauthorized_message
102
124
 
103
- def initialize(operation, ctx, trace_operation)
125
+ def initialize(operation, ctx, trace_operation:, policy_unauthorized_message:)
104
126
  @operation = operation
105
127
  @ctx = ctx
106
128
  @trace_operation = trace_operation
129
+ @policy_unauthorized_message = policy_unauthorized_message
107
130
  end
108
131
 
109
132
  def log_level
@@ -159,6 +182,16 @@ module Hubbado
159
182
  @returned
160
183
  end
161
184
 
185
+ def not_found
186
+ return unless ctx.terminus.to_h[:semantic] == :not_found
187
+
188
+ @not_found_executed = true
189
+ @returned = yield(ctx)
190
+
191
+ logger.send(log_level, "Not found block executed for operation #{operation}")
192
+ @returned
193
+ end
194
+
162
195
  def otherwise
163
196
  return if executed?
164
197
 
@@ -169,6 +202,12 @@ module Hubbado
169
202
  @returned
170
203
  end
171
204
 
205
+ def raise_not_found
206
+ msg = "Record for operation #{operation.name} not found"
207
+
208
+ raise ActiveRecord::RecordNotFound, msg
209
+ end
210
+
172
211
  def raise_operation_failed
173
212
  msg = "Operation #{operation.name} failed"
174
213
 
@@ -182,13 +221,10 @@ module Hubbado
182
221
  end
183
222
 
184
223
  def raise_policy_failed
185
- current_user = ctx[:current_user]
186
- true_user = ctx[:true_user]
187
-
188
- msg = "User #{current_user&.id}/#{true_user&.id} (#{current_user&.roles&.join ', '}) " \
189
- "not allowed to run #{operation.name}"
190
-
191
- raise Hubbado::Trailblazer::Errors::Unauthorized.new(msg, ctx['result.policy.default'][:policy_result])
224
+ raise Hubbado::Trailblazer::Errors::Unauthorized.new(
225
+ policy_unauthorized_message,
226
+ ctx['result.policy.default'][:policy_result]
227
+ )
192
228
  end
193
229
 
194
230
  def success_executed?
@@ -203,6 +239,10 @@ module Hubbado
203
239
  !!@validation_failed_executed
204
240
  end
205
241
 
242
+ def not_found_executed?
243
+ !!@not_found_executed
244
+ end
245
+
206
246
  def otherwise_executed?
207
247
  !!@otherwise_executed
208
248
  end
@@ -211,6 +251,7 @@ module Hubbado
211
251
  success_executed? ||
212
252
  policy_failed_executed? ||
213
253
  validation_failed_executed? ||
254
+ not_found_executed? ||
214
255
  otherwise_executed?
215
256
  end
216
257
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hubbado-trailblazer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hubbado Devs
@@ -217,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
217
217
  - !ruby/object:Gem::Version
218
218
  version: '0'
219
219
  requirements: []
220
- rubygems_version: 3.6.9
220
+ rubygems_version: 4.0.1
221
221
  specification_version: 4
222
222
  summary: Enhanced Trailblazer operation utilities for Ruby applications with improved
223
223
  error handling, operation execution patterns, and ActiveRecord integration.