Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

copy spec models from mongoid

  • Loading branch information...
commit 525e2d65f549ac2f8c4980defc9584986840dad4 1 parent 46f92d6
@flyerhzm authored
Showing with 243 additions and 67 deletions.
  1. +11 −5 spec/models/account.rb
  2. +8 −1 spec/models/address.rb
  3. +5 −0 spec/models/address_component.rb
  4. +2 −3 spec/models/agent.rb
  5. +8 −5 spec/models/animal.rb
  6. +1 −1  spec/models/answer.rb
  7. +5 −0 spec/models/bar.rb
  8. +5 −0 spec/models/book.rb
  9. +7 −0 spec/models/business.rb
  10. +2 −2 spec/models/callbacks.rb
  11. +3 −3 spec/models/category.rb
  12. +1 −1  spec/models/country_code.rb
  13. +3 −0  spec/models/description.rb
  14. +5 −0 spec/models/drug.rb
  15. +1 −3 spec/models/favorite.rb
  16. +11 −0 spec/models/fruits.rb
  17. +2 −1  spec/models/game.rb
  18. +4 −0 spec/models/house.rb
  19. +19 −4 spec/models/inheritance.rb
  20. +1 −1  spec/models/location.rb
  21. +4 −0 spec/models/membership.rb
  22. +5 −0 spec/models/movie.rb
  23. +1 −1  spec/models/name.rb
  24. +5 −0 spec/models/page.rb
  25. +4 −0 spec/models/page_question.rb
  26. +1 −1  spec/models/paranoid_post.rb
  27. +1 −1  spec/models/parents.rb
  28. +2 −2 spec/models/patient.rb
  29. +27 −12 spec/models/person.rb
  30. +1 −1  spec/models/pet.rb
  31. +1 −1  spec/models/pet_owner.rb
  32. +1 −1  spec/models/phone.rb
  33. +6 −4 spec/models/post.rb
  34. +2 −1  spec/models/preference.rb
  35. +3 −3 spec/models/question.rb
  36. +4 −0 spec/models/quiz.rb
  37. +6 −0 spec/models/rating.rb
  38. +5 −0 spec/models/role.rb
  39. +5 −0 spec/models/shelf.rb
  40. +14 −0 spec/models/slave_address_numbers.rb
  41. +1 −2  spec/models/survey.rb
  42. +25 −0 spec/models/tracking_id_validation_history.rb
  43. +1 −1  spec/models/translation.rb
  44. +3 −2 spec/models/user.rb
  45. +3 −2 spec/models/user_account.rb
  46. +1 −1  spec/models/vet_visit.rb
  47. +1 −1  spec/models/video.rb
  48. +6 −0 spec/models/wiki_page.rb
View
16 spec/models/account.rb
@@ -1,10 +1,16 @@
class Account
include Mongoid::Document
+ field :number, :type => String
+ field :balance, :type => String
+ field :nickname, :type => String
+ field :name, :type => String
+
+ embeds_many :memberships
referenced_in :creator, :class_name => "User", :foreign_key => :creator_id
referenced_in :person
- field :number
- field :balance
- field :nickname
-
- attr_accessible :nickname
+
+ attr_accessible :nickname, :name
+
+ validates_presence_of :name
+ validates_length_of :name, :maximum => 10, :on => :create
end
View
9 spec/models/address.rb
@@ -15,7 +15,7 @@ class Address
key :street
embeds_many :locations
- embedded_in :addressable, :inverse_of => :addresses do
+ embedded_in :addressable, :polymorphic => true do
def extension
"Testing"
end
@@ -24,6 +24,8 @@ def doctor?
end
end
+ referenced_in :account
+
named_scope :rodeo, where(:street => "Rodeo Dr") do
def mansion?
all? { |address| address.street == "Rodeo Dr" }
@@ -31,11 +33,16 @@ def mansion?
end
validates_presence_of :street, :on => :update
+ validates_format_of :street, :with => /\D/, :allow_nil => true
def set_parent=(set = false)
self.parent_title = addressable.title if set
end
+ def <=>(other)
+ street <=> other.street
+ end
+
class << self
def california
where(:state => "CA")
View
5 spec/models/address_component.rb
@@ -0,0 +1,5 @@
+class AddressComponent
+ include Mongoid::Document
+ field :street, :type => String
+ embedded_in :person
+end
View
5 spec/models/agent.rb
@@ -2,7 +2,6 @@ class Agent
include Mongoid::Document
field :title
field :number
- embeds_many :names
- references_many :posts, :foreign_key => :poster_id
- accepts_nested_attributes_for :posts, :allow_destroy => true
+ embeds_many :names, :as => :namable
+ referenced_in :game
end
View
13 spec/models/animal.rb
@@ -1,15 +1,18 @@
class Animal
include Mongoid::Document
field :name
- field :tags, :type => Array
+ field :tags, :type => Array
key :name
- embedded_in :person, :inverse_of => :pet
-
+
+ embedded_in :person
+
+ accepts_nested_attributes_for :person
+
def tag_list
tags.join(", ")
end
-
+
def tag_list=(_tag_list)
self.tags = _tag_list.split(",").map(&:strip)
- end
+ end
end
View
2  spec/models/answer.rb
@@ -1,4 +1,4 @@
class Answer
include Mongoid::Document
- embedded_in :question, :inverse_of => :answers
+ embedded_in :question
end
View
5 spec/models/bar.rb
@@ -0,0 +1,5 @@
+class Bar
+ include Mongoid::Document
+ field :name, :type => String
+ references_one :rating, :as => :ratable
+end
View
5 spec/models/book.rb
@@ -0,0 +1,5 @@
+class Book
+ include Mongoid::Document
+ field :title, :type => String
+ references_one :rating, :as => :ratable, :dependent => :nullify
+end
View
7 spec/models/business.rb
@@ -0,0 +1,7 @@
+class Business
+ include Mongoid::Document
+
+ set_database :secondary
+
+ field :name
+end
View
4 spec/models/callbacks.rb
@@ -20,13 +20,13 @@ def create_songs
class Song
include Mongoid::Document
field :title
- embedded_in :artist, :inverse_of => :songs
+ embedded_in :artist
end
class Label
include Mongoid::Document
field :name
- embedded_in :artist, :inverse_of => :labels
+ embedded_in :artist
before_validation :cleanup
private
View
6 spec/models/category.rb
@@ -5,9 +5,9 @@ class RootCategory
class Category
include Mongoid::Document
- embedded_in :root_category, :inverse_of => :categories
- embedded_in :category, :inverse_of => :categories
+ embedded_in :root_category
+ embedded_in :category
embeds_many :categories
field :name
-end
+end
View
2  spec/models/country_code.rb
@@ -2,5 +2,5 @@ class CountryCode
include Mongoid::Document
field :code, :type => Integer
key :code
- embedded_in :phone_number, :inverse_of => :country_codes
+ embedded_in :phone_number, :class_name => "Phone"
end
View
3  spec/models/description.rb
@@ -5,4 +5,7 @@ class Description
referenced_in :user
referenced_in :updater, :class_name => 'User'
+
+ validates :user, :associated => true
+ validates :details, :presence => true
end
View
5 spec/models/drug.rb
@@ -0,0 +1,5 @@
+class Drug
+ include Mongoid::Document
+ field :name, :type => String
+ referenced_in :person
+end
View
4 spec/models/favorite.rb
@@ -1,8 +1,6 @@
class Favorite
include Mongoid::Document
-
field :title
validates_uniqueness_of :title, :case_sensitive => false
-
- embedded_in :person, :inverse_of => :favorites
+ embedded_in :perp, :inverse_of => :favorites
end
View
11 spec/models/fruits.rb
@@ -0,0 +1,11 @@
+module Fruits
+ class Apple
+ include Mongoid::Document
+ references_many :bananas, :class_name => "Fruits::Banana"
+ end
+
+ class Banana
+ include Mongoid::Document
+ referenced_in :apple, :class_name => "Fruits::Apple"
+ end
+end
View
3  spec/models/game.rb
@@ -4,11 +4,12 @@ class Game
field :score, :type => Integer, :default => 0
field :name
referenced_in :person, :index => true
+ accepts_nested_attributes_for :person
enslave and cache
attr_protected :_id
set_callback(:initialize, :after) do |document|
- write_attribute("name", "Testing")
+ write_attribute("name", "Testing") unless name
end
end
View
4 spec/models/house.rb
@@ -0,0 +1,4 @@
+class House
+ include Mongoid::Document
+ field :name, :type => String
+end
View
23 spec/models/inheritance.rb
@@ -25,7 +25,7 @@ class Shape
field :x, :type => Integer, :default => 0
field :y, :type => Integer, :default => 0
- embedded_in :canvas, :inverse_of => :shapes
+ embedded_in :canvas
def render; end
end
@@ -43,7 +43,7 @@ class Writer
include Mongoid::Document
field :speed, :type => Integer, :default => 0
- embedded_in :canvas, :inverse_of => :writer
+ embedded_in :canvas
def write; end
end
@@ -58,15 +58,30 @@ def write; end
class Palette
include Mongoid::Document
- embedded_in :canvas, :inverse_of => :palette
+ embedded_in :canvas
embeds_many :tools
end
class Tool
include Mongoid::Document
- embedded_in :palette, :inverse_of => :tools
+ embedded_in :palette
end
class Pencil < Tool; end
class Eraser < Tool; end
+
+########################################
+# These are for references relationships
+########################################
+class ShippingContainer
+ include Mongoid::Document
+ references_many :vehicles
+end
+class Vehicle
+ include Mongoid::Document
+ referenced_in :shipping_container
+end
+class Car < Vehicle; end
+class Truck < Vehicle; end
+
View
2  spec/models/location.rb
@@ -1,5 +1,5 @@
class Location
include Mongoid::Document
field :name
- embedded_in :address, :inverse_of => :locations
+ embedded_in :address
end
View
4 spec/models/membership.rb
@@ -0,0 +1,4 @@
+class Membership
+ include Mongoid::Document
+ embedded_in :account
+end
View
5 spec/models/movie.rb
@@ -0,0 +1,5 @@
+class Movie
+ include Mongoid::Document
+ field :title, :type => String
+ references_many :ratings, :as => :ratable, :dependent => :nullify
+end
View
2  spec/models/name.rb
@@ -5,7 +5,7 @@ class Name
field :parent_title
key :first_name, :last_name
embeds_many :translations
- embedded_in :namable, :inverse_of => [:name, :names]
+ embedded_in :namable, :polymorphic => true
def set_parent=(set = false)
self.parent_title = namable.title if set
View
5 spec/models/page.rb
@@ -0,0 +1,5 @@
+class Page
+ include Mongoid::Document
+ embedded_in :quiz
+ embeds_many :page_questions
+end
View
4 spec/models/page_question.rb
@@ -0,0 +1,4 @@
+class PageQuestion
+ include Mongoid::Document
+ embedded_in :page
+end
View
2  spec/models/paranoid_post.rb
@@ -6,7 +6,7 @@ class ParanoidPost
field :title
referenced_in :person
- references_many :tags, :stored_as => :array
+ references_and_referenced_in_many :tags
named_scope :recent, where(:created_at => { "$lt" => Time.now, "$gt" => 30.days.ago })
View
2  spec/models/parents.rb
@@ -11,7 +11,7 @@ class ParentDoc
class ChildDoc
include Mongoid::Document
- embedded_in :parent_doc, :inverse_of => :child_docs
+ embedded_in :parent_doc
attr_writer :position
View
4 spec/models/patient.rb
@@ -2,14 +2,14 @@ class Email
include Mongoid::Document
field :address
validates_uniqueness_of :address
- embedded_in :patient, :inverse_of => :email
+ embedded_in :patient
end
class Patient
include Mongoid::Document
field :title
store_in :inpatient
- embeds_many :addresses
+ embeds_many :addresses, :as => :addressable
embeds_one :email
validates_presence_of :title, :on => :create
end
View
39 spec/models/person.rb
@@ -21,6 +21,7 @@ class Person
field :owner_id, :type => Integer
field :security_code
field :reading, :type => Object
+ field :bson_id, :type => BSON::ObjectId
index :age
index :addresses
@@ -33,10 +34,10 @@ class Person
attr_protected :security_code, :owner_id
- embeds_many :favorites
- embeds_many :videos
- embeds_many :phone_numbers, :class_name => "Phone"
- embeds_many :addresses do
+ embeds_many :favorites, :order => :title.desc, :validate => false, :inverse_of => :perp
+ embeds_many :videos, :order => [[ :title, :asc ]], :validate => false
+ embeds_many :phone_numbers, :class_name => "Phone", :validate => false
+ embeds_many :addresses, :as => :addressable, :validate => false do
def extension
"Testing"
end
@@ -44,9 +45,10 @@ def find_by_street(street)
@target.select { |doc| doc.street == street }
end
end
+ embeds_many :address_components, :validate => false
- embeds_one :pet, :class_name => "Animal"
- embeds_one :name do
+ embeds_one :pet, :class_name => "Animal", :validate => false
+ embeds_one :name, :as => :namable, :validate => false do
def extension
"Testing"
end
@@ -55,26 +57,39 @@ def dawkins?
end
end
- accepts_nested_attributes_for :addresses, :reject_if => lambda { |attrs| attrs["street"].blank? }
+ accepts_nested_attributes_for :addresses
accepts_nested_attributes_for :name, :update_only => true
accepts_nested_attributes_for :pet, :allow_destroy => true
accepts_nested_attributes_for :game, :allow_destroy => true
accepts_nested_attributes_for :favorites, :allow_destroy => true, :limit => 5
+ accepts_nested_attributes_for :posts
+ accepts_nested_attributes_for :preferences
- references_one :game, :dependent => :destroy do
+ references_one :game, :dependent => :destroy, :validate => false do
def extension
"Testing"
end
end
- references_many :posts, :dependent => :delete do
+ references_many \
+ :posts,
+ :dependent => :delete,
+ :validate => :false,
+ :default_order => :created_at.desc do
def extension
"Testing"
end
end
- references_many :paranoid_posts
- references_many :preferences, :stored_as => :array, :inverse_of => :people, :index => true
- references_many :user_accounts, :stored_as => :array, :inverse_of => :person
+ references_many :paranoid_posts, :validate => false
+ references_and_referenced_in_many \
+ :preferences,
+ :index => true,
+ :dependent => :nullify
+ references_and_referenced_in_many :user_accounts
+ references_and_referenced_in_many :houses
+
+ references_many :drugs, :autosave => true
+ references_one :account, :autosave => true
def score_with_rescoring=(score)
@rescored = score.to_i + 20
View
2  spec/models/pet.rb
@@ -3,5 +3,5 @@ class Pet
field :name
field :weight, :type => Float, :default => 0.0
embeds_many :vet_visits
- embedded_in :pet_owner, :inverse_of => :pet
+ embedded_in :pet_owner
end
View
2  spec/models/pet_owner.rb
@@ -2,5 +2,5 @@ class PetOwner
include Mongoid::Document
field :title
embeds_one :pet
- embeds_one :address
+ embeds_one :address, :as => :addressable
end
View
2  spec/models/phone.rb
@@ -3,5 +3,5 @@ class Phone
field :number
key :number
embeds_one :country_code
- embedded_in :person, :inverse_of => :phone_numbers
+ embedded_in :person
end
View
10 spec/models/post.rb
@@ -1,7 +1,7 @@
class Tag
include Mongoid::Document
field :text
- referenced_in :post, :stored_as => :array
+ references_and_referenced_in_many :posts
end
class Post
@@ -9,13 +9,15 @@ class Post
include Mongoid::Versioning
include Mongoid::Timestamps
field :title
+ field :content
referenced_in :person
referenced_in :author, :foreign_key => :author_id, :class_name => "User"
- referenced_in :poster, :foreign_key => :poster_id, :class_name => "Agent"
+ references_and_referenced_in_many :tags
- references_many :tags, :stored_as => :array
+ scope :recent, where(:created_at => { "$lt" => Time.now, "$gt" => 30.days.ago })
+ scope :posting, where(:content.in => [ "Posting" ])
- named_scope :recent, where(:created_at => { "$lt" => Time.now, "$gt" => 30.days.ago })
+ validates_format_of :title, :without => /\$\$\$/
class << self
def old
View
3  spec/models/preference.rb
@@ -2,6 +2,7 @@ class Preference
include Mongoid::Document
field :name
field :value
- references_many :people, :stored_as => :array, :inverse_of => :preferences
+ references_and_referenced_in_many :people
validates_length_of :name, :minimum => 2, :allow_nil => true
+ scope :posting, where(:value.in => [ "Posting" ])
end
View
6 spec/models/question.rb
@@ -1,8 +1,8 @@
class Question
include Mongoid::Document
field :content
- embedded_in :survey, :inverse_of => :questions
+ embedded_in :survey
embeds_many :answers
-
+
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
-end
+end
View
4 spec/models/quiz.rb
@@ -0,0 +1,4 @@
+class Quiz
+ include Mongoid::Document
+ embeds_many :pages
+end
View
6 spec/models/rating.rb
@@ -0,0 +1,6 @@
+class Rating
+ include Mongoid::Document
+ field :value, :type => Integer
+ referenced_in :ratable, :polymorphic => true
+ validates_numericality_of :value, :less_than => 100, :allow_nil => true
+end
View
5 spec/models/role.rb
@@ -0,0 +1,5 @@
+class Role
+ include Mongoid::Document
+ field :name, :type => String
+ recursively_embeds_many
+end
View
5 spec/models/shelf.rb
@@ -0,0 +1,5 @@
+class Shelf
+ include Mongoid::Document
+ field :level, :type => Integer
+ recursively_embeds_one
+end
View
14 spec/models/slave_address_numbers.rb
@@ -0,0 +1,14 @@
+# These models used for Github 263
+class Slave
+ include Mongoid::Document
+ field :first_name
+ field :last_name
+ embeds_many :address_numbers
+end
+
+class AddressNumber
+ include Mongoid::Document
+ field :country_code, :type => Integer, :default => 1
+ field :number
+ embedded_in :slave
+end
View
3  spec/models/survey.rb
@@ -1,6 +1,5 @@
class Survey
include Mongoid::Document
embeds_many :questions
-
accepts_nested_attributes_for :questions, :reject_if => lambda{ |a| a[:content].blank? }, :allow_destroy => true
-end
+end
View
25 spec/models/tracking_id_validation_history.rb
@@ -0,0 +1,25 @@
+# These models are spcific to test for Github #313.
+module MyCompany
+ module Model
+ class TrackingId
+ include Mongoid::Document
+ include Mongoid::Timestamps
+ store_in :tracking_ids
+ embeds_many :validation_history, :class_name => "MyCompany::Model::TrackingIdValidationHistory"
+ end
+ end
+end
+
+module MyCompany
+ module Model
+ # A TrackingId validation state change
+ class TrackingIdValidationHistory
+ include Mongoid::Document
+ field :old_state, :type => String
+ field :new_state, :type => String
+ field :when_changed, :type => DateTime
+ attr_protected :_id
+ embedded_in :tracking_id, :class_name => "MyCompany::Model::TrackingId"
+ end
+ end
+end
View
2  spec/models/translation.rb
@@ -1,5 +1,5 @@
class Translation
include Mongoid::Document
field :language
- embedded_in :name, :inverse_of => :translations
+ embedded_in :name
end
View
5 spec/models/user.rb
@@ -1,8 +1,9 @@
class User
include Mongoid::Document
- references_one :account, :foreign_key => :creator_id
- references_many :posts, :foreign_key => :author_id, :inverse_of => :author
+
field :name
+ references_one :account, :foreign_key => :creator_id
+ references_many :posts, :foreign_key => :author_id
references_many :descriptions
end
View
5 spec/models/user_account.rb
@@ -1,9 +1,10 @@
class UserAccount
include Mongoid::Document
field :username
+ field :name
field :email
validates_uniqueness_of :username, :message => "is not unique"
validates_uniqueness_of :email, :message => "is not unique", :case_sensitive => false
-
- referenced_in :person, :inverse_of => :user_accounts
+ validates_length_of :name, :minimum => 2, :allow_nil => true
+ references_and_referenced_in_many :people
end
View
2  spec/models/vet_visit.rb
@@ -1,5 +1,5 @@
class VetVisit
include Mongoid::Document
field :date, :type => Date
- embedded_in :pet, :inverse_of => :vet_visits
+ embedded_in :pet
end
View
2  spec/models/video.rb
@@ -1,5 +1,5 @@
class Video
include Mongoid::Document
field :title
- embedded_in :person, :inverse_of => :videos
+ embedded_in :person
end
View
6 spec/models/wiki_page.rb
@@ -0,0 +1,6 @@
+class WikiPage
+ include Mongoid::Document
+ include Mongoid::Versioning
+ field :title, :type => String
+ max_versions 5
+end
Please sign in to comment.
Something went wrong with that request. Please try again.