granite 0.10.0 → 0.11.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: 6899733124529783eda1187a978475dea09fe5c716349d50f4dbf02f0c70a90c
4
- data.tar.gz: 81113bf6131ba438bb21af75c0b870ca3e0f8e07cbd8d754c274c259311ae640
3
+ metadata.gz: 7c87ec932a4c14889c6de808acc812dc2eac40bda53454aa5b737176a3b02c01
4
+ data.tar.gz: 57c47008c125ec1283bddb6e407b94be2099e1a6e02f909751de03ecbe0669c6
5
5
  SHA512:
6
- metadata.gz: 1f6715806d76f01ec2f017c83bacecc3a33dc3ed9099493a2f245f84eadabe87abe5835bcfcb16fda8fc2f5b71022c84053291a1f4d4c829691cfaf216068d46
7
- data.tar.gz: ec7f880050ff0a6b7de11d576d22038d5dcca6d0e3e71c2c7a2c4adb59280f153e05210bf7c7e4df519ddada2d5d337c12fdec34c3fddb060cf5dd82520ae3ff
6
+ metadata.gz: fca3402fa5593057b236362bdb6050c30f65b1f65dc7e48699ca0179e31005f8c74b1e613a901ae2211869fc2d28ad091fd008b350e7a61c5ba5807528929f98
7
+ data.tar.gz: 604bb52620145a6b23c37ca2a8e35250504171a7ce308371c4c4e964a83d7b5ce4d971ff8841ed3f607a3041ff31a9e9ce1058b46a8335811c1ed15411fd959b
@@ -0,0 +1,38 @@
1
+ module Granite
2
+ module AssignData
3
+ DataAssignment = Struct.new(:method, :options)
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :data_assignments
9
+ self.data_assignments = []
10
+
11
+ alias_method :only_run_validations!, :run_validations!
12
+ protected :only_run_validations! # rubocop:disable Style/AccessModifierDeclarations
13
+ end
14
+
15
+ module ClassMethods
16
+ # Defines a callback to call when assigning data from business action to model.
17
+ # @param methods [Array<Symbol>] list of methods to call
18
+ # @param block [Proc] a block to call
19
+ # @option options [Symbol, Proc, Object] :if call methods/block if this condition evaluates to true
20
+ # @option options [Symbol, Proc, Object] :unless call method/block unless this condition evaluates to true
21
+ def assign_data(*methods, **options, &block)
22
+ self.data_assignments += methods.map { |method| DataAssignment.new(method, options) }
23
+ self.data_assignments += [DataAssignment.new(block, options)] if block
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def run_validations!
30
+ assign_data
31
+ super
32
+ end
33
+
34
+ def assign_data
35
+ data_assignments.each { |assignment| evaluate(assignment.method) if conditions_satisfied?(**assignment.options) }
36
+ end
37
+ end
38
+ end
data/lib/granite/base.rb CHANGED
@@ -5,8 +5,11 @@ require 'active_data/model/associations'
5
5
 
6
6
  require 'granite/translations'
7
7
  require 'granite/represents'
8
+ require 'granite/assign_data'
8
9
 
9
10
  module Granite
11
+ # Base included in Granite::Action, but also used by ActiveData when building data objects (e.g. when using
12
+ # embeds_many)
10
13
  module Base
11
14
  extend ActiveSupport::Concern
12
15
 
@@ -19,6 +22,7 @@ module Granite
19
22
  include ActiveModel::Validations::Callbacks
20
23
 
21
24
  include Granite::Util
25
+ include Granite::AssignData
22
26
  include Granite::Represents
23
27
  end
24
28
  end
@@ -44,11 +44,11 @@ class Granite::Dispatcher
44
44
  ]
45
45
  end
46
46
 
47
- memoize def action_name(request_method_symbol, granite_action, granite_projector, projector_action = '')
47
+ memoize def action_name(request_method_symbol, granite_action, granite_projector, projector_action)
48
48
  projector = projector(granite_action, granite_projector)
49
49
  return unless projector
50
50
 
51
- projector.action_for(request_method_symbol, projector_action)
51
+ projector.action_for(request_method_symbol, projector_action.to_s)
52
52
  end
53
53
 
54
54
  memoize def projector(granite_action, granite_projector)
@@ -13,10 +13,7 @@ module Granite
13
13
  fields.each do |field|
14
14
  add_attribute Granite::Represents::Reflection, field, options, &block
15
15
 
16
- before_validation do
17
- attribute(field).sync if attribute(field).changed?
18
- true
19
- end
16
+ assign_data { attribute(field).sync if attribute(field).changed? }
20
17
  end
21
18
  end
22
19
  end
@@ -1,3 +1,3 @@
1
1
  module Granite
2
- VERSION = '0.10.0'.freeze
2
+ VERSION = '0.11.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: granite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toptal Engineering
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-07 00:00:00.000000000 Z
11
+ date: 2021-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -334,6 +334,7 @@ files:
334
334
  - lib/granite/action/translations.rb
335
335
  - lib/granite/action/types.rb
336
336
  - lib/granite/action/types/collection.rb
337
+ - lib/granite/assign_data.rb
337
338
  - lib/granite/base.rb
338
339
  - lib/granite/config.rb
339
340
  - lib/granite/context.rb