active_form_objects 1.0.0.beta → 1.0.0.beta.2

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: '03871811ee607637a50a7c85d85985dea846a563709f4d1b803aa681589e5517'
4
- data.tar.gz: d6fb754563a486d09e99cb09dc183dc276d83f0d1ab9b8a11c83003a8920340e
3
+ metadata.gz: 4cc38391b46ea4d3dd453708fd14842a0848017c270003e0ffcc67088aaf4ce8
4
+ data.tar.gz: 91c862cc0a28c7f2be22d9af319e41eae764421cf8234badacc0dc66c708afa5
5
5
  SHA512:
6
- metadata.gz: f041ee7728861ab7112f9202284bb1d133c877fe42def63e9ba437efb2ebc87efb8ffab77f5a7d519001eba7639c51b03b87371e5c35d5586bde821dc7a18ce4
7
- data.tar.gz: 362e0149393a0fd88f129e4169e16c3f037ec506610de8469fe6d33a5478f540c7fda66da8aafebe8be0981f17a200c58a011c94f0d695925ec1a5ff565bf194
6
+ metadata.gz: c9a52467cb2e00a176b5044869b125728b45eca076931ecbc5f049ca72eccf1b33d878516449ef29dbbcc08501add7c59ed5eeedd191faadb9d594b571b01146
7
+ data.tar.gz: 401e27c9bd4024c242eda7d211d2df1759cbea23a85a12c0375c59443490d4dec5edafd8159d4ea662fb8300935a2c09cf98c5f65e1ad6989eedd98220bc0632
@@ -1,16 +1,20 @@
1
1
  require 'active_model'
2
+ require 'active_support/core_ext/hash/indifferent_access'
3
+
2
4
 
3
5
  require_relative 'dsl/relations'
4
6
  require_relative 'dsl/attributes'
5
7
  require_relative 'dsl/delegates'
6
8
  require_relative 'dsl/savings'
7
9
  require_relative 'dsl/resource'
10
+ require_relative 'dsl/associates'
8
11
  require_relative 'dsl/polymorphs'
9
12
 
10
13
  require_relative 'handlers/initializer'
11
14
  require_relative 'handlers/relations'
12
15
  require_relative 'handlers/resource'
13
16
  require_relative 'handlers/attributes'
17
+ require_relative 'handlers/associates'
14
18
 
15
19
  module ActiveFormObjects
16
20
  class Base
@@ -21,13 +25,15 @@ module ActiveFormObjects
21
25
  include Dsl::Relations
22
26
  include Dsl::Polymorphs
23
27
  include Dsl::Delegates
28
+ include Dsl::Associates
24
29
  include Dsl::Savings
25
30
 
26
31
  def initialize(params, resource = nil)
27
- @params = params
32
+ @params = params.with_indifferent_access
28
33
  @resource = resource
29
34
 
30
35
  Handlers::Initializer.handle(self)
36
+ Handlers::Associates.handle(self)
31
37
  Handlers::Resource.handle(self)
32
38
  Handlers::Attributes.handle(self)
33
39
  super(@params)
@@ -0,0 +1,18 @@
1
+ require 'active_support'
2
+
3
+ module Dsl
4
+ module Associates
5
+ extend ActiveSupport::Concern
6
+
7
+ @@associated_forms = {}
8
+
9
+ class_methods do
10
+ def associated(key, form, params = nil)
11
+ @@associated_forms[name] ||= []
12
+ @@associated_forms[name] << { name: key, form: form, params: params }
13
+
14
+ set_default value: {}, to: key
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,4 +1,5 @@
1
1
  require 'active_support'
2
+ require 'active_support/core_ext/hash/indifferent_access'
2
3
 
3
4
  module Dsl
4
5
  module Attributes
@@ -9,6 +10,14 @@ module Dsl
9
10
  @@default_params = {}
10
11
  @@preparers = {}
11
12
 
13
+ def attrs_only(params)
14
+ attributes.slice(*params)
15
+ end
16
+
17
+ def attributes
18
+ instance_values.with_indifferent_access.symbolize_keys
19
+ end
20
+
12
21
  included do
13
22
  attr_accessor :params
14
23
 
data/lib/dsl/delegates.rb CHANGED
@@ -5,13 +5,20 @@ module Dsl
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  @@delegators = {}
8
-
9
8
  included do
10
9
  def self.delegate(*params)
11
- delegator = params.last[:to].merge(attributes: params)
10
+ delegator = params.last[:to]
12
11
  params.pop
13
12
 
14
- @@delegators[name] = (@@delegators[name] || []) << delegator
13
+ if delegator.is_a?(Hash)
14
+ delegator[:attributes] = params
15
+ @@delegators[name] = (@@delegators[name] || []) << delegator
16
+ else
17
+ send(:attributes, *params)
18
+ define_method("#{delegator}_params") do
19
+ attrs_only(params)
20
+ end
21
+ end
15
22
  end
16
23
  end
17
24
  end
