active_record-json_has_many 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -15
- data/active_record-json_has_many.gemspec +1 -0
- data/lib/active_record/json_has_many.rb +8 -8
- data/lib/active_record/json_has_many/version.rb +1 -1
- data/spec/json_has_many_spec.rb +9 -6
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e953ea9f7439ad1d939a2c065d03e5510e04c65d
|
4
|
+
data.tar.gz: b0a96975541b75458b1e9837c1cc91c53ac57ef9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10bb7e01b55aadeb1ed1e9fcc95a5ffe453a1bc6a57a0a0a05cfceb074cad179ff8ed81e0ad88a4cf5d3a3657b2aa7042e561529541518fe137f0f9c155477f9
|
7
|
+
data.tar.gz: 9e8a1cd7323123abb4d1cb3d224e5457a9d57878cf7bcbb2c719e3f9eccba9c6f92cd0caef102b44627292cdce475afd2f7e2ea937fc980ba6571c65dbe16f56
|
data/README.md
CHANGED
@@ -5,34 +5,32 @@
|
|
5
5
|
|
6
6
|
Instead of keeping the foreign keys on the children, or in a many-to-many join table, let's keep them in a JSON array on the parent.
|
7
7
|
|
8
|
-
##
|
9
|
-
|
10
|
-
Add this line to your application's Gemfile:
|
11
|
-
|
12
|
-
gem 'active_record-json_has_many'
|
13
|
-
|
14
|
-
And then execute:
|
15
|
-
|
16
|
-
$ bundle
|
8
|
+
## Usage
|
17
9
|
|
18
|
-
|
10
|
+
```ruby
|
11
|
+
require "active_record/json_has_many"
|
19
12
|
|
20
|
-
|
13
|
+
ActiveRecord::Schema.define do
|
14
|
+
create_table :parents do |t|
|
15
|
+
t.text :child_ids
|
16
|
+
end
|
21
17
|
|
22
|
-
|
18
|
+
create_table :children
|
19
|
+
end
|
23
20
|
|
24
|
-
```ruby
|
25
21
|
class Parent < ActiveRecord::Base
|
26
22
|
json_has_many :children
|
27
23
|
end
|
28
24
|
```
|
29
25
|
|
30
|
-
This will add some methods:
|
26
|
+
This will add some familiar `has_many`-style methods:
|
31
27
|
|
32
28
|
```ruby
|
29
|
+
parent.children = [Child.create!, Child.create!, Child.create!]
|
30
|
+
parent.children #=> [#<Child id: 1>, #<Child id: 2>, #<Child id: 3>]
|
31
|
+
|
33
32
|
parent.child_ids = [1,2,3]
|
34
33
|
parent.child_ids #=> [1,2,3]
|
35
|
-
parent.children => [<Child id=1>, <Child id=2>, Child id=3>]
|
36
34
|
```
|
37
35
|
|
38
36
|
## Requirements
|
@@ -7,17 +7,13 @@ module ActiveRecord
|
|
7
7
|
def json_has_many(many, class_name: nil)
|
8
8
|
one = many.to_s.singularize
|
9
9
|
one_ids = :"#{one}_ids"
|
10
|
+
one_ids_equals = :"#{one_ids}="
|
10
11
|
class_name ||= one.classify
|
12
|
+
many_equals = :"#{many}="
|
11
13
|
|
12
14
|
serialize one_ids, JSON
|
13
15
|
|
14
|
-
include
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def instance_methods one_ids, many, class_name
|
20
|
-
Module.new do
|
16
|
+
include Module.new {
|
21
17
|
define_method one_ids do
|
22
18
|
super() || []
|
23
19
|
end
|
@@ -25,7 +21,11 @@ module ActiveRecord
|
|
25
21
|
define_method many do
|
26
22
|
class_name.constantize.where(id: send(one_ids))
|
27
23
|
end
|
28
|
-
|
24
|
+
|
25
|
+
define_method many_equals do |collection|
|
26
|
+
send one_ids_equals, collection.map(&:id)
|
27
|
+
end
|
28
|
+
}
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
data/spec/json_has_many_spec.rb
CHANGED
@@ -8,15 +8,13 @@ describe ActiveRecord::JsonHasMany do
|
|
8
8
|
silence_stream(STDOUT) do
|
9
9
|
ActiveRecord::Schema.define do
|
10
10
|
create_table :parents do |t|
|
11
|
-
t.
|
12
|
-
t.
|
11
|
+
t.text :child_ids
|
12
|
+
t.text :fuzzy_ids
|
13
13
|
end
|
14
14
|
|
15
|
-
create_table :children
|
16
|
-
end
|
15
|
+
create_table :children
|
17
16
|
|
18
|
-
create_table :pets
|
19
|
-
end
|
17
|
+
create_table :pets
|
20
18
|
end
|
21
19
|
end
|
22
20
|
|
@@ -56,6 +54,11 @@ describe ActiveRecord::JsonHasMany do
|
|
56
54
|
subject.child_ids = [1,2,3]
|
57
55
|
expect(subject.children).to eq children
|
58
56
|
end
|
57
|
+
|
58
|
+
it "is an accessor" do
|
59
|
+
subject.children = children
|
60
|
+
expect(subject.children).to eq children
|
61
|
+
end
|
59
62
|
end
|
60
63
|
|
61
64
|
context "when overriding class name" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record-json_has_many
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Micah Geisel
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: byebug
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
97
111
|
description: Instead of a many-to-many join table, serialize the ids into a JSON array.
|
98
112
|
email:
|
99
113
|
- micah@botandrose.com
|