nested_record 0.1.0 → 0.1.1

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: 8e0e28260ca8d52235022d6ebcb471d6ea5cb585ebe4391bb4bc76ef47166d8a
4
- data.tar.gz: 73413b02eb3c334060f3098254e53c0795546ca1875df2874a7f99a8f4a5c9b0
3
+ metadata.gz: f4736181e42c531aad96202018bb79f79ec46aa69a0cf03a0a991abead29f4af
4
+ data.tar.gz: 7aa80b14123c4c8b1e8175d9ff5d187b6918d31ff0f1521a394ebd061d7b14cf
5
5
  SHA512:
6
- metadata.gz: c665c138f57fed85d36071e6decb469e3e5488203cac1f604920f61978ddab323e2be3c16d689c8ba01db514ab04d072528531e1d59bcbadab086eeabe3dec13
7
- data.tar.gz: cd2bda40861ee824ba6adae759d03fdb0582b4c49b1f18c39fbcdb3f649864be1e41e8c834a9eddaa2a16970dd807724d5510f00b921ad9472a796b733338453
6
+ metadata.gz: f02b179664dc676bd35d4e6087194f714d58d56cf88058ea7d739be48e34ec330a1df0a2dd197eb2ed58579757331af7d13b6133ceae5a768f6b07773be0c32b
7
+ data.tar.gz: cffc632808753b0ecc39e2400328c42776f7a503e77f7373085438003c4efc3907d5d4a426737660875a611b22c0ce860f8eeb41b79edb1dd921dbafb7b11538
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nested_record (0.1.0)
4
+ nested_record (0.1.1)
5
5
  rails (~> 5.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # NestedRecord
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/nested_record`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
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/[USERNAME]/nested_record.
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
@@ -4,7 +4,12 @@ class NestedRecord::Setup
4
4
  def initialize(owner, name, **options, &extension)
5
5
  @options = options
6
6
  @owner = owner
7
- @record_class = options[:class_name] || name.to_s.classify
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NestedRecord
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
@@ -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
@@ -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.0
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-06-25 00:00:00.000000000 Z
11
+ date: 2019-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler