granite 1.1.0 → 1.2.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 +4 -4
- data/lib/granite/action/steps/inline_step_adapter.rb +27 -0
- data/lib/granite/action/steps/inline_step_definition.rb +23 -0
- data/lib/granite/action/steps/step_definition.rb +31 -0
- data/lib/granite/action/steps.rb +113 -0
- data/lib/granite/action/subject.rb +2 -2
- data/lib/granite/action.rb +4 -11
- data/lib/granite/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: aba08b5ac13d97ee80cad10e32e2099a7367a89ac7a93e9b9288f3a7bb41e11a
|
|
4
|
+
data.tar.gz: 1ecd0722b2a7560140cccbdb06583360f7537a46f996b188dc1a3ed4d9340982
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e27e82dd4dd6ba332792a01058fee3511f849eeaa708629c9bbf8a967323c96c751733f3a499ce8807814f09ccc4b3eec76485935a92fb9868df1b26faa57662
|
|
7
|
+
data.tar.gz: 6c3a899e08cf9b20342fe4accdf020ab7199674a522ae54a4ee79ddd66cdeb21852798ae995b3908ebc214b674e1184fa8b603d7e526a67c2f10fd392ef089d9
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module Granite
|
|
2
|
+
class Action
|
|
3
|
+
module Steps
|
|
4
|
+
# Adapts `inline_step` interface to match that of a step action as needed.
|
|
5
|
+
class InlineStepAdapter
|
|
6
|
+
def initialize(action, block)
|
|
7
|
+
@action = action
|
|
8
|
+
@block = block
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def perform!(**options)
|
|
12
|
+
@action.instance_exec(**options, &@block)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def assign_attributes(_attributes) end
|
|
16
|
+
|
|
17
|
+
def errors
|
|
18
|
+
{}
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def failed_preconditions
|
|
22
|
+
[]
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module Granite
|
|
2
|
+
class Action
|
|
3
|
+
module Steps
|
|
4
|
+
# Step definition for inline steps. Can be converted to step instance using {#to_step} method.
|
|
5
|
+
class InlineStepDefinition
|
|
6
|
+
attr_reader :options
|
|
7
|
+
|
|
8
|
+
def initialize(block, **options)
|
|
9
|
+
@block = block
|
|
10
|
+
@options = options
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def to_step(action, _attributes)
|
|
14
|
+
InlineStepAdapter.new(action, @block)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def perform_with
|
|
18
|
+
:perform!
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module Granite
|
|
2
|
+
class Action
|
|
3
|
+
module Steps
|
|
4
|
+
# Step definition for class steps. Can be converted to step instance using {#to_step} method.
|
|
5
|
+
class StepDefinition
|
|
6
|
+
attr_reader :options
|
|
7
|
+
|
|
8
|
+
def initialize(klass, required: true, attributes: {}, **options)
|
|
9
|
+
@klass = klass
|
|
10
|
+
@options = options
|
|
11
|
+
@required = required
|
|
12
|
+
@attribute_definitions = attributes
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def to_step(parent, attributes)
|
|
16
|
+
subject_association = @klass.reflect_on_association(:subject)
|
|
17
|
+
subject = parent.public_send(subject_association.name) if subject_association
|
|
18
|
+
@klass.with(parent.ctx).new(*subject, attributes.merge(evaluated_attributes(parent)))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def perform_with
|
|
22
|
+
@required ? :perform! : :try_perform!
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def evaluated_attributes(parent)
|
|
26
|
+
@attribute_definitions.to_h { |key, value| [key, parent.evaluate(value)] }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
require 'granite/action/steps/inline_step_adapter'
|
|
2
|
+
require 'granite/action/steps/inline_step_definition'
|
|
3
|
+
require 'granite/action/steps/step_definition'
|
|
4
|
+
|
|
5
|
+
module Granite
|
|
6
|
+
class Action
|
|
7
|
+
# Implements steps DSL for Granite actions & other steps.
|
|
8
|
+
module Steps
|
|
9
|
+
extend ActiveSupport::Concern
|
|
10
|
+
|
|
11
|
+
included do
|
|
12
|
+
class_attribute :step_definitions
|
|
13
|
+
self.step_definitions = {}
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Defines `step` and `inline_step` methods.
|
|
17
|
+
module ClassMethods
|
|
18
|
+
# Defines class based step in this action/step. If a block is passed it will be evaluated in the context of
|
|
19
|
+
# `step_class` to further customize the step.
|
|
20
|
+
# @param name [Symbol] name of the inline step, that should describe what this inline step performs.
|
|
21
|
+
# Can be used to retrieve step instance using `action.step_name`.
|
|
22
|
+
# @param step_class [Class<Granite::Action>] class that will be used to perform this step.
|
|
23
|
+
# @param required [Boolean, :no_nested_preconditions] if set to `true` step will define its preconditions &
|
|
24
|
+
# validations on parent action and will be called using `perform!`. If set to `:no_nested_preconditions`
|
|
25
|
+
# step will define its validations on parent action but not preconditions and will be called using
|
|
26
|
+
# `perform!`. If set to `false` step will not define its preconditions & validations on parent action and
|
|
27
|
+
# will be called using `try_perform!`.
|
|
28
|
+
# @param options [Hash] any further options will be passed to nested validation & to step definition.
|
|
29
|
+
# @option options [Boolean, Symbol, Proc] :if will only perform/validate step if condition is satisfied
|
|
30
|
+
# @option options [Boolean, Symbol, Proc] :unless will only perform/validate step unless condition is
|
|
31
|
+
# satisfied
|
|
32
|
+
# @option options [Hash{Symbol=>Boolean, Symbol, Proc}] :attributes additional attributes that will be
|
|
33
|
+
# evaluated and passed when initializing step
|
|
34
|
+
def step(name, step_class, required: true, **options, &block)
|
|
35
|
+
if block
|
|
36
|
+
step_class = Class.new(step_class, &block)
|
|
37
|
+
const_set(name.to_s.classify, step_class)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
add_step name, StepDefinition.new(step_class, **options, required:)
|
|
41
|
+
return unless required
|
|
42
|
+
|
|
43
|
+
validation_options = options.slice(:if, :unless)
|
|
44
|
+
precondition embedded: name, **validation_options unless required == :no_nested_preconditions
|
|
45
|
+
assign_data(**validation_options) { @steps[name].assign_data }
|
|
46
|
+
validates_nested name, **validation_options
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Defines inline step in this action/step. Any block passed to this method will be evaluated in the context
|
|
50
|
+
# of parent action/step when it's performed making it convenient method to define a block of code that needs
|
|
51
|
+
# to be executed when parent action/step is performed.
|
|
52
|
+
# @param name [Symbol] name of the inline step, that should describe what this inline step performs.
|
|
53
|
+
# @param options (Hash) any further options will be passed to step definition. These can be used to skip a
|
|
54
|
+
# step completely under certain conditions by passing `if` or `unless` named parameters.
|
|
55
|
+
def inline_step(name, **options, &block)
|
|
56
|
+
add_step name, InlineStepDefinition.new(block, **options)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def add_step(name, definition)
|
|
62
|
+
self.step_definitions = step_definitions.merge(name => definition)
|
|
63
|
+
define_method(name) { @steps[name] }
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def assign_attributes(attributes)
|
|
68
|
+
if @steps
|
|
69
|
+
@steps.each_value { |step| step.assign_attributes(attributes) }
|
|
70
|
+
else
|
|
71
|
+
@steps = {}
|
|
72
|
+
step_definitions.each { |identifier, definition| @steps[identifier] = definition.to_step(self, attributes) }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
super
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
def execute_perform!(*)
|
|
81
|
+
@steps.each do |name, step|
|
|
82
|
+
definition = step_definitions[name]
|
|
83
|
+
step.public_send(definition.perform_with) if conditions_satisfied?(**definition.options)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def emerge_represented_attributes_errors!
|
|
88
|
+
super
|
|
89
|
+
|
|
90
|
+
@steps.each do |step_name, step|
|
|
91
|
+
keys_for_errors(step.errors).each { |attribute| emerge_errors!(step_name, attribute) }
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def keys_for_errors(errors)
|
|
96
|
+
if errors.is_a?(ActiveModel::Errors)
|
|
97
|
+
errors.attribute_names
|
|
98
|
+
else
|
|
99
|
+
errors.keys
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def emerge_errors!(nested_name, attribute)
|
|
104
|
+
key = :"#{nested_name}.#{attribute}"
|
|
105
|
+
|
|
106
|
+
errors.where(key).each do |error|
|
|
107
|
+
errors.import(error, attribute:)
|
|
108
|
+
end
|
|
109
|
+
errors.delete(key)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -62,10 +62,10 @@ module Granite
|
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
def assign_subject(args, attributes, reflection)
|
|
65
|
+
attributes = attributes.merge(reflection.name => args.first) unless args.empty?
|
|
65
66
|
assign_attributes(attributes)
|
|
66
67
|
|
|
67
|
-
|
|
68
|
-
raise SubjectNotFoundError, self.class unless subject
|
|
68
|
+
raise SubjectNotFoundError, self.class if subject.nil?
|
|
69
69
|
rescue Granite::Form::AssociationTypeMismatch
|
|
70
70
|
raise SubjectTypeMismatchError.new(self.class, args.first.class.name, reflection.klass)
|
|
71
71
|
end
|
data/lib/granite/action.rb
CHANGED
|
@@ -11,6 +11,7 @@ require 'granite/action/precondition'
|
|
|
11
11
|
require 'granite/action/preconditions'
|
|
12
12
|
require 'granite/action/policies'
|
|
13
13
|
require 'granite/action/projectors'
|
|
14
|
+
require 'granite/action/steps'
|
|
14
15
|
require 'granite/action/subject'
|
|
15
16
|
require 'granite/action/translations'
|
|
16
17
|
|
|
@@ -73,17 +74,9 @@ module Granite
|
|
|
73
74
|
_run_initialize_callbacks
|
|
74
75
|
end
|
|
75
76
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
errors.messages.
|
|
79
|
-
(this + other).uniq
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
else
|
|
83
|
-
def merge_errors(other_errors)
|
|
84
|
-
other_errors.each do |error|
|
|
85
|
-
errors.import(error) unless errors.messages[error.attribute].include?(error.message)
|
|
86
|
-
end
|
|
77
|
+
def merge_errors(other_errors)
|
|
78
|
+
other_errors.each do |error|
|
|
79
|
+
errors.import(error) unless errors.messages[error.attribute].include?(error.message)
|
|
87
80
|
end
|
|
88
81
|
end
|
|
89
82
|
|
data/lib/granite/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: granite
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Toptal Engineering
|
|
@@ -321,6 +321,10 @@ files:
|
|
|
321
321
|
- lib/granite/action/preconditions/embedded_precondition.rb
|
|
322
322
|
- lib/granite/action/preconditions/object_precondition.rb
|
|
323
323
|
- lib/granite/action/projectors.rb
|
|
324
|
+
- lib/granite/action/steps.rb
|
|
325
|
+
- lib/granite/action/steps/inline_step_adapter.rb
|
|
326
|
+
- lib/granite/action/steps/inline_step_definition.rb
|
|
327
|
+
- lib/granite/action/steps/step_definition.rb
|
|
324
328
|
- lib/granite/action/subject.rb
|
|
325
329
|
- lib/granite/action/transaction.rb
|
|
326
330
|
- lib/granite/action/transaction_manager.rb
|
|
@@ -380,7 +384,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
380
384
|
- !ruby/object:Gem::Version
|
|
381
385
|
version: '0'
|
|
382
386
|
requirements: []
|
|
383
|
-
rubygems_version: 4.0.
|
|
387
|
+
rubygems_version: 4.0.20
|
|
384
388
|
specification_version: 4
|
|
385
389
|
summary: Another business actions architecture for Rails apps
|
|
386
390
|
test_files: []
|