mongoid-included 0.2.0 → 0.3.1

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.
@@ -12,36 +12,62 @@ module Mongoid
12
12
 
13
13
  module ClassMethods
14
14
  def included_in(_model, args = {})
15
+ if args && args[:class_name]
16
+ _class_name = args[:class_name]
17
+ else
18
+ _class_name = _model
19
+ end
20
+
15
21
  raise NotMongoidDocument, "Parent must be Mongoid::Document" unless _mongoid_document? self.parent
16
- raise DocumentAlreadyIncluded, "Document already included" if included_by.present?
22
+ raise DocumentAlreadyIncluded, "Document already included" if (!included_by.blank? && included_by!=[_model_klass(_class_name)])
17
23
  embedded_in _model, args
18
24
 
19
- self.included_by = _model
25
+ self.included_by ||= []
26
+ self.included_by << _model_klass(_class_name) unless included_by.include? _model_klass(_class_name)
20
27
  self.extend ActiveModel::Naming
21
28
  _overwrite_model_name
22
29
  end
23
30
 
24
31
  def includes_many(_model, args = {})
25
- _verify_dependencies(_model)
26
- embeds_many _model, args.merge(:class_name => _included_klass(_model))
27
- (self.including_many ||= []) << _model
32
+ if args && args[:class_name]
33
+ _class_name = args[:class_name]
34
+ self.including_many ||= []
35
+ self.including_many << _model_klass(_class_name) unless including_many.include? _model_klass(_class_name)
36
+ else
37
+ _verify_dependencies(_model)
38
+ _class_name = _included_klass_name(_model)
39
+ self.including_many ||= []
40
+ self.including_many << _included_klass(_model) unless including_many.include? _included_klass(_model)
41
+ end
42
+ embeds_many _model, args.merge(:class_name => _class_name )
28
43
  end
29
44
 
30
45
  def includes_one(_model, args = {})
31
- _verify_dependencies(_model)
32
- embeds_one _model, args.merge(:class_name => _included_klass(_model))
33
- (self.including_one ||= []) << _model
46
+ if args && args[:class_name]
47
+ _class_name = args[:class_name]
48
+ self.including_one ||= []
49
+ self.including_one << _model_klass(_class_name) unless including_one.include? _model_klass(_class_name)
50
+ else
51
+ _verify_dependencies(_model)
52
+ _class_name = _included_klass_name(_model)
53
+ self.including_one ||= []
54
+ self.including_one << _included_klass(_model) unless including_one.include? _included_klass(_model)
55
+ end
56
+ embeds_one _model, args.merge(:class_name => _class_name)
34
57
  end
35
58
 
36
59
  private
37
60
 
38
61
  def _overwrite_model_name
39
62
  self.class_eval <<-EOF
40
- @_model_name ||=begin
41
- if self.parent != Object
42
- Mongoid::EmbeddedName.new(self, self.parent)
43
- else
44
- super
63
+ def self.model_name
64
+ @_model_name ||=begin
65
+ if self.parent != Object
66
+ Mongoid::EmbeddedName.new(self, self.parent)
67
+ else
68
+ namespace = self.parents.detect { |n| n.respond_to?(:_railtie) }
69
+ ActiveModel::Name.new(self, namespace)
70
+ end
45
71
  end
46
72
  end
47
73
  EOF
@@ -55,18 +81,21 @@ module Mongoid
55
81
  _model.to_s.classify.constantize
56
82
  end
57
83
 
58
- def _included_klass(_model)
84
+ def _included_klass_name(_model)
59
85
  "#{self}::#{_model_klass_name(_model)}"
60
86
  end
87
+
88
+ def _included_klass(_model)
89
+ "#{self}::#{_model_klass_name(_model)}".constantize
90
+ end
61
91
 
62
92
  def _mongoid_document?(_model)
63
- _model.ancestors.include?(Mongoid::Document)
93
+ _model.included_modules.include?(Mongoid::Document)
64
94
  end
65
95
 
66
96
  def _verify_dependencies(_model)
67
- raise NotMongoidDocument, "Child must be Mongoid::Document" unless _mongoid_document? _model_klass(_included_klass(_model))
97
+ raise NotMongoidDocument, "Child must be Mongoid::Document" unless _mongoid_document? _model_klass(_included_klass_name(_model))
68
98
  end
69
99
  end
70
100
  end
71
101
  end
72
- Mongoid::Document.send(:include, ::Mongoid::DocumentInclusion)
@@ -1,12 +1,12 @@
1
1
  module Mongoid
