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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2daaa8dd6cc58ada79b0fbab645c19bdc3f8ba9439d5c2c58e2a9db8d45b64ff
4
- data.tar.gz: 2ca8cb23a9f2b1118cfbf699463d64e1e6db8d9a3804ce8e56abffbdbef846e5
3
+ metadata.gz: e8035dcb27f78b8e96f423efa20368bf9a458a10254cf11daac46ac5da94f2e5
4
+ data.tar.gz: 8547c9553340dcc67451b6f0a5eddfe21db24ebeddcd5064f1627344f6070ce6
5
5
  SHA512:
6
- metadata.gz: c2f92a20ca5941af70874c8bce8d7c4cf1a963fe61c933e08a9321e67212a8dfc4c8a88936ded00a063f07825b26d44185fbc50c667e8ded2ce22b3ce10f67a9
7
- data.tar.gz: 49b4ab98dc7d917878ef897a080ce95b61d11345077ee5c93554ac64e1472ad474e9f7ba4ba45cb5c05887c3bb36b5c32640a5712ee3d5b034edf12c3b62a466
6
+ metadata.gz: '08eecf07d908b7940c5611beb5676e629ddc6761bea108fca62689a90b2ec614cc6cfe3f2e65f0bb6f4544fc8b5fa7ff77986ff51658c8cf21969a4aa4a824d5'
7
+ data.tar.gz: 4fab25d2fa5c139d5db542dc14bdc52c190ea4fad92c76cdbe985215eeb2333b79dc443e85f19fc3a77c2084e40e0343ad1e487ee569866dcea563ef9b36894f
@@ -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
 
@@ -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
- record.errors.messages[form] = initial_messages
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
@@ -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
@@ -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)
@@ -39,4 +39,10 @@ module Handlers
39
39
  handle_hooks('after_validation')
40
40
  end
41
41
  end
42
+
43
+ class AfterCommitHooks < BeforeSaveHooks
44
+ def handle
45
+ handle_hooks('after_commit')
46
+ end
47
+ end
42
48
  end
@@ -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.try(:save!)
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.0.4
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: 2019-05-15 00:00:00.000000000 Z
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
- rubyforge_project:
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