mongoid-eager-loading 0.2.0 → 0.3.0

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.
Files changed (58) hide show
  1. data/Gemfile.lock +13 -12
  2. data/README.md +12 -10
  3. data/benchmark/benchmark.rb +11 -4
  4. data/lib/mongoid-eager-loading.rb +1 -6
  5. data/lib/mongoid-eager-loading/mongoid/criterion/eager_loading.rb +36 -36
  6. data/lib/mongoid-eager-loading/version.rb +1 -1
  7. data/mongoid-eager-loading.gemspec +4 -3
  8. data/spec/models/account.rb +11 -5
  9. data/spec/models/address.rb +8 -1
  10. data/spec/models/address_component.rb +5 -0
  11. data/spec/models/agent.rb +2 -3
  12. data/spec/models/animal.rb +8 -5
  13. data/spec/models/answer.rb +1 -1
  14. data/spec/models/bar.rb +5 -0
  15. data/spec/models/book.rb +5 -0
  16. data/spec/models/business.rb +7 -0
  17. data/spec/models/callbacks.rb +2 -2
  18. data/spec/models/category.rb +3 -3
  19. data/spec/models/country_code.rb +1 -1
  20. data/spec/models/description.rb +3 -0
  21. data/spec/models/drug.rb +5 -0
  22. data/spec/models/favorite.rb +1 -3
  23. data/spec/models/fruits.rb +11 -0
  24. data/spec/models/game.rb +2 -1
  25. data/spec/models/house.rb +4 -0
  26. data/spec/models/inheritance.rb +19 -4
  27. data/spec/models/location.rb +1 -1
  28. data/spec/models/membership.rb +4 -0
  29. data/spec/models/movie.rb +5 -0
  30. data/spec/models/name.rb +1 -1
  31. data/spec/models/page.rb +5 -0
  32. data/spec/models/page_question.rb +4 -0
  33. data/spec/models/paranoid_post.rb +1 -1
  34. data/spec/models/parents.rb +1 -1
  35. data/spec/models/patient.rb +2 -2
  36. data/spec/models/person.rb +27 -12
  37. data/spec/models/pet.rb +1 -1
  38. data/spec/models/pet_owner.rb +1 -1
  39. data/spec/models/phone.rb +1 -1
  40. data/spec/models/post.rb +6 -4
  41. data/spec/models/preference.rb +2 -1
  42. data/spec/models/question.rb +3 -3
  43. data/spec/models/quiz.rb +4 -0
  44. data/spec/models/rating.rb +6 -0
  45. data/spec/models/role.rb +5 -0
  46. data/spec/models/shelf.rb +5 -0
  47. data/spec/models/slave_address_numbers.rb +14 -0
  48. data/spec/models/survey.rb +1 -2
  49. data/spec/models/tracking_id_validation_history.rb +25 -0
  50. data/spec/models/translation.rb +1 -1
  51. data/spec/models/user.rb +3 -2
  52. data/spec/models/user_account.rb +3 -2
  53. data/spec/models/vet_visit.rb +1 -1
  54. data/spec/models/video.rb +1 -1
  55. data/spec/models/wiki_page.rb +6 -0
  56. data/spec/mongoid-eager-loading/mongoid/criterion/eager_loading_spec.rb +19 -0
  57. data/spec/spec_helper.rb +1 -1
  58. metadata +93 -59
@@ -2,5 +2,5 @@ class CountryCode
2
2
  include Mongoid::Document
3
3
  field :code, :type => Integer
4
4
  key :code
5
- embedded_in :phone_number, :inverse_of => :country_codes
5
+ embedded_in :phone_number, :class_name => "Phone"
6
6
  end
@@ -5,4 +5,7 @@ class Description
5
5
 
6
6
  referenced_in :user
7
7
  referenced_in :updater, :class_name => 'User'
8
+
9
+ validates :user, :associated => true
10
+ validates :details, :presence => true
8
11
  end
@@ -0,0 +1,5 @@
1
+ class Drug
2
+ include Mongoid::Document
3
+ field :name, :type => String
4
+ referenced_in :person
5
+ end
@@ -1,8 +1,6 @@
1
1
  class Favorite
2
2
  include Mongoid::Document
3
-
4
3
  field :title
5
4
  validates_uniqueness_of :title, :case_sensitive => false
6
-
7
- embedded_in :person, :inverse_of => :favorites
5
+ embedded_in :perp, :inverse_of => :favorites
8
6
  end
@@ -0,0 +1,11 @@
1
+ module Fruits
2
+ class Apple
3
+ include Mongoid::Document
4
+ references_many :bananas, :class_name => "Fruits::Banana"
5
+ end
6
+
7
+ class Banana
8
+ include Mongoid::Document
9
+ referenced_in :apple, :class_name => "Fruits::Apple"
10
+ end
11
+ end
@@ -4,11 +4,12 @@ class Game
4
4
  field :score, :type => Integer, :default => 0
5
5
  field :name
6
6
  referenced_in :person, :index => true
7
+ accepts_nested_attributes_for :person
7
8
  enslave and cache
8
9
 
9
10
  attr_protected :_id
10
11
 
11
12
  set_callback(:initialize, :after) do |document|
12
- write_attribute("name", "Testing")
13
+ write_attribute("name", "Testing") unless name
13
14
  end
14
15
  end
@@ -0,0 +1,4 @@
1
+ class House
2
+ include Mongoid::Document
3
+ field :name, :type => String
4
+ end
@@ -25,7 +25,7 @@ class Shape
25
25
  field :x, :type => Integer, :default => 0
26
26
  field :y, :type => Integer, :default => 0
27
27
 
28
- embedded_in :canvas, :inverse_of => :shapes
28
+ embedded_in :canvas
29
29
 
30
30
  def render; end
31
31
  end
@@ -43,7 +43,7 @@ class Writer
43
43
  include Mongoid::Document
44
44
  field :speed, :type => Integer, :default => 0
45
45
 
46
- embedded_in :canvas, :inverse_of => :writer
46
+ embedded_in :canvas
47
47
 
48
48
  def write; end
49
49
  end
@@ -58,15 +58,30 @@ end
58
58
 
59
59
  class Palette
60
60
  include Mongoid::Document
61
- embedded_in :canvas, :inverse_of => :palette
61
+ embedded_in :canvas
62
62
  embeds_many :tools
63
63
  end
64
64
 
65
65
  class Tool
66
66
  include Mongoid::Document
67
- embedded_in :palette, :inverse_of => :tools
67
+ embedded_in :palette
68
68
  end
69
69
 
70
70
  class Pencil < Tool; end
71
71
 
72
72
  class Eraser < Tool; end
73
+
74
+ ########################################
75
+ # These are for references relationships
76
+ ########################################
77
+ class ShippingContainer
78
+ include Mongoid::Document
79
+ references_many :vehicles
80
+ end
81
+ class Vehicle
82
+ include Mongoid::Document
83
+ referenced_in :shipping_container
84
+ end
85
+ class Car < Vehicle; end
86
+ class Truck < Vehicle; end
87
+
@@ -1,5 +1,5 @@
1
1
  class Location
2
2
  include Mongoid::Document
3
3
  field :name
4
- embedded_in :address, :inverse_of => :locations
4
+ embedded_in :address
5
5
  end
@@ -0,0 +1,4 @@
1
+ class Membership
2
+ include Mongoid::Document
3
+ embedded_in :account
4
+ end
@@ -0,0 +1,5 @@
1
+ class Movie
2
+ include Mongoid::Document
3
+ field :title, :type => String
4
+ references_many :ratings, :as => :ratable, :dependent => :nullify
5
+ end
@@ -5,7 +5,7 @@ class Name
5
5
  field :parent_title
6
6
  key :first_name, :last_name
7
7
  embeds_many :translations
8
- embedded_in :namable, :inverse_of => [:name, :names]
8
+ embedded_in :namable, :polymorphic => true
9
9
 
10
10
  def set_parent=(set = false)
11
11
  self.parent_title = namable.title if set
@@ -0,0 +1,5 @@
1
+ class Page
2
+ include Mongoid::Document
3
+ embedded_in :quiz
4
+ embeds_many :page_questions
5
+ end
@@ -0,0 +1,4 @@
1
+ class PageQuestion
2
+ include Mongoid::Document
3
+ embedded_in :page
4
+ end
@@ -6,7 +6,7 @@ class ParanoidPost
6
6
  field :title
7
7
  referenced_in :person
8
8
 
9
- references_many :tags, :stored_as => :array
9
+ references_and_referenced_in_many :tags
10
10
 
11
11
  named_scope :recent, where(:created_at => { "$lt" => Time.now, "$gt" => 30.days.ago })
12
12
 
@@ -11,7 +11,7 @@ end
11
11
  class ChildDoc
12
12
  include Mongoid::Document
13
13
 
14
- embedded_in :parent_doc, :inverse_of => :child_docs
14
+ embedded_in :parent_doc
15
15
 
16
16
  attr_writer :position
17
17
 
@@ -2,14 +2,14 @@ class Email
2
2
  include Mongoid::Document
3
3
  field :address
4
4
  validates_uniqueness_of :address
5
- embedded_in :patient, :inverse_of => :email
5
+ embedded_in :patient
6
6
  end
7
7
 
8
8
  class Patient
9
9
  include Mongoid::Document
10
10
  field :title
11
11
  store_in :inpatient
12
- embeds_many :addresses
12
+ embeds_many :addresses, :as => :addressable
13
13
  embeds_one :email
14
14
  validates_presence_of :title, :on => :create
15
15
  end
@@ -21,6 +21,7 @@ class Person
21
21
  field :owner_id, :type => Integer
22
22
  field :security_code
23
23
  field :reading, :type => Object
24
+ field :bson_id, :type => BSON::ObjectId
24
25
 
25
26
  index :age
26
27
  index :addresses
@@ -33,10 +34,10 @@ class Person
33
34
 
34
35
  attr_protected :security_code, :owner_id
35
36
 
36
- embeds_many :favorites
37
- embeds_many :videos
38
- embeds_many :phone_numbers, :class_name => "Phone"
39
- embeds_many :addresses do
37
+ embeds_many :favorites, :order => :title.desc, :validate => false, :inverse_of => :perp
38
+ embeds_many :videos, :order => [[ :title, :asc ]], :validate => false
39
+ embeds_many :phone_numbers, :class_name => "Phone", :validate => false
40
+ embeds_many :addresses, :as => :addressable, :validate => false do
40
41
  def extension
41
42
  "Testing"
42
43
  end
@@ -44,9 +45,10 @@ class Person
44
45
  @target.select { |doc| doc.street == street }
45
46
  end
46
47
  end
48
+ embeds_many :address_components, :validate => false
47
49
 
48
- embeds_one :pet, :class_name => "Animal"
49
- embeds_one :name do
50
+ embeds_one :pet, :class_name => "Animal", :validate => false
51
+ embeds_one :name, :as => :namable, :validate => false do
50
52
  def extension
51
53
  "Testing"
52
54
  end
@@ -55,26 +57,39 @@ class Person
55
57
  end
56
58
  end
57
59
 
58
- accepts_nested_attributes_for :addresses, :reject_if => lambda { |attrs| attrs["street"].blank? }
60
+ accepts_nested_attributes_for :addresses
59
61
  accepts_nested_attributes_for :name, :update_only => true
60
62
  accepts_nested_attributes_for :pet, :allow_destroy => true
61
63
  accepts_nested_attributes_for :game, :allow_destroy => true
62
64
  accepts_nested_attributes_for :favorites, :allow_destroy => true, :limit => 5
65
+ accepts_nested_attributes_for :posts
66
+ accepts_nested_attributes_for :preferences
63
67
 
64
- references_one :game, :dependent => :destroy do
68
+ references_one :game, :dependent => :destroy, :validate => false do
65
69
  def extension
66
70
  "Testing"
67
71
  end
68
72
  end
69
73
 
70
- references_many :posts, :dependent => :delete do
74
+ references_many \
75
+ :posts,
76
+ :dependent => :delete,
77
+ :validate => :false,
78
+ :default_order => :created_at.desc do
71
79
  def extension
72
80
  "Testing"
73
81
  end
74
82
  end
75
- references_many :paranoid_posts
76
- references_many :preferences, :stored_as => :array, :inverse_of => :people, :index => true
77
- references_many :user_accounts, :stored_as => :array, :inverse_of => :person
83
+ references_many :paranoid_posts, :validate => false
84
+ references_and_referenced_in_many \
85
+ :preferences,
86
+ :index => true,
87
+ :dependent => :nullify
88
+ references_and_referenced_in_many :user_accounts
89
+ references_and_referenced_in_many :houses
90
+
91
+ references_many :drugs, :autosave => true
92
+ references_one :account, :autosave => true
78
93
 
79
94
  def score_with_rescoring=(score)
80
95
  @rescored = score.to_i + 20
@@ -3,5 +3,5 @@ class Pet
3
3
  field :name
4
4
  field :weight, :type => Float, :default => 0.0
5
5
  embeds_many :vet_visits
6
- embedded_in :pet_owner, :inverse_of => :pet
6
+ embedded_in :pet_owner
7
7
  end
@@ -2,5 +2,5 @@ class PetOwner
2
2
  include Mongoid::Document
3
3
  field :title
4
4
  embeds_one :pet
5
- embeds_one :address
5
+ embeds_one :address, :as => :addressable
6
6
  end
@@ -3,5 +3,5 @@ class Phone
3
3
  field :number
4
4
  key :number
5
5
  embeds_one :country_code
6
- embedded_in :person, :inverse_of => :phone_numbers
6
+ embedded_in :person
7
7
  end
@@ -1,7 +1,7 @@
1
1
  class Tag
2
2
  include Mongoid::Document
3
3
  field :text
4
- referenced_in :post, :stored_as => :array
4
+ references_and_referenced_in_many :posts
5
5
  end
6
6
 
7
7
  class Post
@@ -9,13 +9,15 @@ class Post
9
9
  include Mongoid::Versioning
10
10
  include Mongoid::Timestamps
11
11
  field :title
12
+ field :content
12
13
  referenced_in :person
13
14
  referenced_in :author, :foreign_key => :author_id, :class_name => "User"
14
- referenced_in :poster, :foreign_key => :poster_id, :class_name => "Agent"
15
+ references_and_referenced_in_many :tags
15
16
 
16
- references_many :tags, :stored_as => :array
17
+ scope :recent, where(:created_at => { "$lt" => Time.now, "$gt" => 30.days.ago })
18
+ scope :posting, where(:content.in => [ "Posting" ])
17
19
 
18
- named_scope :recent, where(:created_at => { "$lt" => Time.now, "$gt" => 30.days.ago })
20
+ validates_format_of :title, :without => /\$\$\$/
19
21
 
20
22
  class << self
21
23
  def old
@@ -2,6 +2,7 @@ class Preference
2
2
  include Mongoid::Document
3
3
  field :name
4
4
  field :value
5
- references_many :people, :stored_as => :array, :inverse_of => :preferences
5
+ references_and_referenced_in_many :people
6
6
  validates_length_of :name, :minimum => 2, :allow_nil => true
7
+ scope :posting, where(:value.in => [ "Posting" ])
7
8
  end
@@ -1,8 +1,8 @@
1
1
  class Question
2
2
  include Mongoid::Document
3
3
  field :content
4
- embedded_in :survey, :inverse_of => :questions
4
+ embedded_in :survey
5
5
  embeds_many :answers
6
-
6
+
7
7
  accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
8
- end
8
+ end
@@ -0,0 +1,4 @@
1
+ class Quiz
2
+ include Mongoid::Document
3
+ embeds_many :pages
4
+ end
@@ -0,0 +1,6 @@
1
+ class Rating
2
+ include Mongoid::Document
3
+ field :value, :type => Integer
4
+ referenced_in :ratable, :polymorphic => true
5
+ validates_numericality_of :value, :less_than => 100, :allow_nil => true
6
+ end
@@ -0,0 +1,5 @@
1
+ class Role
2
+ include Mongoid::Document
3
+ field :name, :type => String
4
+ recursively_embeds_many
5
+ end
@@ -0,0 +1,5 @@
1
+ class Shelf
2
+ include Mongoid::Document
3
+ field :level, :type => Integer
4
+ recursively_embeds_one
5
+ end
@@ -0,0 +1,14 @@
1
+ # These models used for Github 263
2
+ class Slave
3
+ include Mongoid::Document
4
+ field :first_name
5
+ field :last_name
6
+ embeds_many :address_numbers
7
+ end
8
+
9
+ class AddressNumber
10
+ include Mongoid::Document
11
+ field :country_code, :type => Integer, :default => 1
12
+ field :number
13
+ embedded_in :slave
14
+ end
@@ -1,6 +1,5 @@
1
1
  class Survey
2
2
  include Mongoid::Document
3
3
  embeds_many :questions
4
-
5
4
  accepts_nested_attributes_for :questions, :reject_if => lambda{ |a| a[:content].blank? }, :allow_destroy => true
6
- end
5
+ end
@@ -0,0 +1,25 @@
1
+ # These models are spcific to test for Github #313.
2
+ module MyCompany
3
+ module Model
4
+ class TrackingId
5
+ include Mongoid::Document
6
+ include Mongoid::Timestamps
7
+ store_in :tracking_ids
8
+ embeds_many :validation_history, :class_name => "MyCompany::Model::TrackingIdValidationHistory"
9
+ end
10
+ end
11
+ end
12
+
13
+ module MyCompany
14
+ module Model
15
+ # A TrackingId validation state change
16
+ class TrackingIdValidationHistory
17
+ include Mongoid::Document
18
+ field :old_state, :type => String
19
+ field :new_state, :type => String
20
+ field :when_changed, :type => DateTime
21
+ attr_protected :_id
22
+ embedded_in :tracking_id, :class_name => "MyCompany::Model::TrackingId"
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  class Translation
2
2
  include Mongoid::Document
3
3
  field :language
4
- embedded_in :name, :inverse_of => :translations
4
+ embedded_in :name
5
5
  end
@@ -1,8 +1,9 @@
1
1
  class User
2
2
  include Mongoid::Document
3
- references_one :account, :foreign_key => :creator_id
4
- references_many :posts, :foreign_key => :author_id, :inverse_of => :author
3
+
5
4
  field :name
6
5
 
6
+ references_one :account, :foreign_key => :creator_id
7
+ references_many :posts, :foreign_key => :author_id
7
8
  references_many :descriptions
8
9
  end