mongoid-included 0.0.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongoid-included.rb +0 -2
- data/lib/mongoid-included/document_inclusion.rb +36 -33
- data/lib/mongoid-included/embedded_name.rb +4 -1
- data/lib/mongoid-included/inclusion_errors.rb +6 -0
- data/lib/mongoid-included/version.rb +1 -1
- data/mongoid-included.gemspec +0 -1
- data/spec/models/invoice.rb +1 -1
- data/spec/models/invoice/item.rb +0 -1
- data/spec/models/invoice/not_included.rb +3 -0
- data/spec/models/invoice/user.rb +0 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/unit/included_spec.rb +32 -96
- data/spec/unit/including_spec.rb +68 -0
- metadata +13 -19
data/lib/mongoid-included.rb
CHANGED
@@ -1,69 +1,72 @@
|
|
1
|
+
require 'mongoid-included/embedded_name'
|
2
|
+
require 'mongoid-included/inclusion_errors'
|
1
3
|
module Mongoid
|
2
4
|
module DocumentInclusion
|
3
5
|
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
class NotMongoidDocument < StandardError; end;
|
6
|
-
class DocumentAlreadyIncluded < StandardError; end;
|
7
6
|
|
8
7
|
included do
|
9
|
-
|
10
|
-
cattr_accessor :
|
8
|
+
cattr_accessor :included_by
|
9
|
+
cattr_accessor :including_many
|
10
|
+
cattr_accessor :including_one
|
11
11
|
end
|
12
12
|
|
13
13
|
module ClassMethods
|
14
14
|
def included_in(_model, args = {})
|
15
|
-
raise NotMongoidDocument, "
|
16
|
-
raise
|
17
|
-
raise DocumentAlreadyIncluded, "Child document already included" if inclusion_model
|
15
|
+
raise NotMongoidDocument, "Parent must be Mongoid::Document" unless _mongoid_document? self.parent
|
16
|
+
raise DocumentAlreadyIncluded, "Document already included" if included_by.present?
|
18
17
|
embedded_in _model, args
|
19
|
-
|
18
|
+
|
19
|
+
self.included_by = _model
|
20
|
+
self.extend ActiveModel::Naming
|
21
|
+
_overwrite_model_name
|
20
22
|
end
|
21
23
|
|
22
24
|
def includes_many(_model, args = {})
|
23
|
-
|
24
|
-
embeds_many _model, args.merge(:class_name =>
|
25
|
-
self.
|
25
|
+
_verify_dependencies(_model)
|
26
|
+
embeds_many _model, args.merge(:class_name => _included_klass(_model))
|
27
|
+
(self.including_many ||= []) << _model
|
26
28
|
end
|
27
29
|
|
28
30
|
def includes_one(_model, args = {})
|
29
|
-
|
30
|
-
embeds_one _model, args.merge(:class_name =>
|
31
|
-
self.
|
31
|
+
_verify_dependencies(_model)
|
32
|
+
embeds_one _model, args.merge(:class_name => _included_klass(_model))
|
33
|
+
(self.including_one ||= []) << _model
|
32
34
|
end
|
33
35
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
private
|
37
|
+
|
38
|
+
def _overwrite_model_name
|
39
|
+
self.class_eval <<-EOF
|
40
|
+
@_model_name ||=begin
|
41
|
+
if self.parent != Object
|
42
|
+
Mongoid::EmbeddedName.new(self, self.parent)
|
43
|
+
else
|
44
|
+
super
|
45
|
+
end
|
40
46
|
end
|
41
|
-
|
47
|
+
EOF
|
42
48
|
end
|
43
49
|
|
44
|
-
|
45
|
-
|
46
|
-
def model_klass_name(_model)
|
50
|
+
def _model_klass_name(_model)
|
47
51
|
_model.to_s.classify
|
48
52
|
end
|
49
53
|
|
50
|
-
def
|
54
|
+
def _model_klass(_model)
|
51
55
|
_model.to_s.classify.constantize
|
52
56
|
end
|
53
57
|
|
54
|
-
def
|
55
|
-
"#{self}::#{
|
58
|
+
def _included_klass(_model)
|
59
|
+
"#{self}::#{_model_klass_name(_model)}"
|
56
60
|
end
|
57
61
|
|
58
|
-
def
|
62
|
+
def _mongoid_document?(_model)
|
59
63
|
_model.ancestors.include?(Mongoid::Document)
|
60
64
|
end
|
61
65
|
|
62
|
-
def
|
63
|
-
raise NotMongoidDocument,
|
64
|
-
raise NotMongoidDocument, "Child document must include Mongoid::Document" unless mongoid_document? model_klass(included_klass(_model))
|
66
|
+
def _verify_dependencies(_model)
|
67
|
+
raise NotMongoidDocument, "Child must be Mongoid::Document" unless _mongoid_document? _model_klass(_included_klass(_model))
|
65
68
|
end
|
66
|
-
|
67
69
|
end
|
68
70
|
end
|
69
71
|
end
|
72
|
+
Mongoid::Document.send(:include, ::Mongoid::DocumentInclusion)
|
@@ -5,7 +5,6 @@ module Mongoid
|
|
5
5
|
|
6
6
|
def initialize(klass, namespace, pluralize_namespace = true)
|
7
7
|
name ||= klass.name
|
8
|
-
# super(name)
|
9
8
|
@unnamespaced = name.sub(/^#{namespace.name}::/, '') if namespace
|
10
9
|
|
11
10
|
@klass = klass
|
@@ -21,5 +20,9 @@ module Mongoid
|
|
21
20
|
@route_key = ActiveSupport::Inflector.pluralize(@param_key).freeze
|
22
21
|
@i18n_key = name.underscore.to_sym
|
23
22
|
end
|
23
|
+
|
24
|
+
def _singularize(string, replacement='_')
|
25
|
+
ActiveSupport::Inflector.underscore(string).tr('/', replacement)
|
26
|
+
end
|
24
27
|
end
|
25
28
|
end
|
data/mongoid-included.gemspec
CHANGED
@@ -19,7 +19,6 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
21
|
|
22
|
-
s.add_runtime_dependency("activemodel", ["~> 3.1.0.beta1"])
|
23
22
|
s.add_runtime_dependency(%q<mongoid>, ["~> 2.0"])
|
24
23
|
s.add_development_dependency("rspec", ["~> 2.6.0"])
|
25
24
|
s.add_development_dependency("bson_ext", ["~> 1.3"])
|
data/spec/models/invoice.rb
CHANGED
data/spec/models/invoice/item.rb
CHANGED
data/spec/models/invoice/user.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
data/spec/unit/included_spec.rb
CHANGED
@@ -1,115 +1,51 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
Invoice.relations["user"].inverse_of.should == :invoice
|
8
|
-
end
|
9
|
-
|
10
|
-
it "embeds many children with given association name" do
|
11
|
-
Invoice.relations.keys.include? "items"
|
12
|
-
Invoice.relations["items"].macro.should == :embeds_many
|
13
|
-
end
|
14
|
-
|
15
|
-
it "embeds one child with given association name" do
|
16
|
-
Invoice.relations.keys.include? "item"
|
17
|
-
Invoice.relations["user"].macro.should == :embeds_one
|
18
|
-
end
|
19
|
-
|
20
|
-
it "embeds child with given association class" do
|
21
|
-
Invoice.relations["items"].class_name.should == "Invoice::Item"
|
22
|
-
end
|
23
|
-
|
24
|
-
it "issues an error if parent is not mongoid document" do
|
25
|
-
Object.const_set "NonMongoidDocument", Class.new
|
26
|
-
NonMongoidDocument.send(:include, Mongoid::DocumentInclusion)
|
27
|
-
NonMongoidDocument.const_set "NonMongoidEmbed", Class.new
|
28
|
-
begin
|
29
|
-
NonMongoidDocument.includes_many :non_mongoid_embeds
|
30
|
-
rescue => e
|
31
|
-
e.class.should == Mongoid::DocumentInclusion::NotMongoidDocument
|
32
|
-
e.message.should =~ /Parent document must include Mongoid::Document/
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
it "issues an error if child is not mongoid document" do
|
37
|
-
Object.const_set "NonMongoidDocument", Class.new
|
38
|
-
NonMongoidDocument.const_set "NonMongoidEmbed", Class.new
|
39
|
-
NonMongoidDocument.send(:include, Mongoid::Document)
|
40
|
-
NonMongoidDocument.send(:include, Mongoid::DocumentInclusion)
|
41
|
-
begin
|
42
|
-
NonMongoidDocument.includes_many :non_mongoid_embeds
|
43
|
-
rescue => e
|
44
|
-
e.class.should == Mongoid::DocumentInclusion::NotMongoidDocument
|
45
|
-
e.message.should =~ /Child document must include Mongoid::Document/
|
46
|
-
end
|
3
|
+
describe "Child Model" do
|
4
|
+
context "when being included" do
|
5
|
+
it "strips namespace from #param_key" do
|
6
|
+
Invoice::Item.model_name.param_key.should == "item"
|
47
7
|
end
|
48
8
|
|
49
|
-
|
9
|
+
it "strips namespace from #route_key" do
|
10
|
+
Invoice::Item.model_name.route_key.should == "items"
|
11
|
+
end
|
50
12
|
|
51
|
-
|
13
|
+
it "pluralize namespace in #partial_path" do
|
14
|
+
Invoice::Item.model_name.partial_path.should == "invoices/items/item"
|
15
|
+
end
|
52
16
|
|
53
17
|
it "delegates options to mongoid relation macro" do
|
54
18
|
Invoice::User.relations["invoice"].inverse_of.should == :user
|
55
19
|
end
|
56
|
-
|
20
|
+
|
57
21
|
it "is embedded in parent class" do
|
58
|
-
Invoice::Item.relations.keys.include
|
22
|
+
Invoice::Item.relations.keys.should include "invoice"
|
59
23
|
Invoice::Item.relations["invoice"].macro.should == :embedded_in
|
60
24
|
end
|
61
|
-
|
62
|
-
|
63
|
-
Object.const_set "NonMongoidDocument", Class.new
|
64
|
-
NonMongoidDocument.const_set "NonMongoidChild", Class.new
|
65
|
-
NonMongoidDocument.send(:include, Mongoid::Document)
|
66
|
-
NonMongoidDocument.send(:include, Mongoid::DocumentInclusion)
|
67
|
-
NonMongoidDocument::NonMongoidChild.send(:include, Mongoid::DocumentInclusion)
|
68
|
-
begin
|
69
|
-
NonMongoidDocument::NonMongoidChild.included_in :invoice
|
70
|
-
rescue => e
|
71
|
-
e.class.should == Mongoid::DocumentInclusion::NotMongoidDocument
|
72
|
-
e.message.should =~ /Child document must include Mongoid::Document/
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
it "issues an error if parent is not mongoid document" do
|
77
|
-
Object.const_set "NonMongoidDocument", Class.new
|
78
|
-
NonMongoidDocument.const_set "NonMongoidChild", Class.new
|
79
|
-
NonMongoidDocument::NonMongoidChild.send(:include, Mongoid::Document)
|
80
|
-
NonMongoidDocument::NonMongoidChild.send(:include, Mongoid::DocumentInclusion)
|
81
|
-
begin
|
82
|
-
NonMongoidDocument::NonMongoidChild.included_in :invoice
|
83
|
-
rescue => e
|
84
|
-
e.class.should == Mongoid::DocumentInclusion::NotMongoidDocument
|
85
|
-
e.message.should =~ /Parent document must include Mongoid::Document/
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
context "when overwriting #model_name" do
|
90
|
-
it "strips namespace from #param_key" do
|
91
|
-
Invoice::Item.model_name.param_key.should == "item"
|
92
|
-
end
|
93
|
-
|
94
|
-
it "strips namespace from #route_key" do
|
95
|
-
Invoice::Item.model_name.route_key.should == "items"
|
96
|
-
end
|
97
|
-
|
98
|
-
it "pluralize namespace in #partial_path" do
|
99
|
-
Invoice::Item.model_name.partial_path.should == "invoices/items/item"
|
100
|
-
end
|
25
|
+
it "#includded_in returns parent model" do
|
26
|
+
Invoice::Item.included_by.should == :invoice
|
101
27
|
end
|
102
|
-
|
103
28
|
it "forbids inclusion in another parent" do
|
104
|
-
|
105
|
-
|
106
|
-
rescue => e
|
107
|
-
e.class.should == Mongoid::DocumentInclusion::DocumentAlreadyIncluded
|
108
|
-
end
|
29
|
+
expect { Invoice::Item.included_in :invoice }.
|
30
|
+
to raise_error Mongoid::DocumentInclusion::DocumentAlreadyIncluded, /Document already included/
|
109
31
|
end
|
110
|
-
|
111
|
-
|
112
32
|
end
|
113
33
|
|
34
|
+
context "when not included" do
|
35
|
+
it "should preserve original name" do
|
36
|
+
Invoice::NotIncluded.model_name.should == "Invoice::NotIncluded"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "issues an error if parent is not mongoid document" do
|
41
|
+
Object.const_set "NonMongoidDocument", Class.new
|
42
|
+
NonMongoidDocument.const_set "NonMongoidChild", Class.new
|
43
|
+
NonMongoidDocument::NonMongoidChild.send(:include, Mongoid::Document)
|
44
|
+
NonMongoidDocument::NonMongoidChild.send(:include, Mongoid::DocumentInclusion)
|
45
|
+
expect { NonMongoidDocument::NonMongoidChild.included_in :invoice }.
|
46
|
+
to raise_error(Mongoid::DocumentInclusion::NotMongoidDocument, /Parent must be Mongoid::Document/)
|
47
|
+
end
|
48
|
+
|
49
|
+
|
114
50
|
end
|
115
51
|
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Parent Model" do
|
4
|
+
|
5
|
+
let(:parent) { Invoice }
|
6
|
+
|
7
|
+
describe "#includes_many" do
|
8
|
+
context "when including one model" do
|
9
|
+
it "#includding_many returns array with one model" do
|
10
|
+
parent.including_many.should include(:items)
|
11
|
+
end
|
12
|
+
it "embeds child with association name" do
|
13
|
+
parent.relations.keys.should include "items"
|
14
|
+
parent.relations["items"].macro.should == :embeds_many
|
15
|
+
end
|
16
|
+
it "embeds child with association class" do
|
17
|
+
parent.relations["items"].class_name.should == "Invoice::Item"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
context "when including two models" do
|
21
|
+
it "#includding_many returns array with two models" do
|
22
|
+
parent.includes_many :not_includeds
|
23
|
+
parent.including_many.should include(:items, :not_includeds)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
context "when not including with #includes_many" do
|
27
|
+
it "#includding_many returns an empty array" do
|
28
|
+
Invoice::Item.including_many.should be_blank
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#includes_one" do
|
34
|
+
context "when including one model" do
|
35
|
+
it "#including_one returns array with one model" do
|
36
|
+
parent.including_one.should include(:user)
|
37
|
+
end
|
38
|
+
it "delegates options to mongoid relation macro" do
|
39
|
+
parent.relations["user"].inverse_of.should == :invoice
|
40
|
+
end
|
41
|
+
it "embeds one child with given association name" do
|
42
|
+
parent.relations.keys.should include "user"
|
43
|
+
parent.relations["user"].macro.should == :embeds_one
|
44
|
+
end
|
45
|
+
end
|
46
|
+
context "when including two models" do
|
47
|
+
it "#includding_one returns array with two models" do
|
48
|
+
parent.includes_one :not_includeds
|
49
|
+
parent.including_one.should include(:user, :not_includeds)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
context "when not including with #includes_one" do
|
53
|
+
it "#includding_one returns an empty array" do
|
54
|
+
Invoice::Item.including_one.should be_blank
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
it "issues an error if child is not mongoid document" do
|
60
|
+
Object.const_set "NonMongoidDocument", Class.new
|
61
|
+
NonMongoidDocument.const_set "NonMongoidEmbed", Class.new
|
62
|
+
NonMongoidDocument.send(:include, Mongoid::Document)
|
63
|
+
NonMongoidDocument.send(:include, Mongoid::DocumentInclusion)
|
64
|
+
expect {NonMongoidDocument.includes_many :non_mongoid_embeds }.
|
65
|
+
to raise_error(Mongoid::DocumentInclusion::NotMongoidDocument, /Child must be Mongoid::Document/)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-included
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,23 +9,12 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-07-
|
12
|
+
date: 2011-07-07 00:00:00.000000000 -03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
16
|
-
name: activemodel
|
17
|
-
requirement: &2156553760 !ruby/object:Gem::Requirement
|
18
|
-
none: false
|
19
|
-
requirements:
|
20
|
-
- - ~>
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: 3.1.0.beta1
|
23
|
-
type: :runtime
|
24
|
-
prerelease: false
|
25
|
-
version_requirements: *2156553760
|
26
15
|
- !ruby/object:Gem::Dependency
|
27
16
|
name: mongoid
|
28
|
-
requirement: &
|
17
|
+
requirement: &2152036440 !ruby/object:Gem::Requirement
|
29
18
|
none: false
|
30
19
|
requirements:
|
31
20
|
- - ~>
|
@@ -33,10 +22,10 @@ dependencies:
|
|
33
22
|
version: '2.0'
|
34
23
|
type: :runtime
|
35
24
|
prerelease: false
|
36
|
-
version_requirements: *
|
25
|
+
version_requirements: *2152036440
|
37
26
|
- !ruby/object:Gem::Dependency
|
38
27
|
name: rspec
|
39
|
-
requirement: &
|
28
|
+
requirement: &2152300640 !ruby/object:Gem::Requirement
|
40
29
|
none: false
|
41
30
|
requirements:
|
42
31
|
- - ~>
|
@@ -44,10 +33,10 @@ dependencies:
|
|
44
33
|
version: 2.6.0
|
45
34
|
type: :development
|
46
35
|
prerelease: false
|
47
|
-
version_requirements: *
|
36
|
+
version_requirements: *2152300640
|
48
37
|
- !ruby/object:Gem::Dependency
|
49
38
|
name: bson_ext
|
50
|
-
requirement: &
|
39
|
+
requirement: &2152299720 !ruby/object:Gem::Requirement
|
51
40
|
none: false
|
52
41
|
requirements:
|
53
42
|
- - ~>
|
@@ -55,7 +44,7 @@ dependencies:
|
|
55
44
|
version: '1.3'
|
56
45
|
type: :development
|
57
46
|
prerelease: false
|
58
|
-
version_requirements: *
|
47
|
+
version_requirements: *2152299720
|
59
48
|
description: Helper to facilitate inclusion of namespaced documents in another Mongoid
|
60
49
|
Document
|
61
50
|
email: angelim@angelim.com.br
|
@@ -86,6 +75,7 @@ files:
|
|
86
75
|
- lib/mongoid-included.rb
|
87
76
|
- lib/mongoid-included/document_inclusion.rb
|
88
77
|
- lib/mongoid-included/embedded_name.rb
|
78
|
+
- lib/mongoid-included/inclusion_errors.rb
|
89
79
|
- lib/mongoid-included/version.rb
|
90
80
|
- log/development.log
|
91
81
|
- mongoid-included.gemspec
|
@@ -94,9 +84,11 @@ files:
|
|
94
84
|
- spec/models/inclusion.rb
|
95
85
|
- spec/models/invoice.rb
|
96
86
|
- spec/models/invoice/item.rb
|
87
|
+
- spec/models/invoice/not_included.rb
|
97
88
|
- spec/models/invoice/user.rb
|
98
89
|
- spec/spec_helper.rb
|
99
90
|
- spec/unit/included_spec.rb
|
91
|
+
- spec/unit/including_spec.rb
|
100
92
|
has_rdoc: true
|
101
93
|
homepage: http://github.com/angelim/mongoid-included
|
102
94
|
licenses: []
|
@@ -128,6 +120,8 @@ test_files:
|
|
128
120
|
- spec/models/inclusion.rb
|
129
121
|
- spec/models/invoice.rb
|
130
122
|
- spec/models/invoice/item.rb
|
123
|
+
- spec/models/invoice/not_included.rb
|
131
124
|
- spec/models/invoice/user.rb
|
132
125
|
- spec/spec_helper.rb
|
133
126
|
- spec/unit/included_spec.rb
|
127
|
+
- spec/unit/including_spec.rb
|