Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: about 2 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 12 years ago)
- Default Branch: master
- Last Pushed: 2012-12-03T18:39:07.000Z (about 12 years ago)
- Last Synced: 2023-04-10T21:52:52.911Z (over 1 year ago)
- Language: Ruby
- Homepage: http://www.markbates.com
- Size: 99.6 KB
- Stars: 0
- Watchers: 2
- 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::Documentfield :name, type: String
field :summary, type: String
validates :name, presence: true
validates :summary, presence: trueembeds_many :pages
endclass Page
include Mongoid::Documentfield :title, type: String
validates :title, presence: trueembedded_in :article, inverse_of: :pages
embeds_many :sections
endclass Section
include Mongoid::Documentfield :header, type: String
field :body, type: String
validates :header, presence: trueembedded_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? # => falsearticle.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::EmbeddedErrorsfield :name, type: String
field :summary, type: String
validates :name, presence: true
validates :summary, presence: trueembeds_many :pages
endarticle = Article.new(pages: [Page.new(sections: [Section.new])])
article.valid? # => falsearticle.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