active_record-json_has_many 0.1.0 → 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
  SHA1:
3
- metadata.gz: 1e8b17732f5551ec1211c1f72f1a4ef94028b6f5
4
- data.tar.gz: 80f89023027d97ebf8c7efafc53f6798697e1a3f
3
+ metadata.gz: ae2083de42d457a0126dfae281028cf4160ec7b3
4
+ data.tar.gz: e9150e43a9833bbedacdc28303ad70e586fd107c
5
5
  SHA512:
6
- metadata.gz: eb7946443e7a7df82b722d56b15518eef11db88f1c3852f3e4f722622c3eec84e3b9c6749e90f02cc91cbd90dcca18776dcc37e296c591d62596eebcf8af3feb
7
- data.tar.gz: d1b993233a2f8709170c3520efbf4fc5c9d9b704cd06534e3b33394ea8f4f64f692e31475b6e267a48891362f9a2a045e83034c78e67809fe908b54bb8d48ae9
6
+ metadata.gz: fd9f6774aa2a44b6596db207a3b157dd457a71b0b21b6fc4ebe77c17f761b376e5bcd19636d70826c0e6b3e03cf282b25862865b29f379dba8b173eedbf5895b
7
+ data.tar.gz: 0fda3ea696b950060de99a9f2ef91d713653bcc8d72487a5f4fc64672f04dfeae14eeadfa080fe53275b4f0c9be0dd7d3a5f00e24227e10fdac500a0808b6236
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.0
5
+
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # ActiveRecord::JsonHasMany
2
2
 
3
- Instead of a many-to-many join table, serialize the ids into a JSON array.
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
- has_many_json :children, class_name: "Child"
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
@@ -1,2 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
2
7
 
@@ -4,20 +4,28 @@ require "json"
4
4
 
5
5
  module ActiveRecord
6
6
  module JsonHasMany
7
- def json_has_many(field, class_name: nil)
8
- singularized_field = field.to_s.singularize.to_sym
9
- class_name ||= singularized_field.to_s.classify
10
- serialize :"#{singularized_field}_ids", JSON
11
-
12
- class_eval <<-RUBY
13
- def #{singularized_field}_ids
14
- super || []
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
- def #{field}
18
- #{class_name}.where(id: #{singularized_field}_ids)
25
+ define_method many do
26
+ class_name.constantize.where(id: send(one_ids))
19
27
  end
20
- RUBY
28
+ end
21
29
  end
22
30
  end
23
31
 
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module JsonHasMany
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -1,4 +1,5 @@
1
1
  require "active_record/json_has_many"
2
+ require "byebug"
2
3
 
3
4
  describe ActiveRecord::JsonHasMany do
4
5
  before do
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.1.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-28 00:00:00.000000000 Z
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