activemodel-embedding 0.1.5 → 0.2.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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c01c4df6223c093586de6dfaab400aa0038ccd3d0d647190a7659f473b65645e
|
4
|
+
data.tar.gz: 002ab8e0340a0c4d77bad5a8350a5c351f9e178f768052a84b72f157828292e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
-
|
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
|
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
|
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
|
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
|
|
@@ -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
|
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.
|
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-
|
11
|
+
date: 2021-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|