durran-mongoid 0.2.3 → 0.2.4
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/VERSION +1 -1
- data/lib/mongoid.rb +1 -1
- data/lib/mongoid/associations/has_many_association.rb +5 -5
- data/lib/mongoid/associations/has_one_association.rb +13 -4
- data/lib/mongoid/document.rb +4 -5
- data/mongoid.gemspec +2 -2
- data/spec/spec_helper.rb +1 -0
- data/spec/unit/mongoid/document_spec.rb +165 -48
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.4
|
data/lib/mongoid.rb
CHANGED
@@ -2,17 +2,17 @@ module Mongoid #:nodoc:
|
|
2
2
|
module Associations #:nodoc:
|
3
3
|
class HasManyAssociation < DelegateClass(Array) #:nodoc:
|
4
4
|
|
5
|
-
# Creates the new association by finding the attributes in
|
6
|
-
# the parent document with its name, and instantiating a
|
7
|
-
# new document for each one found. These will then be put in an
|
5
|
+
# Creates the new association by finding the attributes in
|
6
|
+
# the parent document with its name, and instantiating a
|
7
|
+
# new document for each one found. These will then be put in an
|
8
8
|
# internal array.
|
9
9
|
#
|
10
|
-
# This then delegated all methods to the array class since this is
|
10
|
+
# This then delegated all methods to the array class since this is
|
11
11
|
# essentially a proxy to an array itself.
|
12
12
|
def initialize(association_name, document)
|
13
13
|
@klass = association_name.to_s.classify.constantize
|
14
14
|
attributes = document.attributes[association_name]
|
15
|
-
@documents = attributes ? attributes.collect do |attribute|
|
15
|
+
@documents = attributes ? attributes.collect do |attribute|
|
16
16
|
child = @klass.new(attribute)
|
17
17
|
child.parent = document
|
18
18
|
child
|
@@ -2,6 +2,9 @@ module Mongoid #:nodoc:
|
|
2
2
|
module Associations #:nodoc:
|
3
3
|
class HasOneAssociation #:nodoc:
|
4
4
|
|
5
|
+
attr_reader :document
|
6
|
+
delegate :valid?, :to => :document
|
7
|
+
|
5
8
|
# Creates the new association by finding the attributes in
|
6
9
|
# the parent document with its name, and instantiating a
|
7
10
|
# new document for it.
|
@@ -13,12 +16,18 @@ module Mongoid #:nodoc:
|
|
13
16
|
attributes = document.attributes[association_name]
|
14
17
|
@document = klass.new(attributes)
|
15
18
|
@document.parent = document
|
19
|
+
decorate!
|
16
20
|
end
|
17
21
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
+
private
|
23
|
+
def decorate!
|
24
|
+
@document.public_methods(false).each do |method|
|
25
|
+
(class << self; self; end).class_eval do
|
26
|
+
define_method method do |*args|
|
27
|
+
@document.send method, *args
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
22
31
|
end
|
23
32
|
|
24
33
|
end
|
data/lib/mongoid/document.rb
CHANGED
@@ -56,8 +56,8 @@ module Mongoid #:nodoc:
|
|
56
56
|
def find(*args)
|
57
57
|
type, selector = args[0], args[1]
|
58
58
|
case type
|
59
|
-
when :all then find_all(selector)
|
60
|
-
when :first then find_first(selector)
|
59
|
+
when :all then find_all(selector[:conditions])
|
60
|
+
when :first then find_first(selector[:conditions])
|
61
61
|
else find_first(Mongo::ObjectID.from_string(type.to_s))
|
62
62
|
end
|
63
63
|
end
|
@@ -100,8 +100,8 @@ module Mongoid #:nodoc:
|
|
100
100
|
|
101
101
|
# Find all documents in paginated fashion given the supplied arguments.
|
102
102
|
# If no parameters are passed just default to offset 0 and limit 20.
|
103
|
-
def paginate(selector =
|
104
|
-
collection.find(selector, Mongoid::Paginator.new(params).options).collect { |doc| new(doc) }
|
103
|
+
def paginate(selector = {}, params = {})
|
104
|
+
collection.find(selector[:conditions], Mongoid::Paginator.new(params).options).collect { |doc| new(doc) }
|
105
105
|
end
|
106
106
|
|
107
107
|
end
|
@@ -181,7 +181,6 @@ module Mongoid #:nodoc:
|
|
181
181
|
|
182
182
|
# Takes the supplied raw grouping of documents and alters it to a
|
183
183
|
# grouping of actual document objects.
|
184
|
-
# TODO: Me no likey this...
|
185
184
|
def group!(docs)
|
186
185
|
docs["group"] = docs["group"].collect { |attrs| new(attrs) }; docs
|
187
186
|
end
|
data/mongoid.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Durran Jordan"]
|
12
|
-
s.date = %q{2009-09-
|
12
|
+
s.date = %q{2009-09-27}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.textile"
|
data/spec/spec_helper.rb
CHANGED
@@ -120,8 +120,8 @@ describe Mongoid::Document do
|
|
120
120
|
context "when finding first" do
|
121
121
|
|
122
122
|
it "delegates to find_first" do
|
123
|
-
@collection.expects(:find_one).with(:test => "Test").returns(@attributes)
|
124
|
-
Person.find(:first, :test => "Test")
|
123
|
+
@collection.expects(:find_one).with(:test => "Test" ).returns(@attributes)
|
124
|
+
Person.find(:first, :conditions => { :test => "Test" })
|
125
125
|
end
|
126
126
|
|
127
127
|
end
|
@@ -136,7 +136,7 @@ describe Mongoid::Document do
|
|
136
136
|
it "delegates to find_all" do
|
137
137
|
@collection.expects(:find).with(:test => "Test").returns(@cursor)
|
138
138
|
@cursor.expects(:collect).returns(@people)
|
139
|
-
Person.find(:all, :test => "Test")
|
139
|
+
Person.find(:all, :conditions => { :test => "Test" })
|
140
140
|
end
|
141
141
|
|
142
142
|
end
|
@@ -368,7 +368,7 @@ describe Mongoid::Document do
|
|
368
368
|
|
369
369
|
it "delegates offset and limit to find_all" do
|
370
370
|
@collection.expects(:find).with({ :test => "Test" }, {:limit => 20, :offset => 20}).returns([])
|
371
|
-
Person.paginate({ :test => "Test" }, { :page => 2, :per_page => 20 })
|
371
|
+
Person.paginate({ :conditions => { :test => "Test" } }, { :page => 2, :per_page => 20 })
|
372
372
|
end
|
373
373
|
|
374
374
|
end
|
@@ -377,7 +377,7 @@ describe Mongoid::Document do
|
|
377
377
|
|
378
378
|
it "passes the default offset and limit to find_all" do
|
379
379
|
@collection.expects(:find).with({ :test => "Test" }, {:limit => 20, :offset => 0}).returns([])
|
380
|
-
Person.paginate({ :test => "Test" })
|
380
|
+
Person.paginate(:conditions => { :test => "Test" })
|
381
381
|
end
|
382
382
|
|
383
383
|
end
|
@@ -472,83 +472,200 @@ describe Mongoid::Document do
|
|
472
472
|
|
473
473
|
context "validations" do
|
474
474
|
|
475
|
-
|
476
|
-
|
477
|
-
|
475
|
+
context "when defining using macros" do
|
476
|
+
|
477
|
+
after do
|
478
|
+
Person.validations.clear
|
479
|
+
end
|
478
480
|
|
479
|
-
|
481
|
+
describe "#validates_acceptance_of" do
|
480
482
|
|
481
|
-
|
482
|
-
|
483
|
-
|
483
|
+
it "adds the acceptance validation" do
|
484
|
+
Person.class_eval do
|
485
|
+
validates_acceptance_of :terms
|
486
|
+
end
|
487
|
+
Person.validations.first.should be_a_kind_of(Validatable::ValidatesAcceptanceOf)
|
484
488
|
end
|
485
|
-
|
489
|
+
|
486
490
|
end
|
487
491
|
|
488
|
-
|
492
|
+
describe "#validates_associated" do
|
489
493
|
|
490
|
-
|
494
|
+
it "adds the associated validation" do
|
495
|
+
Person.class_eval do
|
496
|
+
validates_associated :name
|
497
|
+
end
|
498
|
+
Person.validations.first.should be_a_kind_of(Validatable::ValidatesAssociated)
|
499
|
+
end
|
491
500
|
|
492
|
-
|
493
|
-
|
494
|
-
|
501
|
+
end
|
502
|
+
|
503
|
+
describe "#validates_format_of" do
|
504
|
+
|
505
|
+
it "adds the format validation" do
|
506
|
+
Person.class_eval do
|
507
|
+
validates_format_of :title, :with => /[A-Za-z]/
|
508
|
+
end
|
509
|
+
Person.validations.first.should be_a_kind_of(Validatable::ValidatesFormatOf)
|
495
510
|
end
|
496
|
-
|
511
|
+
|
497
512
|
end
|
498
513
|
|
499
|
-
|
514
|
+
describe "#validates_length_of" do
|
515
|
+
|
516
|
+
it "adds the length validation" do
|
517
|
+
Person.class_eval do
|
518
|
+
validates_length_of :title, :minimum => 10
|
519
|
+
end
|
520
|
+
Person.validations.first.should be_a_kind_of(Validatable::ValidatesLengthOf)
|
521
|
+
end
|
522
|
+
|
523
|
+
end
|
500
524
|
|
501
|
-
|
525
|
+
describe "#validates_numericality_of" do
|
502
526
|
|
503
|
-
|
504
|
-
|
505
|
-
|
527
|
+
it "adds the numericality validation" do
|
528
|
+
Person.class_eval do
|
529
|
+
validates_numericality_of :age
|
530
|
+
end
|
531
|
+
Person.validations.first.should be_a_kind_of(Validatable::ValidatesNumericalityOf)
|
506
532
|
end
|
507
|
-
|
533
|
+
|
508
534
|
end
|
509
535
|
|
510
|
-
|
536
|
+
describe "#validates_presence_of" do
|
537
|
+
|
538
|
+
it "adds the presence validation" do
|
539
|
+
Person.class_eval do
|
540
|
+
validates_presence_of :title
|
541
|
+
end
|
542
|
+
Person.validations.first.should be_a_kind_of(Validatable::ValidatesPresenceOf)
|
543
|
+
end
|
544
|
+
|
545
|
+
end
|
511
546
|
|
512
|
-
|
547
|
+
describe "#validates_true_for" do
|
513
548
|
|
514
|
-
|
515
|
-
|
516
|
-
|
549
|
+
it "adds the true validation" do
|
550
|
+
Person.class_eval do
|
551
|
+
validates_true_for :title, :logic => lambda { title == "Esquire" }
|
552
|
+
end
|
553
|
+
Person.validations.first.should be_a_kind_of(Validatable::ValidatesTrueFor)
|
517
554
|
end
|
518
|
-
|
555
|
+
|
519
556
|
end
|
520
557
|
|
521
558
|
end
|
522
559
|
|
523
|
-
|
560
|
+
context "when running validations" do
|
561
|
+
|
562
|
+
before do
|
563
|
+
@person = Person.new
|
564
|
+
end
|
565
|
+
|
566
|
+
after do
|
567
|
+
Person.validations.clear
|
568
|
+
end
|
569
|
+
|
570
|
+
describe "#validates_acceptance_of" do
|
524
571
|
|
525
|
-
|
526
|
-
|
527
|
-
|
572
|
+
it "fails if field not accepted" do
|
573
|
+
Person.class_eval do
|
574
|
+
validates_acceptance_of :terms
|
575
|
+
end
|
576
|
+
@person.valid?.should be_false
|
577
|
+
@person.errors.on(:terms).should_not be_nil
|
528
578
|
end
|
529
|
-
|
579
|
+
|
530
580
|
end
|
531
581
|
|
532
|
-
|
582
|
+
describe "#validates_associated" do
|
583
|
+
|
584
|
+
context "when association is a has_many" do
|
585
|
+
|
586
|
+
it "fails when any association fails validation"
|
533
587
|
|
534
|
-
|
588
|
+
end
|
589
|
+
|
590
|
+
context "when association is a has_one" do
|
591
|
+
|
592
|
+
it "fails when the association fails validation" do
|
593
|
+
Person.class_eval do
|
594
|
+
validates_associated :name
|
595
|
+
end
|
596
|
+
Name.class_eval do
|
597
|
+
validates_presence_of :first_name
|
598
|
+
end
|
599
|
+
@person.name = Name.new
|
600
|
+
@person.valid?.should be_false
|
601
|
+
@person.errors.on(:name).should_not be_nil
|
602
|
+
end
|
535
603
|
|
536
|
-
it "adds the presence validation" do
|
537
|
-
Person.class_eval do
|
538
|
-
validates_presence_of :title
|
539
604
|
end
|
540
|
-
|
605
|
+
|
541
606
|
end
|
542
607
|
|
543
|
-
|
608
|
+
describe "#validates_format_of" do
|
609
|
+
|
610
|
+
it "fails if the field is in the wrong format" do
|
611
|
+
Person.class_eval do
|
612
|
+
validates_format_of :title, :with => /[A-Za-z]/
|
613
|
+
end
|
614
|
+
@person.title = 10
|
615
|
+
@person.valid?.should be_false
|
616
|
+
@person.errors.on(:title).should_not be_nil
|
617
|
+
end
|
544
618
|
|
545
|
-
|
619
|
+
end
|
546
620
|
|
547
|
-
|
548
|
-
|
549
|
-
|
621
|
+
describe "#validates_length_of" do
|
622
|
+
|
623
|
+
it "fails if the field is the wrong length" do
|
624
|
+
Person.class_eval do
|
625
|
+
validates_length_of :title, :minimum => 10
|
626
|
+
end
|
627
|
+
@person.title = "Testing"
|
628
|
+
@person.valid?.should be_false
|
629
|
+
@person.errors.on(:title).should_not be_nil
|
550
630
|
end
|
551
|
-
|
631
|
+
|
632
|
+
end
|
633
|
+
|
634
|
+
describe "#validates_numericality_of" do
|
635
|
+
|
636
|
+
it "fails if the field is not a number" do
|
637
|
+
Person.class_eval do
|
638
|
+
validates_numericality_of :age
|
639
|
+
end
|
640
|
+
@person.age = "foo"
|
641
|
+
@person.valid?.should be_false
|
642
|
+
@person.errors.on(:age).should_not be_nil
|
643
|
+
end
|
644
|
+
|
645
|
+
end
|
646
|
+
|
647
|
+
describe "#validates_presence_of" do
|
648
|
+
|
649
|
+
it "fails if the field is nil" do
|
650
|
+
Person.class_eval do
|
651
|
+
validates_presence_of :title
|
652
|
+
end
|
653
|
+
@person.valid?.should be_false
|
654
|
+
@person.errors.on(:title).should_not be_nil
|
655
|
+
end
|
656
|
+
|
657
|
+
end
|
658
|
+
|
659
|
+
describe "#validates_true_for" do
|
660
|
+
|
661
|
+
it "fails if the logic returns false" do
|
662
|
+
Person.class_eval do
|
663
|
+
validates_true_for :title, :logic => lambda { title == "Esquire" }
|
664
|
+
end
|
665
|
+
@person.valid?.should be_false
|
666
|
+
@person.errors.on(:title).should_not be_nil
|
667
|
+
end
|
668
|
+
|
552
669
|
end
|
553
670
|
|
554
671
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: durran-mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-09-
|
12
|
+
date: 2009-09-27 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|