Skip to content

Instantly share code, notes, and snippets.

@jbmyid
Created June 19, 2015 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbmyid/6f142461b7d6a34928c8 to your computer and use it in GitHub Desktop.
Save jbmyid/6f142461b7d6a34928c8 to your computer and use it in GitHub Desktop.
module DelegateError
extend ActiveSupport::Concern
included do
class_variable_set(:@@delegate_error_attrs , [])
after_validation :copy_errors_to_delegated_attribute
end
module ClassMethods
def delegate_errors(s_atr, t_atr)
attrs = delegate_error_attrs + [{source: s_atr, target: t_atr}]
class_variable_set(:@@delegate_error_attrs, attrs)
end
def delegate_error_attrs
class_variable_get(:@@delegate_error_attrs)
end
end
private
def copy_errors_to_delegated_attribute
self.class.delegate_error_attrs.each do |atr_hash|
self.errors[atr_hash[:source]].each do |e|
self.errors[atr_hash[:target]] << e
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment