mongoid-multiple-polymorphic 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/Gemfile +2 -0
- data/LICENSE.md +22 -0
- data/README.md +6 -0
- data/lib/mongoid-multiple-polymorphic.rb +6 -0
- data/lib/mongoid-multiple-polymorphic/mongoid/relations/binding.rb +40 -0
- data/lib/mongoid-multiple-polymorphic/mongoid/relations/bindings/referenced/in.rb +47 -0
- data/lib/mongoid-multiple-polymorphic/mongoid/relations/metadata.rb +30 -0
- data/lib/mongoid-multiple-polymorphic/mongoid/relations/polymorphic.rb +18 -0
- data/lib/mongoid-multiple-polymorphic/mongoid/relations/referenced/one.rb +24 -0
- data/mongoid-multiple-polymorphic.gemspec +13 -0
- data/spec/app/models/eye.rb +5 -0
- data/spec/app/models/face.rb +6 -0
- data/spec/mongoid-multiple-polymorphic/mongoid-multiple-polymorphic_spec.rb +17 -0
- data/spec/spec_helper.rb +49 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 423cecbf3a2ffa2076a3c1713f36c3ff8b3d9589
|
4
|
+
data.tar.gz: a6a7783c17749d2126016d97fe5862bcbf510a81
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3adabffc2c7015bd8c14969788a18726050ee37cccc82257418281ea2ad990cd02725821379a2de4bd92f535243918243616149df548a10bd811ea9d56491b5c
|
7
|
+
data.tar.gz: 3882091c6eaaf08007d29accf8fd8dbed8135b78e95d57f36443aaf416e89d9f99f054d6c3de7d4de933fcb01d4b56960cfd809c21350bbbb6b7fe2afc75a582
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Grantoo, LLC
|
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,6 @@
|
|
1
|
+
mongoid-multiple-polymorphic
|
2
|
+
============================
|
3
|
+
|
4
|
+
Support backward compatibility for multiple polymorphic relationships for mongoid 4
|
5
|
+
|
6
|
+
In mongoid 4, a model can no longer have more than 1 field with the same polymorphic class. This brings back the functionality to allow a smooth transition from mongoid 3 to mongoid 4.
|
@@ -0,0 +1,6 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
require 'mongoid-multiple-polymorphic/mongoid/relations/bindings/referenced/in'
|
3
|
+
require 'mongoid-multiple-polymorphic/mongoid/relations/referenced/one'
|
4
|
+
require 'mongoid-multiple-polymorphic/mongoid/relations/binding'
|
5
|
+
require 'mongoid-multiple-polymorphic/mongoid/relations/metadata'
|
6
|
+
require 'mongoid-multiple-polymorphic/mongoid/relations/polymorphic'
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Relations
|
3
|
+
class Binding
|
4
|
+
# Bind the inverse of field, when in a polymorphic relation.
|
5
|
+
#
|
6
|
+
# @api private
|
7
|
+
#
|
8
|
+
# @example Bind the inverse of field.
|
9
|
+
# binding.bind_inverse_of_field(doc)
|
10
|
+
#
|
11
|
+
# @param [ Document ] doc The document to bind.
|
12
|
+
# @param [ String ] name The name of the relation.
|
13
|
+
#
|
14
|
+
# @since 3.0.0
|
15
|
+
def bind_inverse_of_field(doc, name)
|
16
|
+
if inverse_metadata = metadata.inverse_metadata(doc)
|
17
|
+
if setter = inverse_metadata.inverse_of_field_setter
|
18
|
+
doc.you_must(setter, name)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def bind_from_relational_parent(doc)
|
24
|
+
check_inverse!(doc)
|
25
|
+
bind_foreign_key(doc, record_id(base))
|
26
|
+
bind_polymorphic_type(doc, base.class.name)
|
27
|
+
bind_inverse(doc, base)
|
28
|
+
bind_inverse_of_field(doc, metadata.name)
|
29
|
+
end
|
30
|
+
|
31
|
+
def unbind_from_relational_parent(doc)
|
32
|
+
check_inverse!(doc)
|
33
|
+
bind_foreign_key(doc, nil)
|
34
|
+
bind_polymorphic_type(doc, nil)
|
35
|
+
bind_inverse(doc, nil)
|
36
|
+
bind_inverse_of_field(doc, nil)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Relations
|
3
|
+
module Bindings
|
4
|
+
module Referenced
|
5
|
+
class In < Binding
|
6
|
+
def bind_one
|
7
|
+
binding do
|
8
|
+
check_inverses!(target)
|
9
|
+
bind_foreign_key(base, record_id(target))
|
10
|
+
bind_polymorphic_inverse_type(base, target.class.name)
|
11
|
+
if inverse = metadata.inverse(target)
|
12
|
+
if set_base_metadata
|
13
|
+
bind_inverse_of_field(base, base.metadata_name)
|
14
|
+
if base.referenced_many?
|
15
|
+
target.__send__(inverse).push(base)
|
16
|
+
else
|
17
|
+
target.set_relation(inverse, base)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def unbind_one
|
25
|
+
binding do
|
26
|
+
inverse = metadata.inverse(target)
|
27
|
+
if !inverse && metadata.inverse_of_field
|
28
|
+
inverse = base.__send__(metadata.inverse_of_field)
|
29
|
+
end
|
30
|
+
bind_foreign_key(base, nil)
|
31
|
+
bind_polymorphic_inverse_type(base, nil)
|
32
|
+
bind_inverse_of_field(base, nil)
|
33
|
+
if inverse
|
34
|
+
set_base_metadata
|
35
|
+
if base.referenced_many?
|
36
|
+
target.__send__(inverse).delete(base)
|
37
|
+
else
|
38
|
+
target.set_relation(inverse, nil)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Relations
|
3
|
+
class Metadata < Hash
|
4
|
+
# Returns the name of the field in which to store the name of the inverse
|
5
|
+
# field for the polymorphic relation.
|
6
|
+
#
|
7
|
+
# @example Get the name of the field.
|
8
|
+
# metadata.inverse_of_field
|
9
|
+
#
|
10
|
+
# @return [ String ] The name of the field for storing the name of the
|
11
|
+
# inverse field.
|
12
|
+
#
|
13
|
+
# @since 2.4.5
|
14
|
+
def inverse_of_field
|
15
|
+
@inverse_of_field ||= determine_inverse_for(:field)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Gets the setter for the field that stores the name of the inverse field
|
19
|
+
# on a polymorphic relation.
|
20
|
+
#
|
21
|
+
# @example Get the inverse type setter.
|
22
|
+
# metadata.inverse_of_field_setter
|
23
|
+
#
|
24
|
+
# @return [ String ] The name of the setter.
|
25
|
+
def inverse_of_field_setter
|
26
|
+
@inverse_of_field_setter ||= inverse_of_field.__setter__
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Relations
|
3
|
+
module Polymorphic
|
4
|
+
module ClassMethods
|
5
|
+
def polymorph(metadata)
|
6
|
+
if metadata.polymorphic?
|
7
|
+
self.polymorphic = true
|
8
|
+
if metadata.relation.stores_foreign_key?
|
9
|
+
field(metadata.inverse_type, type: String)
|
10
|
+
field(metadata.inverse_of_field, type: Symbol)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
self
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Relations
|
3
|
+
module Referenced
|
4
|
+
class One < Relations::One
|
5
|
+
|
6
|
+
private
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def criteria(metadata, object, type = nil)
|
10
|
+
crit = metadata.klass.where(metadata.foreign_key => object)
|
11
|
+
if metadata.polymorphic?
|
12
|
+
crit = crit.where(metadata.type => type.name)
|
13
|
+
end
|
14
|
+
inverse_metadata = metadata.inverse_metadata(metadata.klass)
|
15
|
+
if inverse_metadata.inverse_of_field
|
16
|
+
crit = crit.any_in(inverse_metadata.inverse_of_field => [metadata.name, nil])
|
17
|
+
end
|
18
|
+
crit
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'mongoid-multiple-polymorphic'
|
3
|
+
s.version = '0.0.0'
|
4
|
+
s.summary = "Support backward compatibility for multiple polymorphic relationships for mongoid 4"
|
5
|
+
s.description = "In mongoid 4, a model can no longer have more than 1 field with the same polymorphic class. This brings back the functionality to allow a smooth transition from mongoid 3 to mongoid 4."
|
6
|
+
s.authors = ["Kirk Chen"]
|
7
|
+
s.email = 'chen.kirk@gmail.com'
|
8
|
+
s.files = `git ls-files`.split("\n")
|
9
|
+
s.homepage = 'http://rubygems.org/gems/mongoid-multiple-polymorphic'
|
10
|
+
s.license = 'MIT'
|
11
|
+
|
12
|
+
s.add_runtime_dependency 'mongoid', '~> 4.0'
|
13
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Multiple polymorphic support" do
|
4
|
+
it "should retrieve the right documents" do
|
5
|
+
face = Face.create!
|
6
|
+
face.left_eye = Eye.create!(:pupil_dilation => 3)
|
7
|
+
face.right_eye = Eye.create!(:pupil_dilation => 5)
|
8
|
+
left_eye_id = face.left_eye.id
|
9
|
+
right_eye_id = face.right_eye.id
|
10
|
+
face.save!
|
11
|
+
face.reload
|
12
|
+
expect(face.left_eye.pupil_dilation).to be(3)
|
13
|
+
expect(face.right_eye.pupil_dilation).to be(5)
|
14
|
+
expect(face.left_eye.id).to eq(left_eye_id)
|
15
|
+
expect(face.right_eye.id).to eq(right_eye_id)
|
16
|
+
end
|
17
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
3
|
+
|
4
|
+
MODELS = File.join(File.dirname(__FILE__), "app/models")
|
5
|
+
$LOAD_PATH.unshift(MODELS)
|
6
|
+
|
7
|
+
require "mongoid-multiple-polymorphic"
|
8
|
+
require "rspec"
|
9
|
+
|
10
|
+
# These environment variables can be set if wanting to test against a database
|
11
|
+
# that is not on the local machine.
|
12
|
+
ENV["MONGOID_SPEC_HOST"] ||= "localhost"
|
13
|
+
ENV["MONGOID_SPEC_PORT"] ||= "27017"
|
14
|
+
|
15
|
+
# These are used when creating any connection in the test suite.
|
16
|
+
HOST = ENV["MONGOID_SPEC_HOST"]
|
17
|
+
PORT = ENV["MONGOID_SPEC_PORT"].to_i
|
18
|
+
|
19
|
+
# Moped.logger.level = Logger::DEBUG
|
20
|
+
# Mongoid.logger.level = Logger::DEBUG
|
21
|
+
|
22
|
+
CONFIG = {
|
23
|
+
sessions: {
|
24
|
+
default: {
|
25
|
+
database: "mongoid_multiple_polymorphic_test",
|
26
|
+
hosts: [ "#{HOST}:#{PORT}" ]
|
27
|
+
}
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
# Set the database that the spec suite connects to.
|
32
|
+
Mongoid.configure do |config|
|
33
|
+
config.load_configuration(CONFIG)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Autoload every model for the test suite that sits in spec/app/models.
|
37
|
+
Dir[ File.join(MODELS, "*.rb") ].sort.each do |file|
|
38
|
+
name = File.basename(file, ".rb")
|
39
|
+
autoload name.camelize.to_sym, name
|
40
|
+
end
|
41
|
+
|
42
|
+
RSpec.configure do |config|
|
43
|
+
config.raise_errors_for_deprecations!
|
44
|
+
|
45
|
+
# Drop all collections and clear the identity map before each spec.
|
46
|
+
config.before(:each) do
|
47
|
+
Mongoid.purge!
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-multiple-polymorphic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kirk Chen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mongoid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
description: In mongoid 4, a model can no longer have more than 1 field with the same
|
28
|
+
polymorphic class. This brings back the functionality to allow a smooth transition
|
29
|
+
from mongoid 3 to mongoid 4.
|
30
|
+
email: chen.kirk@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- .gitignore
|
36
|
+
- Gemfile
|
37
|
+
- LICENSE.md
|
38
|
+
- README.md
|
39
|
+
- lib/mongoid-multiple-polymorphic.rb
|
40
|
+
- lib/mongoid-multiple-polymorphic/mongoid/relations/binding.rb
|
41
|
+
- lib/mongoid-multiple-polymorphic/mongoid/relations/bindings/referenced/in.rb
|
42
|
+
- lib/mongoid-multiple-polymorphic/mongoid/relations/metadata.rb
|
43
|
+
- lib/mongoid-multiple-polymorphic/mongoid/relations/polymorphic.rb
|
44
|
+
- lib/mongoid-multiple-polymorphic/mongoid/relations/referenced/one.rb
|
45
|
+
- mongoid-multiple-polymorphic.gemspec
|
46
|
+
- spec/app/models/eye.rb
|
47
|
+
- spec/app/models/face.rb
|
48
|
+
- spec/mongoid-multiple-polymorphic/mongoid-multiple-polymorphic_spec.rb
|
49
|
+
- spec/spec_helper.rb
|
50
|
+
homepage: http://rubygems.org/gems/mongoid-multiple-polymorphic
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 2.2.1
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: Support backward compatibility for multiple polymorphic relationships for
|
74
|
+
mongoid 4
|
75
|
+
test_files: []
|