Created
June 19, 2015 10:50
-
-
Save jbmyid/6f142461b7d6a34928c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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