active_record-json_has_many 0.2.0 → 0.3.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: ae2083de42d457a0126dfae281028cf4160ec7b3
4
- data.tar.gz: e9150e43a9833bbedacdc28303ad70e586fd107c
3
+ metadata.gz: e953ea9f7439ad1d939a2c065d03e5510e04c65d
4
+ data.tar.gz: b0a96975541b75458b1e9837c1cc91c53ac57ef9
5
5
  SHA512:
6
- metadata.gz: fd9f6774aa2a44b6596db207a3b157dd457a71b0b21b6fc4ebe77c17f761b376e5bcd19636d70826c0e6b3e03cf282b25862865b29f379dba8b173eedbf5895b
7
- data.tar.gz: 0fda3ea696b950060de99a9f2ef91d713653bcc8d72487a5f4fc64672f04dfeae14eeadfa080fe53275b4f0c9be0dd7d3a5f00e24227e10fdac500a0808b6236
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
- ## Installation
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
- Or install it yourself as:
10
+ ```ruby
11
+ require "active_record/json_has_many"
19
12
 
20
- $ gem install active_record-json_has_many
13
+ ActiveRecord::Schema.define do
14
+ create_table :parents do |t|
15
+ t.text :child_ids
16
+ end
21
17
 
22
- ## Usage
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
@@ -25,5 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "rake"
26
26
  spec.add_development_dependency "rspec"
27
27
  spec.add_development_dependency "sqlite3"
28
+ spec.add_development_dependency "byebug"
28
29
  end
29
30
 
@@ -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 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
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
- end
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
 
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module JsonHasMany
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -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.string :child_ids
12
- t.string :fuzzy_ids
11
+ t.text :child_ids
12
+ t.text :fuzzy_ids
13
13
  end
14
14
 
15
- create_table :children do |t|
16
- end
15
+ create_table :children
17
16
 
18
- create_table :pets do |t|
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.2.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