Hey, 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? module A include ActiveModel def errors.to_json "blow" end end module B include A def errors.to_json "go " + super end end So far, I''ve been fooling around using instance_eval on errors in A, but I can''t get inheritance from that in B. Lille -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Nov-16 08:30 UTC
Re: I want to override ActiveModel#errors.as_json, but how?
On Nov 16, 2:52 am, Lille <lille.pengu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey, > > 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 Fred> module A > include ActiveModel > > def errors.to_json > "blow" > end > end > > module B > include A > > def errors.to_json > "go " + super > end > end > > So far, I''ve been fooling around using instance_eval on errors in A, > but I can''t get inheritance from that in B. > > Lille-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Nov-16 08:34 UTC
Re: I want to override ActiveModel#errors.as_json, but how?
On Nov 16, 8:30 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Nov 16, 2:52 am, Lille <lille.pengu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey, > > > 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 >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> > > > > module A > > include ActiveModel > > > def errors.to_json > > "blow" > > end > > end > > > module B > > include A > > > def errors.to_json > > "go " + super > > end > > end > > > So far, I''ve been fooling around using instance_eval on errors in A, > > but I can''t get inheritance from that in B. > > > Lille-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.