store_model 1.4.0 → 1.5.1

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: 04d0447a36db9c3cec8c6636e0480c188d5d1f70011485dc8aad3aef17979fe5
4
- data.tar.gz: ab7835f6a2fa0e3d481a92c9dd94f7946f0da2d788f665faefe8196d141d57e4
3
+ metadata.gz: 11081c035b30aac60161c2efaa4c629faf80aee154135d71cd9d1723ac0f96ea
4
+ data.tar.gz: 7ccd368264fd10a0731b81d30f708d1187f5b4571dc0e27cc374e650ab8c5d91
5
5
  SHA512:
6
- metadata.gz: 271ef39c6e35da560a31646b0731f25daa56005ae955c42c40236befbc98bfb3479e233f84cbabc4a40d299c65812bbc752b55e4e9350f65aaaea89cf5c8e0ac
7
- data.tar.gz: a5da18d298f3c254b0f0f2d38075953077a601fc4589df632d6d31222338b12f808cfecc5138d1916a34bf6aa67da96bb71535bb74fa3989a31df44e013d9305
6
+ metadata.gz: 9842ac11b144fe8bde05bb50e0d91b6301450a8bd3f67cfdb3beca09c7d806568dc40994db42eebe929c01eca83ce8762a622eeea512bdfa0e04b92458ed33ba
7
+ data.tar.gz: 88044a2f28e0edf8a6babfd7935d1e89af2df45e6b9c831227cfabc9346e89421d53ce19e8dfcdf44c96efe93d3bf6faf28ccdf3251fb167391ccba38eef81e5
@@ -10,16 +10,52 @@ module StoreModel
10
10
  module ClassMethods # :nodoc:
11
11
  # Enables handling of nested StoreModel::Model attributes
12
12
  #
13
- # @param associations [Array] list of associations to define attributes
13
+ # @param associations [Array] list of associations and options to define attributes, for example:
14
+ # accepts_nested_attributes_for [:suppliers, allow_destroy: true]
15
+ #
16
+ # Supported options:
17
+ # [:allow_destroy]
18
+ # If true, destroys any members from the attributes hash with a
19
+ # <tt>_destroy</tt> key and a value that evaluates to +true+
20
+ # (e.g. 1, '1', true, or 'true'). This option is off by default.
14
21
  def accepts_nested_attributes_for(*associations)
15
- associations.each do |association|
22
+ associations.each do |association, options|
16
23
  case attribute_types[association.to_s]
17
24
  when Types::One
25
+ define_association_setter_for_single(association, options)
18
26
  alias_method "#{association}_attributes=", "#{association}="
19
27
  when Types::Many
20
- define_method "#{association}_attributes=" do |attributes|
21
- assign_nested_attributes_for_collection_association(association, attributes)
22
- end
28
+ define_association_setter_for_many(association, options)
29
+ end
30
+
31
+ define_attr_accessor_for_destroy(association, options)
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def define_attr_accessor_for_destroy(association, options)
38
+ return unless options&.dig(:allow_destroy)
39
+
40
+ attribute_types[association.to_s].model_klass.class_eval do
41
+ attr_accessor :_destroy
42
+ end
43
+ end
44
+
45
+ def define_association_setter_for_many(association, options)
46
+ define_method "#{association}_attributes=" do |attributes|
47
+ assign_nested_attributes_for_collection_association(association, attributes, options)
48
+ end
49
+ end
50
+
51
+ def define_association_setter_for_single(association, options)
52
+ return unless options&.dig(:allow_destroy)
53
+
54
+ define_method "#{association}=" do |attributes|
55
+ if ActiveRecord::Type::Boolean.new.cast(attributes.stringify_keys.dig("_destroy"))
56
+ super(nil)
57
+ else
58
+ super(attributes)
23
59
  end
24
60
  end
25
61
  end
@@ -27,9 +63,16 @@ module StoreModel
27
63
 
28
64
  private
29
65
 
30
- def assign_nested_attributes_for_collection_association(association, attributes)
66
+ def assign_nested_attributes_for_collection_association(association, attributes, options)
31
67
  attributes = attributes.values if attributes.is_a?(Hash)
32
- send "#{association}=", attributes
68
+
69
+ if options&.dig(:allow_destroy)
70
+ attributes.reject! do |attribute|
71
+ ActiveRecord::Type::Boolean.new.cast(attribute.stringify_keys.dig("_destroy"))
72
+ end
73
+ end
74
+
75
+ send("#{association}=", attributes)
33
76
  end
34
77
  end
35
78
  end
@@ -7,6 +7,8 @@ module StoreModel
7
7
  # Implements ActiveModel::Type::Value type for handling an array of
8
8
  # StoreModel::Model
9
9
  class ManyBase < ActiveModel::Type::Value
10
+ attr_reader :model_klass
11
+
10
12
  # Returns type
11
13
  #
12
14
  # @return [Symbol]
@@ -6,6 +6,8 @@ module StoreModel
6
6
  module Types
7
7
  # Implements ActiveModel::Type::Value type for handling an instance of StoreModel::Model
8
8
  class OneBase < ActiveModel::Type::Value
9
+ attr_reader :model_klass
10
+
9
11
  # Returns type
10
12
  #
11
13
  # @return [Symbol]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel # :nodoc:
4
- VERSION = "1.4.0"
4
+ VERSION = "1.5.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: store_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-16 00:00:00.000000000 Z
11
+ date: 2023-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord