I want custom errors.as_json behavior from my implementation of
ActiveModel and with inheritance!
So how do I do something like shown in the following pseudo?
Ruby doesn’t work that way. You’d need to override to_json on the
ActiveModel::Errors class. if you want to be able to have different
classes have an errors object that behave in different ways then you
could try subclassing ActiveModel::Errors and have the errors method
on your object return an instance of that subclass rather than an
instance of ActiveModel::Errors
could try subclassing ActiveModel::Errors and have the errors method
on your object return an instance of that subclass rather than an
instance of ActiveModel::Errors
yet another option would be to redefine the errors method to be
def errors
super.tap {|e| e.extend ExtraErrorsBehaviour}
end
where ExtraErrorsBehaviour is a module that overrides some of the
methods on the errors object (I vaguely recall an railsconf talk a
long time ago by the jruby guys saying that calling extend forces ruby
to dump its method caches, i.e. calling extend a lot will slow you
down a bit but I’ve no idea if that applies to ruby 1.9.x)
Fred
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.