granite 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b350a3ec5e67ddb8963e53fd5662b907281bd99ae80bf2c56d1e38bc5aab1e91
4
- data.tar.gz: e50e8205ed1c15b743c19162a707aaeb9e22aa56956b7cfc468c9c2ddaba9211
3
+ metadata.gz: aba08b5ac13d97ee80cad10e32e2099a7367a89ac7a93e9b9288f3a7bb41e11a
4
+ data.tar.gz: 1ecd0722b2a7560140cccbdb06583360f7537a46f996b188dc1a3ed4d9340982
5
5
  SHA512:
6
- metadata.gz: e0bd294852f36a6e3995cc8bd2637093be985032d32645350f04fdad67c6897dafde07770ec53f720af43bb7b6470fe59643a3ba4784d97a829e35c8bc609fc7
7
- data.tar.gz: 15c8c6b16d6514fd6b3699e99615e06b1f2ee523cd978bb3725891c969a991f81e72fba821d5e52064a4afe48c4875fd4aa4c77a5e499eb03f69b103a678b874
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
- self.subject = args.first unless args.empty?
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
@@ -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
- if ActiveModel.version < Gem::Version.new('6.1.0')
77
- def merge_errors(other_errors)
78
- errors.messages.deep_merge!(other_errors.messages) do |_, this, other|
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
 
@@ -1,3 +1,3 @@
1
1
  module Granite
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.2.0'.freeze
3
3
  end
@@ -0,0 +1,32 @@
1
+ require 'lint_roller'
2
+ require 'granite/version'
3
+
4
+ module RuboCop
5
+ module Granite
6
+ # Plugin integration for RuboCop's plugin system (1.72+).
7
+ class Plugin < LintRoller::Plugin
8
+ def about
9
+ LintRoller::About.new(
10
+ name: 'rubocop-granite',
11
+ version: ::Granite::VERSION,
12
+ homepage: 'https://github.com/toptal/granite',
13
+ description: 'Custom RuboCop configuration for Granite'
14
+ )
15
+ end
16
+
17
+ def supported?(context)
18
+ context.engine == :rubocop
19
+ end
20
+
21
+ def rules(_context)
22
+ project_root = Pathname.new(__dir__).parent.parent.parent.expand_path
23
+
24
+ LintRoller::Rules.new(
25
+ type: :path,
26
+ config_format: :rubocop,
27
+ value: project_root.join('config', 'rubocop-default.yml').to_s
28
+ )
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,9 +1,4 @@
1
1
  module RuboCop
2
2
  module Granite # :nodoc:
3
- PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
4
- CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'rubocop-default.yml').freeze
5
- CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
6
-
7
- private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
8
3
  end
9
4
  end
@@ -1,6 +1,4 @@
1
1
  require 'rubocop'
2
2
 
3
3
  require_relative 'rubocop/granite'
4
- require_relative 'rubocop/granite/inject'
5
-
6
- RuboCop::Granite::Inject.defaults!
4
+ require_relative 'rubocop/granite/plugin'
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.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toptal Engineering
@@ -225,14 +225,14 @@ dependencies:
225
225
  requirements:
226
226
  - - ">="
227
227
  - !ruby/object:Gem::Version
228
- version: '0'
228
+ version: 1.72.1
229
229
  type: :development
230
230
  prerelease: false
231
231
  version_requirements: !ruby/object:Gem::Requirement
232
232
  requirements:
233
233
  - - ">="
234
234
  - !ruby/object:Gem::Version
235
- version: '0'
235
+ version: 1.72.1
236
236
  - !ruby/object:Gem::Dependency
237
237
  name: rubocop-rails
238
238
  requirement: !ruby/object:Gem::Requirement
@@ -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
@@ -359,11 +363,12 @@ files:
359
363
  - lib/granite/version.rb
360
364
  - lib/rubocop-granite.rb
361
365
  - lib/rubocop/granite.rb
362
- - lib/rubocop/granite/inject.rb
366
+ - lib/rubocop/granite/plugin.rb
363
367
  homepage: https://github.com/toptal/granite
364
368
  licenses:
365
369
  - MIT
366
370
  metadata:
371
+ default_lint_roller_plugin: RuboCop::Granite::Plugin
367
372
  rubygems_mfa_required: 'true'
368
373
  rdoc_options: []
369
374
  require_paths:
@@ -379,7 +384,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
379
384
  - !ruby/object:Gem::Version
380
385
  version: '0'
381
386
  requirements: []
382
- rubygems_version: 3.6.9
387
+ rubygems_version: 4.0.20
383
388
  specification_version: 4
384
389
  summary: Another business actions architecture for Rails apps
385
390
  test_files: []
@@ -1,18 +0,0 @@
1
- # The original code is from https://github.com/rubocop-hq/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
2
- # See https://github.com/rubocop-hq/rubocop-rspec/blob/master/MIT-LICENSE.md
3
- module RuboCop
4
- module Granite
5
- # Because RuboCop doesn't yet support plugins, we have to monkey patch in a
6
- # bit of our configuration.
7
- module Inject
8
- def self.defaults!
9
- path = CONFIG_DEFAULT.to_s
10
- hash = ConfigLoader.load_file(path)
11
- config = Config.new(hash, path).tap(&:make_excludes_absolute)
12
- Rails.logger.debug { "configuration from #{path}" } if ConfigLoader.debug?
13
- config = ConfigLoader.merge_with_default(config, path)
14
- ConfigLoader.instance_variable_set(:@default_configuration, config)
15
- end
16
- end
17
- end
18
- end