nested_record 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +86 -5
- data/lib/nested_record/collection.rb +10 -0
- data/lib/nested_record/setup.rb +6 -1
- data/lib/nested_record/version.rb +1 -1
- data/spec/nested_record/collection_spec.rb +21 -0
- data/spec/nested_record_spec.rb +16 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4736181e42c531aad96202018bb79f79ec46aa69a0cf03a0a991abead29f4af
|
4
|
+
data.tar.gz: 7aa80b14123c4c8b1e8175d9ff5d187b6918d31ff0f1521a394ebd061d7b14cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f02b179664dc676bd35d4e6087194f714d58d56cf88058ea7d739be48e34ec330a1df0a2dd197eb2ed58579757331af7d13b6133ceae5a768f6b07773be0c32b
|
7
|
+
data.tar.gz: cffc632808753b0ecc39e2400328c42776f7a503e77f7373085438003c4efc3907d5d4a426737660875a611b22c0ce860f8eeb41b79edb1dd921dbafb7b11538
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# NestedRecord
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
This gem is for mapping of json fields on `ActiveModel` objects!
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,90 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
Use `nested_record` to define nested associations on `ActiveRecord` models via JSON attributes.
|
24
|
+
|
25
|
+
First add `json` column into your database:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
change_table :users do |t|
|
29
|
+
t.json :profile
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
Then define association using `has_one_nested` macro:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class User < ActiveRecord::Base
|
37
|
+
include NestedRecord::Macro
|
38
|
+
|
39
|
+
has_one_nested :profile
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
Or you can include the `Macro` globally:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
class ApplicationRecord < ActiveRecord::Base
|
47
|
+
include NestedRecord::Macro
|
48
|
+
end
|
49
|
+
|
50
|
+
class User < ApplicationRecord
|
51
|
+
has_one_nested :profile
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
Define nested record attributes using `ActiveModel::Attributes` API (since Rails 5.2):
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
class Profile < NestedRecord::Base
|
59
|
+
attribute :age, :integer
|
60
|
+
attribute :active, :boolean
|
61
|
+
has_one_nested :contacts
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
You can go deeper and define models on the next nesting level:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
class Profile::Contacts < NestedRecord::Base
|
69
|
+
attribute :email, :string
|
70
|
+
attribute :phone, :string
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
You can store **a collection** of objects with `has_many_nested`:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
class Profile::Contacts < NestedRecord::Base
|
78
|
+
attribute :email, :string
|
79
|
+
attribute :phone, :string
|
80
|
+
has_many_nested :socials
|
81
|
+
end
|
82
|
+
|
83
|
+
class Profile::Social < NestedRecord::Base
|
84
|
+
attribute :name
|
85
|
+
attribute :url
|
86
|
+
end
|
87
|
+
|
88
|
+
user.profile.age = 39
|
89
|
+
user.profile.contacts.email = 'john@doe.com'
|
90
|
+
user.profile.contacts.socials[0].name # => 'facebook'
|
91
|
+
```
|
92
|
+
|
93
|
+
You can assign attributes in the way like `accepts_nested_attributes_for` macros provides for AR models:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
user.profile_attributes = {
|
97
|
+
age: 39,
|
98
|
+
contacts_attributes: {
|
99
|
+
email: 'john@doe.com',
|
100
|
+
socials_attributes: [
|
101
|
+
{ name: 'facebook', url: 'facebook.example.com/johndoe' },
|
102
|
+
{ name: 'twitter', url: 'twitter.example.com/johndoe' }
|
103
|
+
]
|
104
|
+
}
|
105
|
+
}
|
106
|
+
```
|
26
107
|
|
27
108
|
## Development
|
28
109
|
|
@@ -32,7 +113,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
113
|
|
33
114
|
## Contributing
|
34
115
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
116
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/marshall-lee/nested_record.
|
36
117
|
|
37
118
|
## License
|
38
119
|
|
@@ -63,6 +63,10 @@ class NestedRecord::Collection
|
|
63
63
|
@ary.length
|
64
64
|
end
|
65
65
|
|
66
|
+
def size
|
67
|
+
@ary.size
|
68
|
+
end
|
69
|
+
|
66
70
|
def select!
|
67
71
|
return to_enum(:select!) unless block_given?
|
68
72
|
@ary.select!(&proc)
|
@@ -75,6 +79,12 @@ class NestedRecord::Collection
|
|
75
79
|
self
|
76
80
|
end
|
77
81
|
|
82
|
+
def sort_by!
|
83
|
+
return to_enum(:sort_by!) unless block_given?
|
84
|
+
@ary.sort_by!(&proc)
|
85
|
+
self
|
86
|
+
end
|
87
|
+
|
78
88
|
def reject_by!(attrs)
|
79
89
|
return to_enum(:reject_by!) unless block_given?
|
80
90
|
attrs = attrs.stringify_keys
|
data/lib/nested_record/setup.rb
CHANGED
@@ -4,7 +4,12 @@ class NestedRecord::Setup
|
|
4
4
|
def initialize(owner, name, **options, &extension)
|
5
5
|
@options = options
|
6
6
|
@owner = owner
|
7
|
-
|
7
|
+
if options[:class_name]
|
8
|
+
@record_class = options[:class_name]
|
9
|
+
else
|
10
|
+
@record_class = name.to_s.camelize
|
11
|
+
@record_class = @record_class.singularize if self.is_a?(HasMany)
|
12
|
+
end
|
8
13
|
@name = name
|
9
14
|
@extension = extension
|
10
15
|
|
@@ -24,4 +24,25 @@ RSpec.describe NestedRecord::Collection do
|
|
24
24
|
expect { collection << Bar.new }.not_to raise_error
|
25
25
|
end
|
26
26
|
end
|
27
|
+
|
28
|
+
describe '#sort_by!' do
|
29
|
+
it 'sorts collection' do
|
30
|
+
collection << Foo.new(id: 2)
|
31
|
+
collection << Foo.new(id: 1)
|
32
|
+
|
33
|
+
collection.sort_by!(&:id)
|
34
|
+
expect(collection.map(&:id)).to eq [1, 2]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#size' do
|
39
|
+
it 'works' do
|
40
|
+
expect(collection.size).to eq 0
|
41
|
+
|
42
|
+
collection << Foo.new(id: 2)
|
43
|
+
collection << Foo.new(id: 1)
|
44
|
+
|
45
|
+
expect(collection.size).to eq 2
|
46
|
+
end
|
47
|
+
end
|
27
48
|
end
|
data/spec/nested_record_spec.rb
CHANGED
@@ -37,6 +37,22 @@ RSpec.describe NestedRecord do
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
+
context 'with plural model name' do
|
41
|
+
nested_model(:Points) do
|
42
|
+
attribute :x, :string
|
43
|
+
attribute :y, :integer
|
44
|
+
attribute :z, :boolean
|
45
|
+
end
|
46
|
+
|
47
|
+
active_model(:Foo) do
|
48
|
+
has_one_nested :points
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'properly locates the model class' do
|
52
|
+
expect(Foo.new.build_points).to be_an_instance_of(Points)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
40
56
|
describe 'writer' do
|
41
57
|
it 'is defined' do
|
42
58
|
foo = Foo.new
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nested_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladimir Kochnev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|