active_record-json_has_many 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1e8b17732f5551ec1211c1f72f1a4ef94028b6f5
4
+ data.tar.gz: 80f89023027d97ebf8c7efafc53f6798697e1a3f
5
+ SHA512:
6
+ metadata.gz: eb7946443e7a7df82b722d56b15518eef11db88f1c3852f3e4f722622c3eec84e3b9c6749e90f02cc91cbd90dcca18776dcc37e296c591d62596eebcf8af3feb
7
+ data.tar.gz: d1b993233a2f8709170c3520efbf4fc5c9d9b704cd06534e3b33394ea8f4f64f692e31475b6e267a48891362f9a2a045e83034c78e67809fe908b54bb8d48ae9
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .ruby-version
7
+ .ruby-gemset
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
20
+ *.bundle
21
+ *.so
22
+ *.o
23
+ *.a
24
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_record-json_has_many.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Micah Geisel
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # ActiveRecord::JsonHasMany
2
+
3
+ Instead of a many-to-many join table, serialize the ids into a JSON array.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'active_record-json_has_many'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install active_record-json_has_many
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ class Parent < ActiveRecord::Base
23
+ has_many_json :children, class_name: "Child"
24
+ end
25
+ ```
26
+
27
+ This will add some methods:
28
+
29
+ ```ruby
30
+ parent.child_ids = [1,2,3]
31
+ parent.child_ids #=> [1,2,3]
32
+ parent.children => [<Child id=1>, <Child id=2>, Child id=3>]
33
+ ```
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it ( https://github.com/[my-github-username]/active_record-json_has_many/fork )
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create a new Pull Request
42
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record/json_has_many/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_record-json_has_many"
8
+ spec.version = ActiveRecord::JsonHasMany::VERSION
9
+ spec.authors = ["Micah Geisel"]
10
+ spec.email = ["micah@botandrose.com"]
11
+ spec.summary = %q{Instead of a many-to-many join table, serialize the ids into a JSON array.}
12
+ spec.description = %q{Instead of a many-to-many join table, serialize the ids into a JSON array.}
13
+ spec.homepage = "https://github.com/botandrose/active_record-json_has_many"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord"
22
+ spec.add_dependency "json"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "sqlite3"
28
+ end
29
+
@@ -0,0 +1,26 @@
1
+ require "active_record/json_has_many/version"
2
+ require "active_record"
3
+ require "json"
4
+
5
+ module ActiveRecord
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 || []
15
+ end
16
+
17
+ def #{field}
18
+ #{class_name}.where(id: #{singularized_field}_ids)
19
+ end
20
+ RUBY
21
+ end
22
+ end
23
+
24
+ Base.extend JsonHasMany
25
+ end
26
+
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module JsonHasMany
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,73 @@
1
+ require "active_record/json_has_many"
2
+
3
+ describe ActiveRecord::JsonHasMany do
4
+ before do
5
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
6
+
7
+ silence_stream(STDOUT) do
8
+ ActiveRecord::Schema.define do
9
+ create_table :parents do |t|
10
+ t.string :child_ids
11
+ t.string :fuzzy_ids
12
+ end
13
+
14
+ create_table :children do |t|
15
+ end
16
+
17
+ create_table :pets do |t|
18
+ end
19
+ end
20
+ end
21
+
22
+ class Parent < ActiveRecord::Base
23
+ json_has_many :children
24
+ json_has_many :fuzzies, class_name: "Pet"
25
+ end
26
+
27
+ class Child < ActiveRecord::Base
28
+ end
29
+
30
+ class Pet < ActiveRecord::Base
31
+ end
32
+ end
33
+
34
+ subject { Parent.new }
35
+
36
+ describe "#child_ids" do
37
+ it "is empty by default" do
38
+ expect(subject.child_ids).to eq []
39
+ end
40
+
41
+ it "is an accessor" do
42
+ subject.child_ids = [1,2,3]
43
+ expect(subject.child_ids).to eq [1,2,3]
44
+ end
45
+ end
46
+
47
+ describe "#children" do
48
+ let(:children) { [Child.create!, Child.create!, Child.create!] }
49
+
50
+ it "returns an empty array when there are no children" do
51
+ expect(subject.children).to eq []
52
+ end
53
+
54
+ it "finds the children by id" do
55
+ subject.child_ids = [1,2,3]
56
+ expect(subject.children).to eq children
57
+ end
58
+ end
59
+
60
+ context "when overriding class name" do
61
+ let(:pets) { [Pet.create!, Pet.create!, Pet.create!] }
62
+
63
+ it "returns an empty array when there are no children" do
64
+ expect(subject.fuzzies).to eq []
65
+ end
66
+
67
+ it "finds the children by id" do
68
+ subject.fuzzy_ids = [1,2,3]
69
+ expect(subject.fuzzies).to eq pets
70
+ end
71
+ end
72
+ end
73
+
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record-json_has_many
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Micah Geisel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Instead of a many-to-many join table, serialize the ids into a JSON array.
98
+ email:
99
+ - micah@botandrose.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - active_record-json_has_many.gemspec
110
+ - lib/active_record/json_has_many.rb
111
+ - lib/active_record/json_has_many/version.rb
112
+ - spec/json_has_many_spec.rb
113
+ homepage: https://github.com/botandrose/active_record-json_has_many
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.2.2
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Instead of a many-to-many join table, serialize the ids into a JSON array.
137
+ test_files:
138
+ - spec/json_has_many_spec.rb