@@ -0,0 +1,23 @@
1
+ require 'active_support/core_ext/hash/reverse_merge'
2
+ require_relative 'base'
3
+
4
+ module Handlers
5
+ class Associates < Base
6
+ def handle
7
+ (upper('@@associated_forms') || []).each do |form|
8
+ @klass.instance_variable_set("@#{form[:name]}_form", form[:form])
9
+ end
10
+ end
11
+
12
+ def save_associated_forms!
13
+ (upper('@@associated_forms') || []).each do |associated|
14
+ params = associated[:params].nil? ? @params[associated[:name]] : @klass.send(associated[:params])
15
+ resource_to_save = @klass.send("#{associated[:name]}_to_update") rescue nil
16
+ the_form = associated[:form]
17
+
18
+ saved_form = the_form.new(params, resource_to_save).save!
19
+ @klass.instance_variable_set("@#{associated[:name]}", saved_form)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -3,7 +3,8 @@ require_relative 'base'
3
3
  module Handlers
4
4
  class Delegates < Base
5
5
  def handle
6
- delegates = upper :@@delegators
6
+ delegates = upper('@@delegators')
7
+
7
8
  (delegates || []).each do |delegate|
8
9
  Delegate.new(@klass, delegate).handle
9
10
  end
@@ -23,7 +24,7 @@ module Handlers
23
24
  end
24
25
 
25
26
  def handle
26
- @attributes = @raw_params.slice(*@attributes).merge({
27
+ @attributes = @raw_params.slice(*@attributes.map(&:to_s)).merge({
27
28
  @foreign_key => @resource.id
28
29
  })
29
30
 
@@ -34,15 +34,14 @@ module Handlers
34
34
  def handle
35
35
  @params_to_send = @params[@polymorph[:key]] || {}
36
36
  polymorph_instance = form_class.new(@params_to_send, @resource.try(@polymorph[:key])).save!
37
-
38
- @params["#{@polymorph[:key]}_id".to_sym] = polymorph_instance.id
39
- @params["#{@polymorph[:key]}_type".to_sym] = polymorph_instance.class.name
37
+ @params["#{@polymorph[:key]}_id"] = polymorph_instance.id
38
+ @params["#{@polymorph[:key]}_type"] = polymorph_instance.class.name
40
39
  @params.delete(@polymorph[:key])
41
40
  end
42
41
 
43
42
  def form_class
44
43
  @polymorph[:types][
45
- @resource.try("#{@polymorph[:key]}_type".to_sym) || clean_params.try(:to_sym)
44
+ @resource.try("#{@polymorph[:key]}_type") || clean_params.try(:to_sym)
46
45
  ]
47
46
  end
48
47
 
@@ -5,7 +5,7 @@ module Handlers
5
5
  def affect_relation_values!
6
6
  relation_keys = upper(:@@relation_keys) || {}
7
7
  {}.merge(@params).each_key do |key|
8
- @params["#{key}_id".to_sym] = @params[key.to_sym].id if relation_keys.include?(key)
8
+ @params["#{key}_id"] = @params[key].id if relation_keys.include?(key.to_sym)
9
9
  end
10
10
  end
11
11
  end
@@ -4,19 +4,19 @@ require 'active_record'
4
4
  require_relative 'base'
5
5
  require_relative 'polymorphs'
6
6
  require_relative 'delegates'
7
+ require_relative 'associates'
7
8
 
8
9
  module Handlers
9
10
  class Savings < Base
10
11
  def save!(&block)
11
- # ActiveRecord::Base.transaction do
12
+ ActiveRecord::Base.transaction do
13
+ return save_associated_forms! unless upper('@@associated_forms').nil?
12
14
  @klass.validate!
13
-
14
15
  before_save_hooks!
15
16
  block ? save_with_block!(block) : save_without_block!
16
17
  after_save_hooks!
17
-
18
18
  @resource
19
- # end
19
+ end
20
20
  rescue ActiveRecord::RecordInvalid => e
21
21
  e.record.errors.add(e.record.class.name, '')
22
22
  raise ActiveRecord::RecordInvalid.new(e.record)
@@ -28,6 +28,7 @@ module Handlers
28
28
 
29
29
  def save_with_block!(block)
30
30
  @klass.instance_eval(&block)
31
+ @resource = @klass.instance_variable_get(:@resource)
31
32
  @resource.try(:save!)
32
33
  end
33
34
 
@@ -38,5 +39,9 @@ module Handlers
38
39
  def before_save_hooks!
39
40
  Polymorphs.handle(@klass)
40
41
  end
42
+
43
+ def save_associated_forms!
44
+ Associates.new(@klass).save_associated_forms!
45
+ end
41
46
  end
42
47
  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: 1.0.0.beta
4
+ version: 1.0.0.beta.2
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: 2018-11-20 00:00:00.000000000 Z
11
+ date: 2018-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -59,12 +59,14 @@ extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
61
  - lib/active_form_objects.rb
62
+ - lib/dsl/associates.rb
62
63
  - lib/dsl/attributes.rb
63
64
  - lib/dsl/delegates.rb
64
65
  - lib/dsl/polymorphs.rb
65
66
  - lib/dsl/relations.rb
66
67
  - lib/dsl/resource.rb
67
68
  - lib/dsl/savings.rb
69
+ - lib/handlers/associates.rb
68
70
  - lib/handlers/attributes.rb
69
71
  - lib/handlers/base.rb
70
72
  - lib/handlers/delegates.rb