mongoid-included 0.0.1 → 0.0.2

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mongoid-rspec.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Alexandre Angelim
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ Mongoid::Included
2
+ ====================
3
+
4
+ Helper module to embed documents using the parent's namespace while preserving url helpers consistency. This is useful to organize your project if you heavily rely on embedded documents. This gem concerns the issue 1429 of rails repository: https://github.com/rails/rails/issues/1429
5
+ Works with [Mongoid 2.0](https://github.com/mongoid/mongoid) and ActiveModel 3.1.0.beta1.
6
+
7
+ Installation
8
+ ------------
9
+
10
+ Add the gem to your Bundler `Gemfile`:
11
+
12
+ gem 'mongoid-included'
13
+
14
+ Or install with RubyGems:
15
+
16
+ $ gem install mongoid-included
17
+
18
+
19
+ Usage
20
+ -----
21
+
22
+ In your embedded model:
23
+
24
+ # Include the helper module
25
+ include Mongoid::DocumentInclusion
26
+
27
+ # Define parent document in embedded class
28
+ included_in :invoice
29
+
30
+ In your parent model:
31
+
32
+ # Define embedded relation in parent document
33
+ includes_many :items
34
+ includes_one :user
35
+
36
+
37
+ Example
38
+ -------
39
+
40
+ class Invoice
41
+ include Mongoid::Document
42
+ include Mongoid::DocumentInclusion
43
+
44
+ includes_many :items
45
+ end
46
+
47
+ # This will generate a Mongoid Relation with the following signature:
48
+ embeds_many :items, :class_name => Invoice::Item
49
+
50
+ class Invoice::Item
51
+ include Mongoid::Document
52
+ include Mongoid::DocumentInclusion
53
+
54
+ included_in :invoice
55
+ end
56
+ # This will generate the following code:
57
+ extend ActiveModel::Naming
58
+ # Overriding .model_name so rails recognizes the class as __Item__ and generate more convenient urls.
59
+ def model_name
60
+ if self.parent != Object
61
+ ActiveModel::Name.new(self, self.parent)
62
+ else
63
+ super
64
+ end
65
+ end
66
+
67
+ embedded_in :invoice
68
+
69
+ # Define routes with the embedded document as a nested resource
70
+ resources :invoices do
71
+ resources :items
72
+ end
73
+
74
+ # if you decide that the controller should also use namespaces, place the __items_controller__ inside an __invoices__ folder and use:
75
+ resources :invoices do
76
+ resources :items, :module => :invoices
77
+ end
78
+
79
+
80
+
81
+ Rake tasks
82
+ ----------
83
+
84
+ Todo
85
+ ----------
86
+
87
+ Known issues
88
+ ------------
89
+
90
+
91
+ Credits
92
+ -------
93
+
94
+ Copyright (c) 2011 Alexandre Angelim, released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ require 'bundler'
4
+ require "rspec"
5
+ require 'rspec/core/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new("spec:functional") do |spec|
8
+ spec.rspec_opts = %w(--format progress)
9
+ spec.pattern = "spec/functional/**/*_spec.rb"
10
+ end
11
+
12
+ task :default => :spec
13
+ task :test => :spec
14
+
15
+ require 'rake/rdoctask'
16
+ require "mongoid-included/version"
17
+ Rake::RDocTask.new do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = "mongoid-included #{Mongoid::Mirrored::VERSION}"
20
+ rdoc.rdoc_files.include('README*')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,2 @@
1
+ require 'mongoid'
2
+ require 'mongoid-included/document_inclusion'
@@ -0,0 +1,67 @@
1
+ module Mongoid
2
+ module DocumentInclusion
3
+ extend ActiveSupport::Concern
4
+
5
+ class NotMongoidDocument < StandardError; end;
6
+ class DocumentAlreadyIncluded < StandardError; end;
7
+
8
+ included do
9
+ extend ActiveModel::Naming
10
+ cattr_accessor :inclusion_model
11
+ end
12
+
13
+ module ClassMethods
14
+ def included_in(_model, args = {})
15
+ raise NotMongoidDocument, "Document must be a Mongoid Document" unless mongoid_document? self
16
+ raise NotMongoidDocument, "Parent document must be a Mongoid Document" unless mongoid_document? self.parent
17
+ raise DocumentAlreadyIncluded, "Document can be included only in one class" if inclusion_model
18
+ embedded_in _model, args
19
+ self.inclusion_model = true
20
+ end
21
+
22
+ def includes_many(_model, args = {})
23
+ verify_dependencies(_model)
24
+ embeds_many _model, args.merge(:class_name => included_klass(_model))
25
+ self.inclusion_model = true
26
+ end
27
+
28
+ def includes_one(_model, args = {})
29
+ verify_dependencies(_model)
30
+ embeds_one _model, args.merge(:class_name => included_klass(_model))
31
+ self.inclusion_model = true
32
+ end
33
+
34
+ def model_name
35
+ if self.parent != Object
36
+ ActiveModel::Name.new(self, self.parent)
37
+ else
38
+ super
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def model_klass_name(_model)
45
+ _model.to_s.classify
46
+ end
47
+
48
+ def model_klass(_model)
49
+ _model.to_s.classify.constantize
50
+ end
51
+
52
+ def included_klass(_model)
53
+ "#{self}::#{model_klass_name(_model)}"
54
+ end
55
+
56
+ def mongoid_document?(_model)
57
+ _model.ancestors.include?(Mongoid::Document)
58
+ end
59
+
60
+ def verify_dependencies(_model)
61
+ raise NotMongoidDocument, "Document must be a Mongoid Document" unless mongoid_document? self
62
+ raise NotMongoidDocument, "Descendent document must be a Mongoid Document" unless mongoid_document? model_klass(included_klass(_model))
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Included
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
File without changes
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mongoid-included/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mongoid-included"
7
+ s.version = Mongoid::Included::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Alexandre Angelim"]
10
+ s.email = %q{angelim@angelim.com.br}
11
+ s.homepage = %q{http://github.com/angelim/mongoid-included}
12
+ s.summary = %q{Included namespaces documents for Mongoid}
13
+ s.description = %q{Helper to facilitate inclusion of namespaced documents in another Mongoid Document}
14
+
15
+ s.rubyforge_project = "mongoid-included"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_runtime_dependency("activemodel", ["~> 3.1.0.beta1"])
23
+ s.add_runtime_dependency(%q<mongoid>, ["~> 2.0"])
24
+ s.add_development_dependency("rspec", ["~> 2.6"])
25
+ s.add_development_dependency("bson_ext", ["~> 1.3"])
26
+
27
+ end
@@ -0,0 +1,16 @@
1
+ # test methods
2
+ # test from_mirror
3
+ require 'spec_helper'
4
+
5
+ describe Invoice do
6
+ let(:invoice) { Invoice.create(:title => "InvoiceTest") }
7
+
8
+ it "respond to items" do
9
+ invoice.should respond_to(:items)
10
+ end
11
+
12
+ it "has proxy to items" do
13
+ invoice.items.create(:name => "ItemTest")
14
+ invoice.items.should have(1).item
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # test methods
2
+ # test from_mirror
3
+ require 'spec_helper'
4
+
5
+ describe Invoice::Item do
6
+ let(:invoice) { Invoice.create(:title => "InvoiceTest") }
7
+ let(:item) { invoice.items.create(:name => "ItemTest") }
8
+
9
+ it "respond to invoice" do
10
+ item.should respond_to(:invoice)
11
+ end
12
+
13
+ it "has proxy to invoice" do
14
+ item.invoice.should == invoice
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ class Invoice
2
+ includes_many :items
3
+ includes_one :user, :inverse_of => :invoice, :index => true
4
+ end
5
+
6
+ class Invoice::Item
7
+ included_in :invoice
8
+ end
9
+
10
+ class Invoice::User
11
+ included_in :invoice, :inverse_of => :user
12
+ end
@@ -0,0 +1,6 @@
1
+ class Invoice
2
+ include Mongoid::Document
3
+ include Mongoid::DocumentInclusion
4
+ field :title
5
+ validates_presence_of :title
6
+ end
@@ -0,0 +1,11 @@
1
+ class Invoice::Item
2
+ include Mongoid::Document
3
+ include Mongoid::DocumentInclusion
4
+
5
+ field :name
6
+ field :test, :default => "ale"
7
+ field :test1, :default => "ale"
8
+
9
+
10
+ validates_presence_of :name
11
+ end
@@ -0,0 +1,6 @@
1
+ class Invoice::User
2
+ include Mongoid::Document
3
+ include Mongoid::DocumentInclusion
4
+
5
+ field :name
6
+ end
@@ -0,0 +1,36 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+ Bundler.setup
4
+
5
+ require 'rspec'
6
+ require 'mongoid'
7
+ require 'mongoid-included'
8
+
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
10
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib"))
11
+ MODELS = [File.join(File.dirname(__FILE__), "models"), File.join(File.dirname(__FILE__), "models/invoice")]
12
+ MODELS.each do |_model|
13
+ $LOAD_PATH.unshift(_model)
14
+ end
15
+ require 'models/invoice'
16
+ require 'models/invoice/item'
17
+ require 'models/invoice/user'
18
+ require 'models/inclusion'
19
+
20
+ Mongoid.configure do |config|
21
+ name = "mongoid-included-test"
22
+ host = "localhost"
23
+ config.master = Mongo::Connection.new.db(name)
24
+ config.autocreate_indexes = true
25
+ end
26
+
27
+ # MODELS.each do |_model|
28
+ # Dir[ File.join(_model, "*.rb") ].sort.each { |file| require File.basename(file) }
29
+ # end
30
+
31
+ RSpec.configure do |config|
32
+ config.mock_with :rspec
33
+ config.after :each do
34
+ Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)
35
+ end
36
+ end
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Mongoid::Included" do
4
+
5
+ context "when parent class includes a child" do
6
+ it "delegates options to mongoid relation macro" do
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 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 =~ /Document must be a 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 =~ /Descendent document must be a Mongoid Document/
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ context "when child class is included in parent" do
52
+
53
+ it "delegates options to mongoid relation macro" do
54
+ Invoice::User.relations["invoice"].inverse_of.should == :user
55
+ end
56
+
57
+ it "is embedded in parent class" do
58
+ Invoice::Item.relations.keys.include? "invoice"
59
+ Invoice::Item.relations["invoice"].macro.should == :embedded_in
60
+ end
61
+
62
+ it "strips namespace from param_key" do
63
+ Invoice::Item.model_name.param_key.should == "item"
64
+ end
65
+
66
+ it "strips namespace from #route_key" do
67
+ Invoice::Item.model_name.route_key.should == "items"
68
+ end
69
+
70
+ it "forbids inclusion in another parent" do
71
+ begin
72
+ Invoice::Item.included_in :invoice
73
+ rescue => e
74
+ e.class.should == Mongoid::DocumentInclusion::DocumentAlreadyIncluded
75
+ end
76
+ end
77
+
78
+
79
+ it "issues an error if parent is not mongoid document" do
80
+ Object.const_set "NonMongoidDocument", Class.new
81
+ NonMongoidDocument.const_set "NonMongoidParent", Class.new
82
+ NonMongoidDocument.send(:include, Mongoid::Document)
83
+ NonMongoidDocument.send(:include, Mongoid::DocumentInclusion)
84
+ begin
85
+ NonMongoidDocument.included_in :invoice
86
+ rescue => e
87
+ e.class.should == Mongoid::DocumentInclusion::NotMongoidDocument
88
+ e.message.should =~ /Parent document must be a Mongoid Document/
89
+ end
90
+ end
91
+
92
+ it "issues an error if parent is not mongoid document" do
93
+ Object.const_set "NonMongoidDocument", Class.new
94
+ NonMongoidDocument.const_set "NonMongoidParent", Class.new
95
+ NonMongoidDocument.send(:include, Mongoid::DocumentInclusion)
96
+ begin
97
+ NonMongoidDocument.included_in :invoice
98
+ rescue => e
99
+ e.class.should == Mongoid::DocumentInclusion::NotMongoidDocument
100
+ e.message.should =~ /Document must be a Mongoid Document/
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+ end
107
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Alexandre Angelim
@@ -83,8 +83,27 @@ extensions: []
83
83
 
84
84
  extra_rdoc_files: []
85
85
 
86
- files: []
87
-
86
+ files:
87
+ - .document
88
+ - .gitignore
89
+ - .rvmrc
90
+ - Gemfile
91
+ - LICENSE
92
+ - README.md
93
+ - Rakefile
94
+ - lib/mongoid-included.rb
95
+ - lib/mongoid-included/document_inclusion.rb
96
+ - lib/mongoid-included/version.rb
97
+ - log/development.log
98
+ - mongoid-included.gemspec
99
+ - spec/functional/invoice.rb
100
+ - spec/functional/item.rb
101
+ - spec/models/inclusion.rb
102
+ - spec/models/invoice.rb
103
+ - spec/models/invoice/item.rb
104
+ - spec/models/invoice/user.rb
105
+ - spec/spec_helper.rb
106
+ - spec/unit/included_spec.rb
88
107
  has_rdoc: true
89
108
  homepage: http://github.com/angelim/mongoid-included
90
109
  licenses: []
@@ -117,5 +136,12 @@ rubygems_version: 1.3.7
117
136
  signing_key:
118
137
  specification_version: 3
119
138
  summary: Included namespaces documents for Mongoid
120
- test_files: []
121
-
139
+ test_files:
140
+ - spec/functional/invoice.rb
141
+ - spec/functional/item.rb
142
+ - spec/models/inclusion.rb
143
+ - spec/models/invoice.rb
144
+ - spec/models/invoice/item.rb
145
+ - spec/models/invoice/user.rb
146
+ - spec/spec_helper.rb
147
+ - spec/unit/included_spec.rb