redding/mr — pull request #178 by jcredding (2014-02-10 23:30:57+00:00)
Title: Tweak `Model` and `FakeRecord` save logic

This tweaks the `Model` and `FakeRecord` save logic. I noticed
while using a `Model` with a real `Record` backing it, that it
would call validations twice when saving (I saw this while
watching ActiveRecord logs). This is inefficient especially with
uniqueness validations. They will hit the DB twice. To avoid this,
`Model#save` no longer calls `valid?` and manually raises an
exception. Instead, it relies on `save!` properly raising
exceptions. This is a cleaner and slightly more efficient
implementation than the previous one.

While doing this, I discovered why I had gone with the previous
implementation. Since we now rely on `save!` to raise an exception,
`FakeRecord#save!` needs to fake this behavior. The annoyance with
this is raising an `ActiveRecord::RecordInvalid` is annoying. It
requires having `errors` return an instance of
`ActiveModel::Errors` (thankfully I had already switched to this)
and it requires a class method, `human_attribute_name`. Thus,
`FakeRecord` also had to fake this method. Doing this, allows
passing a `FakeRecord` instance when building an invalid error
and allows `FakeRecord#save!` to fake the behavior correctly.