https://github.com/zapnap/mongoid-embedded-errors
Easily bubble up errors from embedded documents in Mongoid.
https://github.com/zapnap/mongoid-embedded-errors
Last synced: 9 months ago
JSON representation
Easily bubble up errors from embedded documents in Mongoid.
- Host: GitHub
- URL: https://github.com/zapnap/mongoid-embedded-errors
- Owner: zapnap
- License: mit
- Created: 2012-12-03T18:30:32.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2012-12-03T18:39:07.000Z (about 13 years ago)
- Last Synced: 2025-04-07T17:04:23.821Z (10 months ago)
- Language: Ruby
- Homepage: http://www.markbates.com
- Size: 99.6 KB
- Stars: 0
- Watchers: 1
- Forks: 31
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Mongoid::EmbeddedErrors
Easily bubble up errors from embedded documents in Mongoid.
## Installation
Add this line to your application's Gemfile:
gem 'mongoid-embedded-errors'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mongoid-embedded-errors
## Usage
Embedded documents in Mongoid can be really useful. However, when one of those embedded documents is invalid, Mongoid spits up completely useless errors.
Let's look an example. Here we have an `Article` which `embeds_many :pages`. A `Page` `embeds_many :sections`.
```ruby
class Article
include Mongoid::Document
field :name, type: String
field :summary, type: String
validates :name, presence: true
validates :summary, presence: true
embeds_many :pages
end
class Page
include Mongoid::Document
field :title, type: String
validates :title, presence: true
embedded_in :article, inverse_of: :pages
embeds_many :sections
end
class Section
include Mongoid::Document
field :header, type: String
field :body, type: String
validates :header, presence: true
embedded_in :page, inverse_of: :sections
end
```
If we were to create an invalid `Article` with an invalid `Page` and tried to validate it the errors we see would not be very helpful:
```ruby
article = Article.new(pages: [Page.new])
article.valid? # => false
article.error.messages
# => {:name=>["can't be blank"], :summary=>["can't be blank"], :pages=>["is invalid"]}
```
Why was the `Page` invalid? Who knows! But, if we include the `Mongoid::EmbeddedErrors` module we get much better error messaging:
```ruby
class Article
include Mongoid::Document
include Mongoid::EmbeddedErrors
field :name, type: String
field :summary, type: String
validates :name, presence: true
validates :summary, presence: true
embeds_many :pages
end
article = Article.new(pages: [Page.new(sections: [Section.new])])
article.valid? # => false
article.error.messages
# => {:name=>["can't be blank"], :summary=>["can't be blank"], :pages=>[{"5086ec0d421aa94f0f000002"=>{:title=>["can't be blank"], :sections=>[{"5086ec0d421aa94f0f000001"=>{:header=>["can't be blank"]}}]}}]}
```
Now, isn't that much nicer? Yeah, I think so to.
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request