I'm working on a small DataMapper-like ODM project, and I'm trying to make use of the ActiveModel::Validations
component. However, I ran into a problem while writing tests - I'm using anonymous classes to construct my test schemas, however when it comes to running the validators, the ActiveModel::Name
class is throwing an error:
Class name cannot be blank. You need to supply a name argument when anonymous class given
Here's a simple code example to reproduce:
require 'active_model'
book_class = Class.new do
include ActiveModel::Validations
validates_presence_of :title
def title; ""; end # This will fail validation
end
book_class.new.valid? # => throws error
The exception is only raised when there's a failed validator - I'm guessing the problem happens when it tries to construct the validation error message. So my question is:
- I did a lot of searching, but couldn't find anyone trying to do something similar. Is this simply not possible with ActiveModel, or is there a workaround I'm not aware of?
ActiveModel::Name
can take a class name as an argument, but didn't spot that I can return my own instance viaself.model_name
. Another reason not to program late at night... :P – Prognosis