active_form_objects 2.0.4 → 2.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dsl/attributes.rb +10 -0
- data/lib/dsl/error_handling.rb +3 -2
- data/lib/dsl/hooks.rb +6 -0
- data/lib/handlers/attributes.rb +9 -0
- data/lib/handlers/hooks.rb +6 -0
- data/lib/handlers/savings.rb +7 -2
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8035dcb27f78b8e96f423efa20368bf9a458a10254cf11daac46ac5da94f2e5
|
4
|
+
data.tar.gz: 8547c9553340dcc67451b6f0a5eddfe21db24ebeddcd5064f1627344f6070ce6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '08eecf07d908b7940c5611beb5676e629ddc6761bea108fca62689a90b2ec614cc6cfe3f2e65f0bb6f4544fc8b5fa7ff77986ff51658c8cf21969a4aa4a824d5'
|
7
|
+
data.tar.gz: 4fab25d2fa5c139d5db542dc14bdc52c190ea4fad92c76cdbe985215eeb2333b79dc443e85f19fc3a77c2084e40e0343ad1e487ee569866dcea563ef9b36894f
|
data/lib/dsl/attributes.rb
CHANGED
@@ -8,6 +8,7 @@ module Dsl
|
|
8
8
|
|
9
9
|
@@attributes = {}
|
10
10
|
@@overrided_params = {}
|
11
|
+
@@remaped_params = {}
|
11
12
|
@@default_params = {}
|
12
13
|
@@preparers = {}
|
13
14
|
|
@@ -46,6 +47,15 @@ module Dsl
|
|
46
47
|
send(:attributes, param)
|
47
48
|
end
|
48
49
|
|
50
|
+
def self.remap(key, options)
|
51
|
+
@@remaped_params[name] = (@@remaped_params[name] || []) + [{
|
52
|
+
key: key,
|
53
|
+
to: options[:to]
|
54
|
+
}]
|
55
|
+
|
56
|
+
send(:attributes, options[:to])
|
57
|
+
end
|
58
|
+
|
49
59
|
def self.prepare(*params)
|
50
60
|
raise ActiveFormObjects::DslError.new("[#{self.name}] prepare takes a lambda as second argument, not a #{params.last.class.name}") if !params.last.respond_to?(:call)
|
51
61
|
|
data/lib/dsl/error_handling.rb
CHANGED
@@ -6,8 +6,9 @@ module Dsl
|
|
6
6
|
record = (e.try(:record) || e.model)
|
7
7
|
initial_messages = record.errors.messages.as_json
|
8
8
|
record.errors.clear
|
9
|
-
|
10
|
-
|
9
|
+
initial_messages.each do |i|
|
10
|
+
record.errors.add(form, i)
|
11
|
+
end
|
11
12
|
raise ActiveModel::ValidationError, record
|
12
13
|
end
|
13
14
|
end
|
data/lib/dsl/hooks.rb
CHANGED
@@ -8,6 +8,7 @@ module Dsl
|
|
8
8
|
@@before_save = {}
|
9
9
|
@@after_save = {}
|
10
10
|
@@after_validation = {}
|
11
|
+
@@after_commit = {}
|
11
12
|
|
12
13
|
class_methods do
|
13
14
|
def before_save(*params, &block)
|
@@ -24,6 +25,11 @@ module Dsl
|
|
24
25
|
hooks = block.present? ? [block] : params
|
25
26
|
@@after_validation[name] = (@@after_validation[name] || []) + hooks
|
26
27
|
end
|
28
|
+
|
29
|
+
def after_commit(*params, &block)
|
30
|
+
hooks = block.present? ? [block] : params
|
31
|
+
@@after_commit[name] = (@@after_commit[name] || []) + hooks
|
32
|
+
end
|
27
33
|
end
|
28
34
|
end
|
29
35
|
end
|
data/lib/handlers/attributes.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'active_support/core_ext/hash/reverse_merge'
|
2
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
2
3
|
require_relative 'base'
|
3
4
|
require_relative 'relations'
|
4
5
|
require_relative 'delegates'
|
@@ -8,6 +9,7 @@ module Handlers
|
|
8
9
|
def handle
|
9
10
|
start_with_default_params!
|
10
11
|
affect_prepared_values!
|
12
|
+
remap_attributes!
|
11
13
|
merge_with_resource_attributes!
|
12
14
|
affect_relation_values!
|
13
15
|
merge_overrided_params!
|
@@ -27,6 +29,13 @@ module Handlers
|
|
27
29
|
end
|
28
30
|
end
|
29
31
|
|
32
|
+
# DSL method : remap
|
33
|
+
def remap_attributes!
|
34
|
+
(upper(:@@remaped_params) || []).each do |remaped|
|
35
|
+
@params[remaped[:to]] = @raw_params[remaped[:key].to_s] if @raw_params.key?(remaped[:key].to_s) && @params[remaped[:to]].blank?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
30
39
|
def merge_with_resource_attributes!
|
31
40
|
return unless @resource.respond_to?(:attributes)
|
32
41
|
@params.reverse_merge!(@resource.attributes.symbolize_keys)
|
data/lib/handlers/hooks.rb
CHANGED
data/lib/handlers/savings.rb
CHANGED
@@ -14,8 +14,9 @@ module Handlers
|
|
14
14
|
after_validations_hooks!
|
15
15
|
block ? save_with_block!(block) : save_without_block!
|
16
16
|
after_save_hooks!
|
17
|
-
@resource
|
18
17
|
end
|
18
|
+
after_commit_hooks!
|
19
|
+
@resource
|
19
20
|
rescue ActiveRecord::RecordInvalid => e
|
20
21
|
e.record.errors.add(e.record.class.name, '')
|
21
22
|
raise ActiveRecord::RecordInvalid.new(e.record)
|
@@ -29,7 +30,7 @@ module Handlers
|
|
29
30
|
def save_with_block!(block)
|
30
31
|
@klass.instance_eval(&block)
|
31
32
|
@resource = @klass.instance_variable_get(:@resource)
|
32
|
-
@resource
|
33
|
+
@resource
|
33
34
|
end
|
34
35
|
|
35
36
|
def save_without_block!
|
@@ -40,5 +41,9 @@ module Handlers
|
|
40
41
|
AfterValidationsHooks.handle(@klass)
|
41
42
|
Polymorphs.handle(@klass)
|
42
43
|
end
|
44
|
+
|
45
|
+
def after_commit_hooks!
|
46
|
+
AfterCommitHooks.handle(@klass)
|
47
|
+
end
|
43
48
|
end
|
44
49
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_form_objects
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michaël Villeneuve
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -142,8 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
142
|
- !ruby/object:Gem::Version
|
143
143
|
version: '0'
|
144
144
|
requirements: []
|
145
|
-
|
146
|
-
rubygems_version: 2.7.3
|
145
|
+
rubygems_version: 3.1.2
|
147
146
|
signing_key:
|
148
147
|
specification_version: 4
|
149
148
|
summary: A step toward an hexagonal Rails architecture
|