granite 0.10.0 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/granite/assign_data.rb +38 -0
- data/lib/granite/base.rb +4 -0
- data/lib/granite/dispatcher.rb +2 -2
- data/lib/granite/represents.rb +1 -4
- data/lib/granite/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c87ec932a4c14889c6de808acc812dc2eac40bda53454aa5b737176a3b02c01
|
4
|
+
data.tar.gz: 57c47008c125ec1283bddb6e407b94be2099e1a6e02f909751de03ecbe0669c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/granite/dispatcher.rb
CHANGED
@@ -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)
|
data/lib/granite/represents.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/granite/version.rb
CHANGED
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.
|
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-
|
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
|