activemodel-embedding 0.1.5 → 0.2.0

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: 8f2e73a8dfef66fcbb740be69feb3b700e0ac9dccb51e3e2d5667a46d66ff601
4
- data.tar.gz: db5c730161a89a5618bdd556d3927fad81b5aee4b1fa574e76957b6b4e211591
3
+ metadata.gz: c01c4df6223c093586de6dfaab400aa0038ccd3d0d647190a7659f473b65645e
4
+ data.tar.gz: 002ab8e0340a0c4d77bad5a8350a5c351f9e178f768052a84b72f157828292e3
5
5
  SHA512:
6
- metadata.gz: 23a9641ef3e6d52a1b491b48652b71eeac71ebb872a43a92d21430505e725acbc0085c97b20409a7dd9b0b0f9b6c3d0216ad2584155ddf1849b58edad1b7de3d
7
- data.tar.gz: 536223569744645e51403284c0c68257712039c8d6bcfa32ada21f7565dc02156f60ad58302708d0e6892a7d7931707bbbdc2e8a4d500aa96b164435a1395718
6
+ metadata.gz: 04b3a70f58161bc302727fdb6d1c5a932c23a9b3503317359afc77e6154eed9b4d28ab2a62f50b85d5c62b721290ae07d8360ce2dbb9dc762b846a6585962ec4
7
+ data.tar.gz: ecde4fa3045957a2f14430c6cacdb11af2f1400d711359dd8edacbdc28747a204d4c8b4a9053d6e15d76283969163239d52daed7c6c859e78f55a9e1d827ce8e
data/README.md CHANGED
@@ -13,8 +13,9 @@ An ActiveModel extension to model your [semi-structured data](#semi-structured-d
13
13
  - [License](#license)
14
14
 
15
15
  ## Features
16
- - ActiveRecord-like associations (powered by the [Attributes API](https://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html#method-i-attribute))
16
+ - [Embedded associations](#embedded-associations) (powered by the [Attributes API](https://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html))
17
17
  - Nested attributes support out-of-the-box
18
+ - [Validations](#validations)
18
19
  - [Custom collections](#custom-collections)
19
20
  - [Custom types](#custom-types)
20
21
  - Autosaving
@@ -115,6 +116,45 @@ And display it like this (with nested attributes support out-of-the-box):
115
116
  <%= book_form.submit %>
116
117
  <% end %>
117
118
  ```
119
+ ### Validations
120
+ ```ruby
121
+ class SomeModel < ApplicationRecord
122
+ include ActiveModel::Embedding::Associations
123
+
124
+ embeds_many :things
125
+
126
+ validates_associated :things
127
+ end
128
+
129
+ class Thing
130
+ include ActiveModel::Embedding::Document
131
+
132
+ embeds_many :other_things
133
+
134
+ validates_associated :other_things
135
+ end
136
+
137
+ class OtherThing
138
+ include ActiveModel::Embedding::Document
139
+
140
+ attribute :some_attribute, :string
141
+
142
+ validates :some_attribute, presence: true
143
+ end
144
+ ```
145
+ ```ruby
146
+ things = Array.new(3) { Thing.new(other_things: Array.new(3) { OtherThing.new } }
147
+ record = SomeModel.new things: things
148
+
149
+ record.valid? # => false
150
+ record.save # => false
151
+
152
+ record.things.other_things = Array.new(3) { OtherThing.new(some_attribute: "present") }
153
+
154
+ record.valid? # => true
155
+ record.save # => true
156
+ ```
157
+
118
158
  ### Custom collections
119
159
  ```ruby
120
160
  class SomeCollection
@@ -610,13 +650,13 @@ API for defining [embedded associations](#embedded-associations). Uses the Attri
610
650
  the `:document` type.
611
651
 
612
652
  ### `ActiveModel::Embedding::Document`
613
- A module which includes everything needed to work with the `:document` type
653
+ A mixin which includes everything needed to work with the `:document` type
614
654
  (`ActiveModel::Model`, `ActiveModel::Attributes`, `ActiveModel::Serializers::JSON`,
615
655
  `ActiveModel::Embedding::Associations`). Provides an `id` attribute and implements methods like `#persisted?`
616
656
  and `#save` to emulate persistence.
617
657
 
618
658
  ### `ActiveModel::Embedding::Collecting`
619
- A module which provides capabailities similar to ActiveRecord collection proxies. Provides
659
+ A mixin which provides capabailities similar to ActiveRecord collection proxies. Provides
620
660
  support for nested attributes.
621
661
 
622
662
  ### `ActiveModel::Embedding::Collection`
@@ -644,7 +684,7 @@ The gem is available as open source under the terms of the [MIT
644
684
  License](https://opensource.org/licenses/MIT).
645
685
 
646
686
  ## Alternatives
647
- Here's are some alternatives I came accross after I've started working on this gem:
687
+ Here's some alternatives I came accross after I've started working on this gem:
648
688
  - [attr_json](https://github.com/jrochkind/attr_json)
649
689
  - [store_model](https://github.com/DmitryTsepelev/store_model)
650
690
 
@@ -21,7 +21,7 @@ module ActiveModel
21
21
  public_send association_name
22
22
  end.compact
23
23
 
24
- targets.all?(&:save)
24
+ targets.each(&:save)
25
25
  end
26
26
 
27
27
  yield
@@ -86,10 +86,6 @@ module ActiveModel
86
86
  documents.all?(&:save)
87
87
  end
88
88
 
89
- def persisted?
90
- documents.all?(&:persisted?)
91
- end
92
-
93
89
  def each
94
90
  return self.to_enum unless block_given?
95
91
 
@@ -5,6 +5,7 @@ module ActiveModel
5
5
  module Document
6
6
  def self.included(klass)
7
7
  klass.class_eval do
8
+ extend ClassMethods
8
9
  extend ActiveModel::Callbacks
9
10
 
10
11
  define_model_callbacks :save
@@ -35,6 +36,13 @@ module ActiveModel
35
36
  end
36
37
  end
37
38
  end
39
+
40
+ module ClassMethods
41
+ def validates_associated(*attr_names)
42
+ validates_with ActiveRecord::Validations::AssociatedValidator,
43
+ _merge_attributes(attr_names)
44
+ end
45
+ end
38
46
  end
39
47
  end
40
48
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  module Embedding
3
- VERSION = '0.1.5'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activemodel-embedding
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mansakondo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-30 00:00:00.000000000 Z
11
+ date: 2021-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails