mongoid-eager-loading 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rvmrc.example +2 -0
- data/.watchr +24 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +54 -0
- data/LICENSE +20 -0
- data/README.md +35 -0
- data/Rakefile +45 -0
- data/lib/mongoid-eager-loading.rb +8 -0
- data/lib/mongoid-eager-loading/mongoid/criteria.rb +18 -0
- data/lib/mongoid-eager-loading/mongoid/criterion/eager_loading.rb +116 -0
- data/lib/mongoid-eager-loading/mongoid/finders.rb +5 -0
- data/lib/mongoid-eager-loading/version.rb +5 -0
- data/mongoid-eager-loading.gemspec +28 -0
- data/spec/config/mongoid.yml +21 -0
- data/spec/models/account.rb +10 -0
- data/spec/models/address.rb +52 -0
- data/spec/models/agent.rb +8 -0
- data/spec/models/animal.rb +15 -0
- data/spec/models/answer.rb +4 -0
- data/spec/models/callbacks.rb +47 -0
- data/spec/models/category.rb +13 -0
- data/spec/models/comment.rb +10 -0
- data/spec/models/country_code.rb +6 -0
- data/spec/models/description.rb +8 -0
- data/spec/models/employer.rb +5 -0
- data/spec/models/favorite.rb +8 -0
- data/spec/models/game.rb +14 -0
- data/spec/models/inheritance.rb +72 -0
- data/spec/models/location.rb +5 -0
- data/spec/models/login.rb +6 -0
- data/spec/models/mixed_drink.rb +4 -0
- data/spec/models/name.rb +13 -0
- data/spec/models/namespacing.rb +11 -0
- data/spec/models/paranoid_post.rb +18 -0
- data/spec/models/parents.rb +32 -0
- data/spec/models/patient.rb +15 -0
- data/spec/models/person.rb +113 -0
- data/spec/models/pet.rb +7 -0
- data/spec/models/pet_owner.rb +6 -0
- data/spec/models/phone.rb +7 -0
- data/spec/models/post.rb +25 -0
- data/spec/models/preference.rb +7 -0
- data/spec/models/question.rb +8 -0
- data/spec/models/survey.rb +6 -0
- data/spec/models/translation.rb +5 -0
- data/spec/models/user.rb +8 -0
- data/spec/models/user_account.rb +9 -0
- data/spec/models/vet_visit.rb +5 -0
- data/spec/models/video.rb +5 -0
- data/spec/mongoid-eager-loading/mongoid/criteria_spec.rb +26 -0
- data/spec/mongoid-eager-loading/mongoid/criterion/eager_loading_spec.rb +103 -0
- data/spec/spec_helper.rb +29 -0
- metadata +213 -0
@@ -0,0 +1,15 @@
|
|
1
|
+
class Animal
|
2
|
+
include Mongoid::Document
|
3
|
+
field :name
|
4
|
+
field :tags, :type => Array
|
5
|
+
key :name
|
6
|
+
embedded_in :person, :inverse_of => :pet
|
7
|
+
|
8
|
+
def tag_list
|
9
|
+
tags.join(", ")
|
10
|
+
end
|
11
|
+
|
12
|
+
def tag_list=(_tag_list)
|
13
|
+
self.tags = _tag_list.split(",").map(&:strip)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
class Artist
|
2
|
+
include Mongoid::Document
|
3
|
+
field :name
|
4
|
+
embeds_many :songs
|
5
|
+
embeds_many :labels
|
6
|
+
|
7
|
+
before_create :before_create_stub
|
8
|
+
after_create :create_songs
|
9
|
+
|
10
|
+
protected
|
11
|
+
def before_create_stub
|
12
|
+
true
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_songs
|
16
|
+
2.times { |n| songs.create!(:title => "#{n}") }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Song
|
21
|
+
include Mongoid::Document
|
22
|
+
field :title
|
23
|
+
embedded_in :artist, :inverse_of => :songs
|
24
|
+
end
|
25
|
+
|
26
|
+
class Label
|
27
|
+
include Mongoid::Document
|
28
|
+
field :name
|
29
|
+
embedded_in :artist, :inverse_of => :labels
|
30
|
+
before_validation :cleanup
|
31
|
+
|
32
|
+
private
|
33
|
+
def cleanup
|
34
|
+
self.name = self.name.downcase.capitalize
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class ValidationCallback
|
39
|
+
include Mongoid::Document
|
40
|
+
field :history, :type => Array, :default => []
|
41
|
+
validate do
|
42
|
+
self.history << :validate
|
43
|
+
end
|
44
|
+
|
45
|
+
before_validation { self.history << :before_validation }
|
46
|
+
after_validation { self.history << :after_validation }
|
47
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class RootCategory
|
2
|
+
include Mongoid::Document
|
3
|
+
embeds_many :categories
|
4
|
+
end
|
5
|
+
|
6
|
+
class Category
|
7
|
+
include Mongoid::Document
|
8
|
+
embedded_in :root_category, :inverse_of => :categories
|
9
|
+
embedded_in :category, :inverse_of => :categories
|
10
|
+
embeds_many :categories
|
11
|
+
|
12
|
+
field :name
|
13
|
+
end
|
data/spec/models/game.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
class Game
|
2
|
+
include Mongoid::Document
|
3
|
+
field :high_score, :type => Integer, :default => 500
|
4
|
+
field :score, :type => Integer, :default => 0
|
5
|
+
field :name
|
6
|
+
referenced_in :person, :index => true
|
7
|
+
enslave and cache
|
8
|
+
|
9
|
+
attr_protected :_id
|
10
|
+
|
11
|
+
set_callback(:initialize, :after) do |document|
|
12
|
+
write_attribute("name", "Testing")
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
class Canvas
|
2
|
+
include Mongoid::Document
|
3
|
+
field :name
|
4
|
+
embeds_many :shapes
|
5
|
+
embeds_one :writer
|
6
|
+
embeds_one :palette
|
7
|
+
|
8
|
+
def render
|
9
|
+
shapes.each { |shape| render }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Browser < Canvas
|
14
|
+
field :version, :type => Integer
|
15
|
+
def render; end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Firefox < Browser
|
19
|
+
field :user_agent
|
20
|
+
def render; end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Shape
|
24
|
+
include Mongoid::Document
|
25
|
+
field :x, :type => Integer, :default => 0
|
26
|
+
field :y, :type => Integer, :default => 0
|
27
|
+
|
28
|
+
embedded_in :canvas, :inverse_of => :shapes
|
29
|
+
|
30
|
+
def render; end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Square < Shape
|
34
|
+
field :width, :type => Integer, :default => 0
|
35
|
+
field :height, :type => Integer, :default => 0
|
36
|
+
end
|
37
|
+
|
38
|
+
class Circle < Shape
|
39
|
+
field :radius, :type => Integer, :default => 0
|
40
|
+
end
|
41
|
+
|
42
|
+
class Writer
|
43
|
+
include Mongoid::Document
|
44
|
+
field :speed, :type => Integer, :default => 0
|
45
|
+
|
46
|
+
embedded_in :canvas, :inverse_of => :writer
|
47
|
+
|
48
|
+
def write; end
|
49
|
+
end
|
50
|
+
|
51
|
+
class HtmlWriter < Writer
|
52
|
+
def write; end
|
53
|
+
end
|
54
|
+
|
55
|
+
class PdfWriter < Writer
|
56
|
+
def write; end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Palette
|
60
|
+
include Mongoid::Document
|
61
|
+
embedded_in :canvas, :inverse_of => :palette
|
62
|
+
embeds_many :tools
|
63
|
+
end
|
64
|
+
|
65
|
+
class Tool
|
66
|
+
include Mongoid::Document
|
67
|
+
embedded_in :palette, :inverse_of => :tools
|
68
|
+
end
|
69
|
+
|
70
|
+
class Pencil < Tool; end
|
71
|
+
|
72
|
+
class Eraser < Tool; end
|
data/spec/models/name.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
class Name
|
2
|
+
include Mongoid::Document
|
3
|
+
field :first_name
|
4
|
+
field :last_name
|
5
|
+
field :parent_title
|
6
|
+
key :first_name, :last_name
|
7
|
+
embeds_many :translations
|
8
|
+
embedded_in :namable, :inverse_of => [:name, :names]
|
9
|
+
|
10
|
+
def set_parent=(set = false)
|
11
|
+
self.parent_title = namable.title if set
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class ParanoidPost
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::Versioning
|
4
|
+
include Mongoid::Timestamps
|
5
|
+
include Mongoid::Paranoia
|
6
|
+
field :title
|
7
|
+
referenced_in :person
|
8
|
+
|
9
|
+
references_many :tags, :stored_as => :array
|
10
|
+
|
11
|
+
named_scope :recent, where(:created_at => { "$lt" => Time.now, "$gt" => 30.days.ago })
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def old
|
15
|
+
where(:created_at => { "$lt" => 30.days.ago })
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class ParentDoc
|
2
|
+
include Mongoid::Document
|
3
|
+
|
4
|
+
embeds_many :child_docs
|
5
|
+
|
6
|
+
field :statistic
|
7
|
+
field :children_order, :type => Array, :default => [] # hold all the children's id
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
class ChildDoc
|
12
|
+
include Mongoid::Document
|
13
|
+
|
14
|
+
embedded_in :parent_doc, :inverse_of => :child_docs
|
15
|
+
|
16
|
+
attr_writer :position
|
17
|
+
|
18
|
+
after_save :update_position
|
19
|
+
|
20
|
+
def position
|
21
|
+
exsited_position = parent_doc.children_order.index(id)
|
22
|
+
exsited_position ? exsited_position + 1 : parent_doc.aspects.size
|
23
|
+
end
|
24
|
+
|
25
|
+
def update_position
|
26
|
+
if @position && (@position.to_i > 0)
|
27
|
+
parent_doc.children_order.delete(id)
|
28
|
+
parent_doc.children_order.insert(@position.to_i - 1, id)
|
29
|
+
parent_doc.save
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Email
|
2
|
+
include Mongoid::Document
|
3
|
+
field :address
|
4
|
+
validates_uniqueness_of :address
|
5
|
+
embedded_in :patient, :inverse_of => :email
|
6
|
+
end
|
7
|
+
|
8
|
+
class Patient
|
9
|
+
include Mongoid::Document
|
10
|
+
field :title
|
11
|
+
store_in :inpatient
|
12
|
+
embeds_many :addresses
|
13
|
+
embeds_one :email
|
14
|
+
validates_presence_of :title, :on => :create
|
15
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
class Person
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::Timestamps
|
4
|
+
include Mongoid::EagerLoading
|
5
|
+
|
6
|
+
attr_accessor :mode
|
7
|
+
|
8
|
+
field :title
|
9
|
+
field :terms, :type => Boolean
|
10
|
+
field :pets, :type => Boolean, :default => false
|
11
|
+
field :age, :type => Integer, :default => 100
|
12
|
+
field :dob, :type => Date
|
13
|
+
field :mixed_drink, :type => MixedDrink
|
14
|
+
field :employer_id
|
15
|
+
field :lunch_time, :type => Time
|
16
|
+
field :aliases, :type => Array
|
17
|
+
field :map, :type => Hash
|
18
|
+
field :score, :type => Integer
|
19
|
+
field :blood_alcohol_content, :type => Float, :default => lambda{ 0.0 }
|
20
|
+
field :last_drink_taken_at, :type => Date, :default => lambda { 1.day.ago.in_time_zone("Alaska") }
|
21
|
+
field :ssn
|
22
|
+
field :owner_id, :type => Integer
|
23
|
+
field :security_code
|
24
|
+
field :reading, :type => Object
|
25
|
+
|
26
|
+
index :age
|
27
|
+
index :addresses
|
28
|
+
index :dob
|
29
|
+
index :name
|
30
|
+
index :title
|
31
|
+
index :ssn, :unique => true
|
32
|
+
|
33
|
+
attr_reader :rescored
|
34
|
+
|
35
|
+
attr_protected :security_code, :owner_id
|
36
|
+
|
37
|
+
embeds_many :favorites
|
38
|
+
embeds_many :videos
|
39
|
+
embeds_many :phone_numbers, :class_name => "Phone"
|
40
|
+
embeds_many :addresses do
|
41
|
+
def extension
|
42
|
+
"Testing"
|
43
|
+
end
|
44
|
+
def find_by_street(street)
|
45
|
+
@target.select { |doc| doc.street == street }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
embeds_one :pet, :class_name => "Animal"
|
50
|
+
embeds_one :name do
|
51
|
+
def extension
|
52
|
+
"Testing"
|
53
|
+
end
|
54
|
+
def dawkins?
|
55
|
+
first_name == "Richard" && last_name == "Dawkins"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
accepts_nested_attributes_for :addresses, :reject_if => lambda { |attrs| attrs["street"].blank? }
|
60
|
+
accepts_nested_attributes_for :name, :update_only => true
|
61
|
+
accepts_nested_attributes_for :pet, :allow_destroy => true
|
62
|
+
accepts_nested_attributes_for :game, :allow_destroy => true
|
63
|
+
accepts_nested_attributes_for :favorites, :allow_destroy => true, :limit => 5
|
64
|
+
|
65
|
+
references_one :game, :dependent => :destroy do
|
66
|
+
def extension
|
67
|
+
"Testing"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
references_many :posts, :dependent => :delete do
|
72
|
+
def extension
|
73
|
+
"Testing"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
references_many :paranoid_posts
|
77
|
+
references_many :preferences, :stored_as => :array, :inverse_of => :people, :index => true
|
78
|
+
references_many :user_accounts, :stored_as => :array, :inverse_of => :person
|
79
|
+
|
80
|
+
def score_with_rescoring=(score)
|
81
|
+
@rescored = score.to_i + 20
|
82
|
+
self.score_without_rescoring = score
|
83
|
+
end
|
84
|
+
|
85
|
+
alias_method_chain :score=, :rescoring
|
86
|
+
|
87
|
+
def update_addresses
|
88
|
+
addresses.each do |address|
|
89
|
+
address.street = "Updated Address"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def employer=(emp)
|
94
|
+
self.employer_id = emp.id
|
95
|
+
end
|
96
|
+
|
97
|
+
class << self
|
98
|
+
def accepted
|
99
|
+
criteria.where(:terms => true)
|
100
|
+
end
|
101
|
+
def knight
|
102
|
+
criteria.where(:title => "Sir")
|
103
|
+
end
|
104
|
+
def old
|
105
|
+
criteria.where(:age => { "$gt" => 50 })
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
class Doctor < Person
|
112
|
+
field :specialty
|
113
|
+
end
|