active_record-json_has_many 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -0
- data/README.md +9 -2
- data/Rakefile +5 -0
- data/lib/active_record/json_has_many.rb +19 -11
- data/lib/active_record/json_has_many/version.rb +1 -1
- data/spec/json_has_many_spec.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae2083de42d457a0126dfae281028cf4160ec7b3
|
4
|
+
data.tar.gz: e9150e43a9833bbedacdc28303ad70e586fd107c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd9f6774aa2a44b6596db207a3b157dd457a71b0b21b6fc4ebe77c17f761b376e5bcd19636d70826c0e6b3e03cf282b25862865b29f379dba8b173eedbf5895b
|
7
|
+
data.tar.gz: 0fda3ea696b950060de99a9f2ef91d713653bcc8d72487a5f4fc64672f04dfeae14eeadfa080fe53275b4f0c9be0dd7d3a5f00e24227e10fdac500a0808b6236
|
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# ActiveRecord::JsonHasMany
|
2
2
|
|
3
|
-
|
3
|
+
[![Build Status](https://travis-ci.org/botandrose/active_record-json_has_many.svg)](https://travis-ci.org/botandrose/active_record-json_has_many)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/botandrose/active_record-json_has_many/badges/gpa.svg)](https://codeclimate.com/github/botandrose/active_record-json_has_many)
|
5
|
+
|
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.
|
4
7
|
|
5
8
|
## Installation
|
6
9
|
|
@@ -20,7 +23,7 @@ Or install it yourself as:
|
|
20
23
|
|
21
24
|
```ruby
|
22
25
|
class Parent < ActiveRecord::Base
|
23
|
-
|
26
|
+
json_has_many :children
|
24
27
|
end
|
25
28
|
```
|
26
29
|
|
@@ -32,6 +35,10 @@ parent.child_ids #=> [1,2,3]
|
|
32
35
|
parent.children => [<Child id=1>, <Child id=2>, Child id=3>]
|
33
36
|
```
|
34
37
|
|
38
|
+
## Requirements
|
39
|
+
|
40
|
+
* Ruby 2.0+
|
41
|
+
|
35
42
|
## Contributing
|
36
43
|
|
37
44
|
1. Fork it ( https://github.com/[my-github-username]/active_record-json_has_many/fork )
|
data/Rakefile
CHANGED
@@ -4,20 +4,28 @@ require "json"
|
|
4
4
|
|
5
5
|
module ActiveRecord
|
6
6
|
module JsonHasMany
|
7
|
-
def json_has_many(
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
7
|
+
def json_has_many(many, class_name: nil)
|
8
|
+
one = many.to_s.singularize
|
9
|
+
one_ids = :"#{one}_ids"
|
10
|
+
class_name ||= one.classify
|
11
|
+
|
12
|
+
serialize one_ids, JSON
|
13
|
+
|
14
|
+
include instance_methods(one_ids, many, class_name)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def instance_methods one_ids, many, class_name
|
20
|
+
Module.new do
|
21
|
+
define_method one_ids do
|
22
|
+
super() || []
|
15
23
|
end
|
16
24
|
|
17
|
-
|
18
|
-
|
25
|
+
define_method many do
|
26
|
+
class_name.constantize.where(id: send(one_ids))
|
19
27
|
end
|
20
|
-
|
28
|
+
end
|
21
29
|
end
|
22
30
|
end
|
23
31
|
|
data/spec/json_has_many_spec.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Micah Geisel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -102,6 +102,7 @@ extensions: []
|
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
104
|
- ".gitignore"
|
105
|
+
- ".travis.yml"
|
105
106
|
- Gemfile
|
106
107
|
- LICENSE.txt
|
107
108
|
- README.md
|