store_model 1.4.0 → 1.5.0
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 +4 -4
- data/lib/store_model/nested_attributes.rb +40 -7
- data/lib/store_model/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cc3ab45857fd85292b845bb4b7dccfe0c3df64e4baebb1387599800f16caafe
|
4
|
+
data.tar.gz: a772d44ba83b7236992f21b854a1b7a76ef353c2b32083f996a9c78306402468
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2ece15986e487b1eb67c6a81dc8c2a6173f726f7be72ee531328b9fe79271a30d6cd608d0d51d4d32ffd59316e7f961c946445d83f686099dc9992dc256f990
|
7
|
+
data.tar.gz: e9e4512c14d680360db6fb57b0e9a96676a62377656057ab4a530730c38c91826f7603bf5ea11e46e1c8673c2d0c93d48a6c1347350db34a6c2cd9f6753fd9a2
|
@@ -10,16 +10,42 @@ 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
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def define_association_setter_for_many(association, options)
|
36
|
+
define_method "#{association}_attributes=" do |attributes|
|
37
|
+
assign_nested_attributes_for_collection_association(association, attributes, options)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def define_association_setter_for_single(association, options)
|
42
|
+
return unless options&.dig(:allow_destroy)
|
43
|
+
|
44
|
+
define_method "#{association}=" do |attributes|
|
45
|
+
if ActiveRecord::Type::Boolean.new.cast(attributes.stringify_keys.dig("_destroy"))
|
46
|
+
super(nil)
|
47
|
+
else
|
48
|
+
super(attributes)
|
23
49
|
end
|
24
50
|
end
|
25
51
|
end
|
@@ -27,9 +53,16 @@ module StoreModel
|
|
27
53
|
|
28
54
|
private
|
29
55
|
|
30
|
-
def assign_nested_attributes_for_collection_association(association, attributes)
|
56
|
+
def assign_nested_attributes_for_collection_association(association, attributes, options)
|
31
57
|
attributes = attributes.values if attributes.is_a?(Hash)
|
32
|
-
|
58
|
+
|
59
|
+
if options&.dig(:allow_destroy)
|
60
|
+
attributes.reject! do |attribute|
|
61
|
+
ActiveRecord::Type::Boolean.new.cast(attribute.stringify_keys.dig("_destroy"))
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
send("#{association}=", attributes)
|
33
66
|
end
|
34
67
|
end
|
35
68
|
end
|
data/lib/store_model/version.rb
CHANGED