store_model 1.4.0 → 1.5.1
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11081c035b30aac60161c2efaa4c629faf80aee154135d71cd9d1723ac0f96ea
|
4
|
+
data.tar.gz: 7ccd368264fd10a0731b81d30f708d1187f5b4571dc0e27cc374e650ab8c5d91
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
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
|
data/lib/store_model/version.rb
CHANGED
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
|
+
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-
|
11
|
+
date: 2023-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|