Skip to content

Commit

Permalink
added embed_one association matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco Rodriguez committed Apr 14, 2012
1 parent d0acb20 commit dd56c13
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -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`.
Expand Down
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/matchers/associations/associations.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
2 changes: 2 additions & 0 deletions test/matchers/associations_test.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/models/models.rb
Expand Up @@ -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")

Expand Down Expand Up @@ -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.