munster 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -0
- data/lib/munster/controllers/receive_webhooks_controller.rb +3 -1
- data/lib/munster/models/received_webhook.rb +2 -2
- data/lib/munster/version.rb +1 -1
- metadata +16 -3
- data/lib/munster/state_machine_enum.rb +0 -125
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a01d139e5111555bba17ada9659ffcf0a42bbb9fc119ece2242e83ac12571f09
|
4
|
+
data.tar.gz: cc5892ac2ac534aa2112192a4c4e937a81d6d697bd87711b795d14a6aa19032f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6abb1b3e7b2ca12fcbbdfcebf4cb4d806554a27e8faaac2166e78cfaf0c49ae5441934079d737457723c169a5c2c842f5c882cb67e38ae03e67952fbb782885f
|
7
|
+
data.tar.gz: f8640e4e671d9b5cbb2759ae1130b71f702651cdc7c4f85002a775777ef937d947b261a04000a9efa6c300af1091cbcaec311fa6d94acaf26b055cf8f978d3de
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,26 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
This format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
5
|
+
|
6
|
+
## 0.3.0
|
7
|
+
|
8
|
+
### Changed
|
9
|
+
- state_machine_enum library was moved in it's own library/gem.
|
10
|
+
|
11
|
+
### Fixed
|
12
|
+
- Provide handled: true attribute for Rails.error.report method, because it is required in Rails 7.0.
|
13
|
+
|
1
14
|
## 0.2.0
|
2
15
|
|
16
|
+
### Changed
|
17
|
+
|
3
18
|
- Handler methods are now defined as instance methods for simplicity.
|
4
19
|
- Define service_id in initializer with active_handlers, instead of handler class.
|
5
20
|
- Use ruby 3.0 as a base for standard/rubocop, format all code according to it.
|
21
|
+
|
22
|
+
### Added
|
23
|
+
|
6
24
|
- Introduce Rails common error reporter ( https://guides.rubyonrails.org/error_reporting.html )
|
7
25
|
|
8
26
|
## 0.1.0
|
@@ -20,7 +20,9 @@ module Munster
|
|
20
20
|
render_error("Required parameters were not present in the request", :not_found)
|
21
21
|
rescue => e
|
22
22
|
Rails.error.set_context(**Munster.configuration.error_context)
|
23
|
-
Rails.error.report
|
23
|
+
# Rails 7.1 only requires `error` attribute for .report method, but Rails 7.0 requires `handled:` attribute additionally.
|
24
|
+
# We're setting `handled:` and `severity:` attributes to maintain compatibility with all versions of > rails 7.
|
25
|
+
Rails.error.report(e, handled: true, severity: :error)
|
24
26
|
|
25
27
|
if handler&.expose_errors_to_sender?
|
26
28
|
error_for_sender_from_exception(e)
|
@@ -1,13 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require 'state_machine_enum'
|
4
4
|
|
5
5
|
module Munster
|
6
6
|
class ReceivedWebhook < ActiveRecord::Base
|
7
7
|
self.implicit_order_column = "created_at"
|
8
8
|
self.table_name = "received_webhooks"
|
9
9
|
|
10
|
-
include
|
10
|
+
include StateMachineEnum
|
11
11
|
|
12
12
|
state_machine_enum :status do |s|
|
13
13
|
s.permit_transition(:received, :processing)
|
data/lib/munster/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: munster
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stanislav Katkov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06-
|
11
|
+
date: 2024-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '7.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: state_machine_enum
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -164,7 +178,6 @@ files:
|
|
164
178
|
- lib/munster/install_generator.rb
|
165
179
|
- lib/munster/jobs/processing_job.rb
|
166
180
|
- lib/munster/models/received_webhook.rb
|
167
|
-
- lib/munster/state_machine_enum.rb
|
168
181
|
- lib/munster/templates/create_munster_tables.rb.erb
|
169
182
|
- lib/munster/templates/munster.rb
|
170
183
|
- lib/munster/version.rb
|
@@ -1,125 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# This concern adds a method called "state_enum" useful for defining an enum using
|
4
|
-
# string values along with valid state transitions. Validations will be added for the
|
5
|
-
# state transitions and a proper enum is going to be defined. For example:
|
6
|
-
#
|
7
|
-
# state_machine_enum :state do |states|
|
8
|
-
# states.permit_transition(:created, :approved_pending_settlement)
|
9
|
-
# states.permit_transition(:approved_pending_settlement, :rejected)
|
10
|
-
# states.permit_transition(:created, :rejected)
|
11
|
-
# states.permit_transition(:approved_pending_settlement, :settled)
|
12
|
-
# end
|
13
|
-
module Munster
|
14
|
-
module StateMachineEnum
|
15
|
-
extend ActiveSupport::Concern
|
16
|
-
|
17
|
-
class StatesCollector
|
18
|
-
attr_reader :states
|
19
|
-
attr_reader :after_commit_hooks
|
20
|
-
attr_reader :common_after_commit_hooks
|
21
|
-
attr_reader :after_attribute_write_hooks
|
22
|
-
attr_reader :common_after_write_hooks
|
23
|
-
|
24
|
-
def initialize
|
25
|
-
@transitions = Set.new
|
26
|
-
@states = Set.new
|
27
|
-
@after_commit_hooks = {}
|
28
|
-
@common_after_commit_hooks = []
|
29
|
-
@after_attribute_write_hooks = {}
|
30
|
-
@common_after_write_hooks = []
|
31
|
-
end
|
32
|
-
|
33
|
-
def permit_transition(from, to)
|
34
|
-
@states << from.to_s << to.to_s
|
35
|
-
@transitions << [from.to_s, to.to_s]
|
36
|
-
end
|
37
|
-
|
38
|
-
def may_transition?(from, to)
|
39
|
-
@transitions.include?([from.to_s, to.to_s])
|
40
|
-
end
|
41
|
-
|
42
|
-
def after_inline_transition_to(target_state, &blk)
|
43
|
-
@after_attribute_write_hooks[target_state.to_s] ||= []
|
44
|
-
@after_attribute_write_hooks[target_state.to_s] << blk.to_proc
|
45
|
-
end
|
46
|
-
|
47
|
-
def after_committed_transition_to(target_state, &blk)
|
48
|
-
@after_commit_hooks[target_state.to_s] ||= []
|
49
|
-
@after_commit_hooks[target_state.to_s] << blk.to_proc
|
50
|
-
end
|
51
|
-
|
52
|
-
def after_any_committed_transition(&blk)
|
53
|
-
@common_after_commit_hooks << blk.to_proc
|
54
|
-
end
|
55
|
-
|
56
|
-
def validate(model, attribute_name)
|
57
|
-
return unless model.persisted?
|
58
|
-
|
59
|
-
was = model.attribute_was(attribute_name)
|
60
|
-
is = model[attribute_name]
|
61
|
-
|
62
|
-
unless was == is || @transitions.include?([was, is])
|
63
|
-
model.errors.add(attribute_name, "Invalid transition from #{was} to #{is}")
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
class InvalidState < StandardError
|
69
|
-
end
|
70
|
-
|
71
|
-
class_methods do
|
72
|
-
def state_machine_enum(attribute_name, **options_for_enum)
|
73
|
-
# Collect the states
|
74
|
-
collector = StatesCollector.new
|
75
|
-
yield(collector).tap do
|
76
|
-
# Define the enum using labels, with string values
|
77
|
-
enum_map = collector.states.map(&:to_sym).zip(collector.states.to_a).to_h
|
78
|
-
enum(attribute_name, enum_map, **options_for_enum)
|
79
|
-
|
80
|
-
# Define validations for transitions
|
81
|
-
validates attribute_name, presence: true
|
82
|
-
validate { |model| collector.validate(model, attribute_name) }
|
83
|
-
|
84
|
-
# Define inline hooks
|
85
|
-
before_save do |model|
|
86
|
-
_value_was, value_has_become = model.changes[attribute_name]
|
87
|
-
next unless value_has_become
|
88
|
-
hook_procs = collector.after_attribute_write_hooks[value_has_become].to_a + collector.common_after_write_hooks.to_a
|
89
|
-
hook_procs.each do |hook_proc|
|
90
|
-
hook_proc.call(model)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
# Define after commit hooks
|
95
|
-
after_commit do |model|
|
96
|
-
_value_was, value_has_become = model.previous_changes[attribute_name]
|
97
|
-
next unless value_has_become
|
98
|
-
hook_procs = collector.after_commit_hooks[value_has_become].to_a + collector.common_after_commit_hooks.to_a
|
99
|
-
hook_procs.each do |hook_proc|
|
100
|
-
hook_proc.call(model)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
# Define the check methods
|
105
|
-
define_method(:"ensure_#{attribute_name}_one_of!") do |*allowed_states|
|
106
|
-
val = self[attribute_name]
|
107
|
-
return if Set.new(allowed_states.map(&:to_s)).include?(val)
|
108
|
-
raise InvalidState, "#{attribute_name} must be one of #{allowed_states.inspect} but was #{val.inspect}"
|
109
|
-
end
|
110
|
-
|
111
|
-
define_method(:"ensure_#{attribute_name}_may_transition_to!") do |next_state|
|
112
|
-
val = self[attribute_name]
|
113
|
-
raise InvalidState, "#{attribute_name} already is #{val.inspect}" if next_state.to_s == val
|
114
|
-
end
|
115
|
-
|
116
|
-
define_method(:"#{attribute_name}_may_transition_to?") do |next_state|
|
117
|
-
val = self[attribute_name]
|
118
|
-
return false if val == next_state.to_s
|
119
|
-
collector.may_transition?(val, next_state)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|