Proposal to expose a class or object of ActiveModel::Errors caller

18 views
Skip to first unread message

Ivan Malykh

unread,
Aug 10, 2016, 11:09:34 AM8/10/16
to Ruby on Rails: Core
ActiveInteraction and a big amount of other frameworks are using ActiveModel::Errors to implement errors.

When combined with other gems or frameworks that are using ActiveModel it is difficult to find out what class have called `ActiveModel::Errors.new(self)`. For example the following case:

module MyAwesomeFramework
 
class Errors < ActiveModel::Errors
   
def merge!(other_instance_of_active_model_errors)
     
if other
_instance_of_active_model_errors.called_by? User # User is an ActiveRecord::Base
       
# Use ActiveModel's error translations
      else
       
# Do magic to decide what translations to use
     
end
   
end
 
end
end



That is why I propose to expose something like

class ActiveModel::Errors

 
#...

 
def called_by?(klass)
   
caller.class == klass
 
end

 
def caller
   
@base
 
end

 
#...

end

Not sure about the name of method however.


Thanks in advance, 

Ivan
Reply all
Reply to author
Forward
0 new messages