2
- class EmbeddedName < ActiveModel::Name
2
+ class EmbeddedName < String
3
3
  attr_reader :singular, :plural, :element, :collection, :partial_path, :route_key, :param_key, :i18n_key
4
4
  alias_method :cache_key, :collection
5
5
 
6
6
  def initialize(klass, namespace, pluralize_namespace = true)
7
7
  name ||= klass.name
8
+ super(name)
8
9
  @unnamespaced = name.sub(/^#{namespace.name}::/, '') if namespace
9
-
10
10
  @klass = klass
11
11
  @singular = _singularize(name).freeze
12
12
  @singular_namespace = _singularize(namespace.name).freeze
@@ -21,6 +21,21 @@ module Mongoid
21
21
  @i18n_key = name.underscore.to_sym
22
22
  end
23
23
 
24
+ def human(options={})
25
+ return @human unless @klass.respond_to?(:lookup_ancestors) &&
26
+ @klass.respond_to?(:i18n_scope)
27
+
28
+ defaults = @klass.lookup_ancestors.map do |klass|
29
+ klass.model_name.i18n_key
30
+ end
31
+
32
+ defaults << options[:default] if options[:default]
33
+ defaults << @human
34
+
35
+ options = {:scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults}.merge(options.except(:default))
36
+ I18n.translate(defaults.shift, options)
37
+ end
38
+
24
39
  def _singularize(string, replacement='_')
25
40
  ActiveSupport::Inflector.underscore(string).tr('/', replacement)
26
41
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Included
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.1"
4
4
  end
5
5
  end
@@ -1 +1,3 @@
1
+ require 'mongoid'
1
2
  require 'mongoid-included/document_inclusion'
3
+ Mongoid::Document.send(:include, ::Mongoid::DocumentInclusion)
@@ -1,12 +1,16 @@
1
1
  class Invoice
2
2
  includes_many :items
3
- includes_one :user, :inverse_of => :invoice, :index => true
3
+ includes_one :user, :inverse_of => :invoice
4
+ includes_one :other_user, :class_name => "Invoice::User", :inverse_of => :user_invoice
5
+ includes_many :other_items, :class_name => "Invoice::Item"
4
6
  end
5
7
 
6
8
  class Invoice::Item
7
- included_in :invoice
9
+ included_in :invoice, :inverse_of => :items
10
+ included_in :invoiced_by, :class_name => "Invoice", :inverse_of => :other_items
8
11
  end
9
12
 
10
13
  class Invoice::User
11
14
  included_in :invoice, :inverse_of => :user
15
+ included_in :user_invoice, :class_name => "Invoice", :inverse_of => :other_user
12
16
  end
@@ -23,12 +23,24 @@ describe "Child Model" do
23
23
  Invoice::Item.relations["invoice"].macro.should == :embedded_in
24
24
  end
25
25
  it "#includded_in returns parent model" do
26
- Invoice::Item.included_by.should == :invoice
26
+ Invoice::Item.included_by.should include Invoice
27
27
  end
28
28
  it "forbids inclusion in another parent" do
29
- expect { Invoice::Item.included_in :invoice }.
29
+ Object.const_set "AnotherDocument", Class.new
30
+ AnotherDocument.send(:include, Mongoid::Document)
31
+ expect { Invoice::Item.included_in :another_document }.
30
32
  to raise_error Mongoid::DocumentInclusion::DocumentAlreadyIncluded, /Document already included/
31
33
  end
34
+ context "when included with custom relation name" do
35
+ it "returns relation name" do
36
+ Invoice::Item.relations["invoiced_by"].macro.should == :embedded_in
37
+ end
38
+ it "returns relation class" do
39
+ Invoice::Item.relations["invoiced_by"].class_name.should == "Invoice"
40
+ end
41
+
42
+
43
+ end
32
44
  end
33
45
 
34
46
  context "when not included" do
@@ -7,7 +7,7 @@ describe "Parent Model" do
7
7
  describe "#includes_many" do
8
8
  context "when including one model" do
9
9
  it "#includding_many returns array with one model" do
10
- parent.including_many.should include(:items)
10
+ parent.including_many.should include(Invoice::Item)
11
11
  end
12
12
  it "embeds child with association name" do
13
13
  parent.relations.keys.should include "items"
@@ -20,7 +20,7 @@ describe "Parent Model" do
20
20
  context "when including two models" do
21
21
  it "#includding_many returns array with two models" do
22
22
  parent.includes_many :not_includeds
23
- parent.including_many.should include(:items, :not_includeds)
23
+ parent.including_many.should include(Invoice::Item, Invoice::NotIncluded)
24
24
  end
25
25
  end
26
26
  context "when not including with #includes_many" do
@@ -28,12 +28,21 @@ describe "Parent Model" do
28
28
  Invoice::Item.including_many.should be_blank
29
29
  end
30
30
  end
31
+ context "when including with custom relation name" do
32
+ it "embeds child with association name" do
33
+ parent.relations.keys.should include "other_items"
34
+ parent.relations["other_items"].macro.should == :embeds_many
35
+ end
36
+ it "embeds child with association class" do
37
+ parent.relations["other_items"].class_name.should == "Invoice::Item"
38
+ end
39
+ end
31
40
  end
32
41
 
33
42
  describe "#includes_one" do
34
43
  context "when including one model" do
35
44
  it "#including_one returns array with one model" do
36
- parent.including_one.should include(:user)
45
+ parent.including_one.should include(Invoice::User)
37
46
  end
38
47
  it "delegates options to mongoid relation macro" do
39
48
  parent.relations["user"].inverse_of.should == :invoice
@@ -46,7 +55,7 @@ describe "Parent Model" do
46
55
  context "when including two models" do
47
56
  it "#includding_one returns array with two models" do
48
57
  parent.includes_one :not_includeds
49
- parent.including_one.should include(:user, :not_includeds)
58
+ parent.including_one.should include(Invoice::User, Invoice::NotIncluded)
50
59
  end
51
60
  end
52
61
  context "when not including with #includes_one" do
@@ -54,6 +63,15 @@ describe "Parent Model" do
54
63
  Invoice::Item.including_one.should be_blank
55
64
  end
56
65
  end
66
+ context "when including with custom relation name" do
67
+ it "embeds child with association name" do
68
+ parent.relations.keys.should include "other_items"
69
+ parent.relations["other_user"].macro.should == :embeds_one
70
+ end
71
+ it "embeds child with association class" do
72
+ parent.relations["other_user"].class_name.should == "Invoice::User"
73
+ end
74
+ end
57
75
  end
58
76
 
59
77
  it "issues an error if child is not mongoid document" do
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.2.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-07 00:00:00.000000000 -03:00
13
- default_executable:
12
+ date: 2012-01-09 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: mongoid
17
- requirement: &2152036440 !ruby/object:Gem::Requirement
16
+ requirement: &70172149957920 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ~>
@@ -22,10 +21,10 @@ dependencies:
22
21
  version: '2.0'
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *2152036440
24
+ version_requirements: *70172149957920
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: rspec
28
- requirement: &2152300640 !ruby/object:Gem::Requirement
27
+ requirement: &70172149946960 !ruby/object:Gem::Requirement
29
28
  none: false
30
29
  requirements:
31
30
  - - ~>
@@ -33,10 +32,10 @@ dependencies:
33
32
  version: 2.6.0
34
33
  type: :development
35
34
  prerelease: false
36
- version_requirements: *2152300640
35
+ version_requirements: *70172149946960
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: bson_ext
39
- requirement: &2152299720 !ruby/object:Gem::Requirement
38
+ requirement: &70172149946180 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
42
41
  - - ~>
@@ -44,7 +43,7 @@ dependencies:
44
43
  version: '1.3'
45
44
  type: :development
46
45
  prerelease: false
47
- version_requirements: *2152299720
46
+ version_requirements: *70172149946180
48
47
  description: Helper to facilitate inclusion of namespaced documents in another Mongoid
49
48
  Document
50
49
  email: angelim@angelim.com.br
@@ -89,7 +88,6 @@ files:
89
88
  - spec/spec_helper.rb
90
89
  - spec/unit/included_spec.rb
91
90
  - spec/unit/including_spec.rb
92
- has_rdoc: true
93
91
  homepage: http://github.com/angelim/mongoid-included
94
92
  licenses: []
95
93
  post_install_message:
@@ -110,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
108
  version: '0'
111
109
  requirements: []
112
110
  rubyforge_project: mongoid-included
113
- rubygems_version: 1.6.2
111
+ rubygems_version: 1.8.10
114
112
  signing_key:
115
113
  specification_version: 3
116
114
  summary: Included namespaces documents for Mongoid
@@ -125,3 +123,4 @@ test_files:
125
123
  - spec/spec_helper.rb
126
124
  - spec/unit/included_spec.rb
127
125
  - spec/unit/including_spec.rb
126
+ has_rdoc: