Permalink
Browse files

added embed_one association matcher

  • Loading branch information...
1 parent d0acb20 commit dd56c13f6fb199426cf6884f414839e6b2fc0628 Francesco Rodriguez committed Apr 14, 2012
Showing with 20 additions and 2 deletions.
  1. +1 −0 CHANGELOG.md
  2. +4 −2 README.md
  3. +7 −0 lib/matchers/associations/associations.rb
  4. +2 −0 test/matchers/associations_test.rb
  5. +6 −0 test/models/models.rb
View
@@ -1,5 +1,6 @@
### 0.0.3
++ Added `embed_one(association_name)` association matcher.
+ Added `have_and_belong_to_many(association_name)` association matcher.
+ Added `validate_associated(association_name) validation matcher'.
+ Added `accept_with(value)` chain matcher to `validate_acceptance_of`.
View
@@ -135,9 +135,11 @@ See the following examples:
describe Person do
subject { Person }
- it { must have_one(:account).of_type(Account) }
+ it { must have_one(:account) }
it { must have_many(:pets).of_type(Pet) }
- it { must have_and_belong_to_many(:friends).of_type(Person) }
+ it { must have_and_belong_to_many(:friends) }
+
+ it { must embed_one(:profile) }
end
describe Pet do
@@ -5,6 +5,7 @@ module Associations
HAS_MANY = Mongoid::Relations::Referenced::Many
HAS_AND_BELONGS_TO_MANY = Mongoid::Relations::Referenced::ManyToMany
BELONGS_TO = Mongoid::Relations::Referenced::In
+ EMBEDS_ONE = Mongoid::Relations::Embedded::One
class HaveAssociationMatcher
include Helpers
@@ -91,6 +92,8 @@ def type_description(type = nil, passive = true)
(passive ? "reference" : "references") << " and referenced in many"
when BELONGS_TO.name
(passive ? "be referenced" : "referenced") << " in"
+ when EMBEDS_ONE.name
+ (passive ? "embed" : "embeds") << " one"
else
raise "Unknown association type #{type}"
end
@@ -112,6 +115,10 @@ def have_and_belong_to_many(association_name)
def belong_to(association_name)
HaveAssociationMatcher.new(association_name, BELONGS_TO)
end
+
+ def embed_one(association_name)
+ HaveAssociationMatcher.new(association_name, EMBEDS_ONE)
+ end
end
end
end
@@ -8,6 +8,8 @@
it { must have_many(:pets).of_type(Pet) }
it { must have_one(:account).of_type(Account) }
it { must have_and_belong_to_many(:friends).of_type(Person) }
+
+ it { must embed_one(:profile) }
end
describe Pet do
View
@@ -16,6 +16,8 @@ class Person
has_many :pets
has_and_belongs_to_many :friends, class_name: "Person"
+ embeds_one :profile
+
validates_presence_of(:name)
validates_presence_of(:role, message: "no role")
@@ -45,3 +47,7 @@ class Account
belongs_to :person
end
+
+class Profile
+ include Mongoid::Document
+end

0 comments on commit dd56c13

Please sign in to comment.