store_model 2.1.1 → 2.1.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: a3b12509ebafeb20d7a2bcb2ae1094dcd756f1b86f31dd8066b0abd61d8f998a
4
- data.tar.gz: d3a06944b46ebf4094de55736e892f15ae27514e0604047260e74a033bbc7cda
3
+ metadata.gz: db36f4fa51b9f71a34198ba18b921565d46a450c62e4892628dfee8e067bf4ff
4
+ data.tar.gz: 4e72e8f0fc7051105a09843040a4cfc61f248884ac2dcc8def0f6a95f9b8390f
5
5
  SHA512:
6
- metadata.gz: 7d8ec95caf59aea39e3cedc20a471db9289f4c1eda9d083a1b2fd321f732e7d056b67ac58a7f4f437cb04e932c1f14f18e56dfb9b04c884e5d64025cc7d7989b
7
- data.tar.gz: e526dbc89d1c4186b1fe94581acb2b1d80e207bcf08d69809dffc393d43e3d8d2cd08c905d3d3d4f842c9bf7db9ad1581611368fabe8206eb0445c16a2c615eb
6
+ metadata.gz: 5c5073d90550cd90049cb0cc2555bfab36c4b036857319ac5bf0f0eeadaea70deabcf71e549c7e1348127f719ba9f03bda444fb700153b8bdbbb5ffdc3e36c0f
7
+ data.tar.gz: f5a3eed707850087dfc95c908725624352da067410b3fa69762764545fd83c243a99bfbf8a4b4ae53086fdff9094377769be720cf7497b0d82ae1d7ad736e411
@@ -35,25 +35,45 @@ module StoreModel
35
35
  #
36
36
  # See https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for
37
37
  def accepts_nested_attributes_for(*attributes)
38
- global_options = attributes.extract_options!
38
+ options = attributes.extract_options!
39
39
 
40
- attributes.each do |attribute, options|
41
- case attribute_types[attribute.to_s]
40
+ attributes.each do |attribute|
41
+ case nested_attribute_type(attribute)
42
42
  when Types::OneBase, Types::ManyBase
43
- define_store_model_attr_accessors(attribute, options || global_options)
43
+ options.reverse_merge!(allow_destroy: false, update_only: false)
44
+ define_store_model_attr_accessors(attribute, options)
44
45
  else
45
- super(attribute, options || global_options)
46
+ super(*attribute, options)
46
47
  end
47
48
  end
48
49
  end
49
50
 
50
51
  private
51
52
 
53
+ # If attribute defined in ActiveRecord model but you dont yet have database created
54
+ # you cannot access attribute types.
55
+ # To handle this case, we can use ActiveRecord::Attributes 'attributes_to_define_after_schema_loads'
56
+ # which stores information about custom defined attributes.
57
+ # See ActiveRecord::Attributes#atribute
58
+ # If #accepts_nested_attributes_for is used inside active model instance
59
+ # schema is not required to determine attribute type so we can still use attribute_types
60
+ # If schema loaded the attribute_types already populated and we can safely use it
61
+ # See ActiveRecord::ModelSchema#load_schema!
62
+ def nested_attribute_type(attribute)
63
+ if self < ActiveRecord::Base && !schema_loaded?
64
+ attributes_to_define_after_schema_loads[attribute.to_s]&.first
65
+ else
66
+ attribute_types[attribute.to_s]
67
+ end
68
+ end
69
+
52
70
  def define_store_model_attr_accessors(attribute, options)
53
- case attribute_types[attribute.to_s]
71
+ case nested_attribute_type(attribute)
54
72
  when Types::OneBase
55
73
  define_association_setter_for_single(attribute, options)
56
- alias_method "#{attribute}_attributes=", "#{attribute}="
74
+ define_method "#{attribute}_attributes=" do |*args, **kwargs|
75
+ send("#{attribute}=", *args, **kwargs)
76
+ end
57
77
  when Types::ManyBase
58
78
  define_association_setter_for_many(attribute, options)
59
79
  end
@@ -88,7 +108,10 @@ module StoreModel
88
108
  end
89
109
  end
90
110
 
91
- def assign_nested_attributes_for_collection_association(association, attributes, options)
111
+ # Base
112
+ def assign_nested_attributes_for_collection_association(association, attributes, options = nil)
113
+ return super(association, attributes) unless options
114
+
92
115
  attributes = attributes.values if attributes.is_a?(Hash)
93
116
 
94
117
  if options&.dig(:allow_destroy)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel # :nodoc:
4
- VERSION = "2.1.1"
4
+ VERSION = "2.1.2"
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: 2.1.1
4
+ version: 2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-29 00:00:00.000000000 Z
11
+ date: 2023-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord